Challenge - 5 Problems
Trapezoidal Rule Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of trapezoidal integration on simple data
What is the output of the following code that uses scipy's trapezoidal rule to integrate y = [1, 2, 3, 4] over x = [0, 1, 2, 3]?
SciPy
import numpy as np from scipy.integrate import trapezoid x = np.array([0, 1, 2, 3]) y = np.array([1, 2, 3, 4]) result = trapezoid(y, x) print(result)
Attempts:
2 left
💡 Hint
Remember trapezoidal rule sums areas of trapezoids between points.
✗ Incorrect
The trapezoidal rule calculates the integral as the sum of trapezoid areas. Here, the intervals are 1 unit each, so the integral is (1+2)/2*1 + (2+3)/2*1 + (3+4)/2*1 = 1.5 + 2.5 + 3.5 = 7.5.
❓ data_output
intermediate1:30remaining
Number of intervals used in trapezoidal integration
Given x = [0, 2, 4, 6, 8] and y = [0, 4, 16, 36, 64], how many trapezoids does scipy.integrate.trapezoid use to compute the integral?
SciPy
import numpy as np from scipy.integrate import trapezoid x = np.array([0, 2, 4, 6, 8]) y = np.array([0, 4, 16, 36, 64]) result = trapezoid(y, x) print(len(x) - 1)
Attempts:
2 left
💡 Hint
Number of trapezoids is one less than number of points.
✗ Incorrect
The trapezoidal rule uses intervals between points. With 5 points, there are 4 intervals, so 4 trapezoids.
🔧 Debug
advanced2:00remaining
Identify the error in trapezoidal integration code
What error will this code raise?
import numpy as np
from scipy.integrate import trapezoid
x = np.array([0, 1, 2])
y = np.array([1, 2])
result = trapezoid(y, x)
print(result)
SciPy
import numpy as np from scipy.integrate import trapezoid x = np.array([0, 1, 2]) y = np.array([1, 2]) result = trapezoid(y, x) print(result)
Attempts:
2 left
💡 Hint
Check if x and y arrays have the same length.
✗ Incorrect
The trapezoid function requires x and y arrays to be the same length. Here, y has length 2 but x has length 3, so it raises a ValueError.
🚀 Application
advanced2:30remaining
Calculate integral of sin(x) from 0 to pi using trapezoidal rule
Using numpy and scipy, which option correctly computes the integral of sin(x) from 0 to π using 100 intervals?
SciPy
import numpy as np from scipy.integrate import trapezoid x = np.linspace(0, np.pi, 101) y = np.sin(x) result = trapezoid(y, x) print(round(result, 4))
Attempts:
2 left
💡 Hint
The exact integral of sin(x) from 0 to π is 2.
✗ Incorrect
Using 100 intervals, trapezoidal rule approximates the integral close to 2, here 1.9998 is the rounded result.
🧠 Conceptual
expert2:00remaining
Effect of non-uniform spacing on trapezoidal integration
Which statement best describes how scipy.integrate.trapezoid handles non-uniform spacing in x values?
Attempts:
2 left
💡 Hint
Think about how trapezoidal rule calculates area between points.
✗ Incorrect
The trapezoidal rule calculates area using the width between points. scipy.integrate.trapezoid uses the actual spacing between x points, so it works correctly with non-uniform spacing.