0
0
SciPydata~20 mins

Single integral (quad) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Integral Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
A4.000
B2.667
C1.333
D8.000
Attempts:
2 left
💡 Hint
Recall the integral of x^2 from 0 to 2 is (2^3)/3.
data_output
intermediate
2: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'])
AResult: 0.0, Points used: around 10
BResult: 1.0, Points used: around 5
CResult: 2.0, Points used: around 50
DResult: 3.1415, Points used: around 100
Attempts:
2 left
💡 Hint
The integral of sin(x) from 0 to π is 2.
🧠 Conceptual
advanced
1:30remaining
Understanding error estimate in scipy.quad
What does the second value returned by scipy.integrate.quad represent when computing an integral?
AThe number of function evaluations performed
BThe time taken to compute the integral
CThe relative error between two integration methods
DThe estimated absolute error of the integral approximation
Attempts:
2 left
💡 Hint
Think about what error means in numerical integration.
🔧 Debug
advanced
2: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)
ANo error, returns a finite result
BValueError: Integration bounds must be finite
CRuntimeWarning: divide by zero encountered in true_divide
DZeroDivisionError
Attempts:
2 left
💡 Hint
Consider what happens at x=0 for 1/x.
🚀 Application
expert
3: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))
A1.0
B2.0
C0.5
D1.5
Attempts:
2 left
💡 Hint
Split the integral at x=1 and sum the two parts.