Bird
Raised Fist0
MATLABdata~20 mins

Numerical integration (integral, trapz) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Numerical Integration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of trapezoidal integration with uneven spacing
What is the output of the following MATLAB code snippet?
MATLAB
x = [0 1 2 4];
y = [0 1 4 16];
result = trapz(x, y);
disp(result);
A23
B18
C20
D22
Attempts:
2 left
💡 Hint
Recall that trapz integrates using trapezoids between points with given x spacing.
❓ Predict Output
intermediate
2:00remaining
Integral function output for sine over 0 to pi
What is the output of this MATLAB code?
MATLAB
f = @(x) sin(x);
result = integral(f, 0, pi);
disp(result);
A2
B1
C0
D3.1416
Attempts:
2 left
💡 Hint
The integral of sin(x) from 0 to pi is a known exact value.
🔧 Debug
advanced
2:00remaining
Identify the error in trapezoidal integration code
What error does this MATLAB code produce when run?
MATLAB
x = 0:0.1:1;
y = x.^2;
result = trapz(y, x);
disp(result);
AError: trapz expects x as first argument and y as second.
BThe code runs and outputs 0.6650
CError: Dimensions of arrays being concatenated are not consistent.
DError: Inputs must be vectors of the same length.
Attempts:
2 left
💡 Hint
Check the order and size of inputs to trapz function.
🧠 Conceptual
advanced
2:00remaining
Comparing integral and trapz accuracy
Which statement best describes the difference between MATLAB's integral and trapz functions?
Atrapz uses adaptive quadrature; integral uses fixed trapezoidal sums.
BBoth integral and trapz use the same algorithm but integral is faster.
Cintegral uses adaptive quadrature for higher accuracy; trapz uses simple trapezoidal sums which may be less accurate.
Dintegral only works for symbolic functions; trapz only works for numeric vectors.
Attempts:
2 left
💡 Hint
Consider how each function approaches numerical integration.
🚀 Application
expert
3:00remaining
Calculate integral of exp(-x^2) from -1 to 1 using trapz
Given the function f(x) = exp(-x^2), which option correctly computes the integral from -1 to 1 using trapz with 1000 points?
A
x = -1:0.002:1;
y = exp(-x.^2);
result = trapz(y,x);
B
x = linspace(-1,1,1000);
y = exp(-x.^2);
result = trapz(y,x);
C
x = linspace(-1,1,1000);
y = exp(-x^2);
result = trapz(x,y);
D
x = linspace(-1,1,1000);
y = exp(-x.^2);
result = trapz(x,y);
Attempts:
2 left
💡 Hint
Remember trapz syntax: trapz(x, y) where x and y are vectors of same length.

Practice

(1/5)
1. What does the MATLAB function integral primarily do?
easy
A. Finds the derivative of a function
B. Calculates the numerical integral of a function over a specified interval
C. Plots data points on a graph
D. Solves algebraic equations symbolically

Solution

  1. Step 1: Understand the purpose of integral

    The integral function in MATLAB is used to calculate the area under a curve defined by a function between two limits.
  2. Step 2: Compare with other options

    Options A, B, and C describe different MATLAB functions or operations unrelated to numerical integration.
  3. Final Answer:

    Calculates the numerical integral of a function over a specified interval -> Option B
  4. Quick Check:

    integral = numerical integration [OK]
Hint: Remember integral finds area under curve from function [OK]
Common Mistakes:
  • Confusing integral with symbolic math functions
  • Thinking integral plots graphs
  • Mixing integral with derivative functions
2. Which of the following is the correct syntax to compute the integral of sin(x) from 0 to pi using integral in MATLAB?
easy
A. integral(@sin, 0, pi)
B. integral(sin(x), 0, pi)
C. integral('sin(x)', 0, pi)
D. integral(sin, [0 pi])

Solution

  1. Step 1: Identify correct function handle syntax

    In MATLAB, integral requires a function handle, written as @functionName. So @sin is correct.
  2. Step 2: Check argument format

    The limits are given as separate arguments, so 0, pi is correct. Other options misuse syntax by passing strings or symbolic expressions.
  3. Final Answer:

    integral(@sin, 0, pi) -> Option A
  4. Quick Check:

    Use @ for function handle in integral [OK]
Hint: Use @ before function name for integral [OK]
Common Mistakes:
  • Passing function as string instead of handle
  • Using symbolic expressions instead of function handle
  • Putting limits inside brackets as one argument
3. What is the output of the following MATLAB code?
x = 0:0.5:2;
y = x.^2;
area = trapz(x, y);
disp(area);
medium
A. 2.5
B. 4.0
C. 3.0
D. 2.75

Solution

  1. Step 1: Calculate y values

    Given x = [0 0.5 1 1.5 2], y = x.^2 = [0 0.25 1 2.25 4].
  2. Step 2: Apply trapezoidal rule with trapz

    trapz approximates integral as sum of trapezoids:
    Area ≈ 0.5*((0+0.25)/2 + (0.25+1)/2 + (1+2.25)/2 + (2.25+4)/2)
    = 0.5*(0.125 + 0.625 + 1.625 + 3.125) = 0.5*5.5 = 2.75
  3. Final Answer:

    2.75 -> Option D
  4. Quick Check:

    trapz(x,y) ≈ 2.75 [OK]
Hint: Calculate y, then sum trapezoids with trapz [OK]
Common Mistakes:
  • Forgetting to square x for y
  • Using incorrect step size in calculation
  • Mixing up trapz and integral outputs
4. Identify the error in this MATLAB code snippet:
x = linspace(0,1,5);
y = x.^3;
result = trapz(y, x);
medium
A. Using .^3 is incorrect for element-wise power
B. linspace should have 6 points instead of 5
C. Arguments to trapz are reversed; should be trapz(x, y)
D. trapz cannot be used with vectors

Solution

  1. Step 1: Check trapz argument order

    trapz expects x values first, then y values. Here, arguments are reversed.
  2. Step 2: Validate other parts

    linspace with 5 points is valid; .^3 is correct for element-wise cube; trapz works with vectors.
  3. Final Answer:

    Arguments to trapz are reversed; should be trapz(x, y) -> Option C
  4. Quick Check:

    trapz(x,y) correct argument order [OK]
Hint: Remember trapz(x, y), x first then y [OK]
Common Mistakes:
  • Swapping x and y in trapz
  • Thinking linspace points count causes error
  • Misunderstanding element-wise power operator
5. You have noisy data points x = 0:0.1:1 and y = sin(2*pi*x) + 0.1*randn(size(x)). Which method is best to estimate the integral of the underlying sine wave over [0,1]?
hard
A. Use trapz(x, y) to approximate integral from noisy data
B. Use trapz(y, x) ignoring x order
C. Use integral(@(t) sin(2*pi*t), 0, 1) for exact integral
D. Use symbolic integration on noisy data

Solution

  1. Step 1: Understand data and goal

    The data y includes noise, so exact function integral is not possible from data alone.
  2. Step 2: Choose appropriate method

    Using trapz(x, y) approximates the integral from discrete noisy data points effectively.
  3. Step 3: Evaluate other options

    Use integral(@(t) sin(2*pi*t), 0, 1) for exact integral uses exact function without noise, not data-based; Use trapz(y, x) ignoring x order reverses arguments; Use symbolic integration on noisy data is invalid for numeric noisy data.
  4. Final Answer:

    Use trapz(x, y) to approximate integral from noisy data -> Option A
  5. Quick Check:

    trapz(x,y) best for noisy data integration [OK]
Hint: Use trapz with noisy data, integral needs exact function [OK]
Common Mistakes:
  • Using integral on noisy data without function
  • Swapping arguments in trapz
  • Trying symbolic integration on numeric noise
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard