0
0
MATLABdata~20 mins

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

Choose your learning style9 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.