Challenge - 5 Problems
Numerical Integration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Recall that trapz integrates using trapezoids between points with given x spacing.
✗ Incorrect
The trapezoidal rule sums areas of trapezoids between points. Here, intervals are [0,1], [1,2], [2,4]. Calculating each area and summing gives 23.
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
The integral of sin(x) from 0 to pi is a known exact value.
✗ Incorrect
The integral of sin(x) from 0 to pi is 2, which is the exact area under the sine curve in that interval.
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
Check the order and size of inputs to trapz function.
✗ Incorrect
trapz(X, Y) computes the integral of Y with respect to X. Here, the order is reversed causing it to compute integral of x with respect to y (0.6650) instead of y with respect to x (approx. 0.3350).
🧠 Conceptual
advanced2:00remaining
Comparing integral and trapz accuracy
Which statement best describes the difference between MATLAB's integral and trapz functions?
Attempts:
2 left
💡 Hint
Consider how each function approaches numerical integration.
✗ Incorrect
integral uses adaptive quadrature methods that adjust evaluation points for accuracy, while trapz uses fixed trapezoidal sums over given points, which can be less accurate for complex functions.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Remember trapz syntax: trapz(x, y) where x and y are vectors of same length.
✗ Incorrect
Option D correctly defines x and y vectors with element-wise power and uses trapz(x,y). Option D swaps arguments causing error. Option D uses ^ instead of .^ causing error. Option D swaps arguments and uses colon operator with step size 0.002 but swaps arguments causing error.