Do you need to find the area under a curve? The function scipy.integrate.quad lets you quickly and accurately find definite integrals in Python without using any symbolic math.
This is the one you use when you need the answer between two limits and you’re working with a real-world function (not a clean x^2). It doesn’t care if you can do it on paper. It simply gets the job done.
SciPy Beginner’s Learning Path
This is how it works in real life and when you should use it.
Start with the simplest case
Let’s say you want to integrate x² from 0 to 3. This is what the code looks like:
from scipy.integrate import quad
def f(x):
return x**2
result, error = quad(f, 0, 3)
print(result)
Output:
9.0
That’s the area under the parabola from x=0 to x=3. What you get back is the result and an estimate of the numerical error. For most cases, that error is negligible (usually something like 1e-14).
When your function isn’t clean
Now imagine you’re working with a bell curve. The function exp(-x²) has no elementary antiderivative. But quad doesn’t care.
from scipy.integrate import quad
import numpy as np
result, _ = quad(lambda x: np.exp(-x**2), -np.inf, np.inf)
print(result)
Output:
1.7724538509055159
Which is √π. That’s the Gaussian integral—solved numerically in one line.
What if your function needs parameters?
Let’s say the function you want to integrate is a * x², where a can vary. Pass any extra arguments using the args parameter:
def f(x, a):
return a * x**2
result, _ = quad(f, 0, 2, args=(3,))
print(result)
This integrates 3x² from 0 to 2. You can use this trick for any number of parameters, and quad will pass them through automatically.
Infinite limits? Just write them
You can use np.inf or -np.inf directly in your bounds. If your function tails off (like 1/(x² + 1)), this becomes incredibly useful:
from scipy.integrate import quad
import numpy as np
f = lambda x: 1 / (x**2 + 1)
area, _ = quad(f, -np.inf, np.inf)
print(area)
This evaluates to π. Try solving that by hand. Now imagine doing this on a noisy real-world function. That’s where numerical integration shines.
How accurate is it really?
The second value that quad gives back is the error estimate. If the errors are close to or bigger than your result, something is wrong. It might be:
- The function is discontinuous
- You have sharp spikes or poles in the interval
- You’re using quad where you should be breaking up the interval
You can split the integration into pieces manually if needed. Just sum the results of multiple quad calls over sub-intervals.
Common mistakes and when not to use it
One common mistake is trying to integrate a function that has vectors as its values. SciPy Quad only works with outputs that are scalars.
If you need:
- A function that returns arrays → use quad_vec
- Double or triple integrals → use dblquad or tplquad
- Approximations on discrete data points → use numpy.trapz or scipy.integrate.simps
quad isn’t your hammer for everything. It’s for smooth, continuous, single-variable functions with defined limits.
Practical use case: pharmacokinetics
Imagine you have a concentration-time curve for a drug and want to calculate the AUC (Area Under the Curve). You’d use quad to integrate your concentration function over time.
def drug_concentration(t):
return 5 * np.exp(-0.3 * t)
auc, _ = quad(drug_concentration, 0, 24)
Now you’ve got the total exposure of the drug over 24 hours. In clinical research, that’s a real number you report.
Final thoughts
Don’t just think of quad as a math function. Instead, think of it as a strong abstraction that lets you turn almost any curve into a number. This is true even if the curve came from an experiment, a simulation, or a complicated model that you can’t solve on paper.
It’s fast. It’s reliable. And once you start using it, you won’t want to do numeric integration by hand anymore.