Challenge - 5 Problems
Integral Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a definite integral using scipy.quad
What is the output of this code that calculates the integral of f(x) = x^2 from 0 to 2 using scipy.quad?
SciPy
from scipy.integrate import quad def f(x): return x**2 result, error = quad(f, 0, 2) print(round(result, 3))
Attempts:
2 left
💡 Hint
Recall the integral of x^2 from 0 to 2 is (2^3)/3.
✗ Incorrect
The integral of x^2 from 0 to 2 is (2^3)/3 = 8/3 ≈ 2.667. The code uses scipy.quad to compute this numerically.
❓ data_output
intermediate2:00remaining
Number of points used by scipy.quad for integral approximation
What is the type and approximate number of points used internally by scipy.quad when integrating f(x) = sin(x) from 0 to π?
SciPy
from scipy.integrate import quad import numpy as np def f(x): return np.sin(x) result, error = quad(f, 0, np.pi, full_output=1) print(result) print(error['neval'])
Attempts:
2 left
💡 Hint
The integral of sin(x) from 0 to π is 2.
✗ Incorrect
The integral of sin(x) from 0 to π is exactly 2. scipy.quad uses adaptive quadrature and typically evaluates around 50 points for this integral.
🧠 Conceptual
advanced1:30remaining
Understanding error estimate in scipy.quad
What does the second value returned by scipy.integrate.quad represent when computing an integral?
Attempts:
2 left
💡 Hint
Think about what error means in numerical integration.
✗ Incorrect
scipy.quad returns two values: the integral result and an estimate of the absolute error in the result.
🔧 Debug
advanced2:00remaining
Identify the error in this scipy.quad usage
What error will this code raise when trying to integrate f(x) = 1/x from 0 to 1 using scipy.quad?
SciPy
from scipy.integrate import quad def f(x): return 1/x result, error = quad(f, 0, 1) print(result)
Attempts:
2 left
💡 Hint
Consider what happens at x=0 for 1/x.
✗ Incorrect
The function 1/x has an improper integral at x=0 but scipy.quad can handle improper integrals with singularities at the boundary and returns a finite result.
🚀 Application
expert3:00remaining
Calculate integral of a piecewise function using scipy.quad
Using scipy.quad, what is the value of the integral of the function f(x) defined as f(x) = x for x < 1 and f(x) = 2 - x for x ≥ 1, integrated from 0 to 2?
SciPy
from scipy.integrate import quad def f(x): return x if x < 1 else 2 - x result, error = quad(f, 0, 2) print(round(result, 3))
Attempts:
2 left
💡 Hint
Split the integral at x=1 and sum the two parts.
✗ Incorrect
The integral from 0 to 1 of x dx is 0.5, and from 1 to 2 of (2 - x) dx is also 0.5, total 1.0.