5. You want to find the area under the curve of f(x) = sin(x) from 0 to π using SciPy's quad. Which code correctly computes this area and why does it work?
hard
A. import scipy
result = scipy.integrate.quad(sin, 0, 3.14)
print(result[0])
B. from scipy.integrate import quad
result, error = quad(lambda x: sin(x), 0, 3.14)
print(result)
C. from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(round(result, 2))
D. from scipy.integrate import quad
result = quad(math.sin, 0, math.pi)
print(result)
Solution
Step 1: Check function and limits
We want to integrate sin(x) from 0 to π. math.sin and math.pi provide correct function and limit.
Step 2: Use quad correctly
quad returns (result, error), so unpack both. Then round result for clear output.
Step 3: Why it works
Using math.sin ensures correct sine function, math.pi is precise π, and unpacking captures integral value.
Final Answer:
from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(round(result, 2)) -> Option C
Quick Check:
Correct function, limits, unpacking = from scipy.integrate import quad
import math
result, error = quad(math.sin, 0, math.pi)
print(round(result, 2)) [OK]
Hint: Use math.sin and math.pi with quad, unpack result [OK]
Common Mistakes:
Using undefined sin without import
Not unpacking quad output
Using approximate pi (3.14) instead of math.pi
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.