🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

integrate.quad()

AI & DATA SCIENCE // integrate-quad

scipy.integrate.quad() numerically computes the definite integral of a single-variable function over a specified interval, using adaptive quadrature.

Syntax

scipy.integrate.quad(func, a, b)

Deep Dive Course

quad() takes a Python function, a lower bound a, and an upper bound b, and returns a tuple of the estimated integral value and an estimate of the numerical error in that result — it uses adaptive quadrature internally, automatically subdividing the interval and using more evaluation points in regions where the function changes rapidly, to achieve high accuracy without you needing to specify a fixed number of sample points yourself. Either bound can be infinite, letting quad() handle improper integrals over an unbounded interval directly.

1Understanding integrate.quad()

quad() takes a Python function, a lower bound a, and an upper bound b, and returns a tuple of the estimated integral value and an estimate of the numerical error in that result — it uses adaptive quadrature internally, automatically subdividing the interval and using more evaluation points in regions where the function changes rapidly, to achieve high accuracy without you needing to specify a fixed number of sample points yourself. Either bound can be infinite, letting quad() handle improper integrals over an unbounded interval directly.

💡

quad() returns a tuple of the result and an estimated error — a common mistake is treating its return value as just the integral result directly, forgetting to unpack the second element, or accidentally using the whole tuple where a single number was expected.

editor.html
from scipy import integrate
import numpy as np

result, error = integrate.quad(lambda x: x**2, 0, 3)
print(result)
localhost:3000

2Practical Example

Here is a real-world application of integrate.quad() showing how it is used in production SciPy code.

editor.html
from scipy import integrate
import numpy as np

result, error = integrate.quad(lambda x: np.exp(-x**2), -np.inf, np.inf)
print(round(result, 4))
localhost:3000

3Best Practices

Follow these guidelines when working with integrate.quad():

1. Always unpack both elements of quad()'s returned tuple, the integral value and the error estimate, rather than assuming it returns just a single number

2. Check the returned error estimate for functions that might be difficult to integrate accurately, very oscillatory, or with singularities, rather than blindly trusting the result

3. Pass an infinite bound directly for improper integrals over an infinite interval, instead of trying to approximate infinity with a very large finite number

⚠️

Tip: quad() returns a tuple of the result and an estimated error — a common mistake is treating its return value as just the integral result directly, forgetting to unpack the second element, or accidentally using the whole tuple where a single number was expected.

editor.html
from scipy import integrate
import numpy as np

result, error = integrate.quad(lambda x: x**2, 0, 3)
print(result)
localhost:3000

Examples

Example 01Basic Usage
from scipy import integrate
import numpy as np

result, error = integrate.quad(lambda x: x**2, 0, 3)
print(result)
Example 02Advanced Example
from scipy import integrate
import numpy as np

result, error = integrate.quad(lambda x: np.exp(-x**2), -np.inf, np.inf)
print(round(result, 4))

Best Practices

  • Always unpack both elements of quad()'s returned tuple, the integral value and the error estimate, rather than assuming it returns just a single number
  • Check the returned error estimate for functions that might be difficult to integrate accurately, very oscillatory, or with singularities, rather than blindly trusting the result
  • Pass an infinite bound directly for improper integrals over an infinite interval, instead of trying to approximate infinity with a very large finite number

Interview Question

Why does scipy.integrate.quad() return a tuple of two values instead of just the integral result alone?

Hint: Think about what the second value actually represents, and why that information is useful.

Numerical integration is fundamentally an approximation — quad() estimates the true integral using a finite number of function evaluations, and it can't guarantee the result is exactly correct, especially for functions that are difficult to integrate accurately, like highly oscillatory ones. The second returned value is an estimate of how large quad()'s own approximation error is likely to be, giving you a way to judge how much to trust the result and detect cases where the integration might have struggled, information that would be lost entirely if quad() only returned the bare numeric result with no indication of its own uncertainty.

Exercises

MediumPractice using integrate.quad() in a real scenario.
View Solution
from scipy import integrate
import numpy as np

result, error = integrate.quad(lambda x: x**2, 0, 3)
print(result)

Frequently Asked Questions

Why does scipy.integrate.quad() return a tuple of two values instead of just the integral result alone?

Numerical integration is fundamentally an approximation — quad() estimates the true integral using a finite number of function evaluations, and it can't guarantee the result is exactly correct, especially for functions that are difficult to integrate accurately, like highly oscillatory ones. The second returned value is an estimate of how large quad()'s own approximation error is likely to be, giving you a way to judge how much to trust the result and detect cases where the integration might have struggled, information that would be lost entirely if quad() only returned the bare numeric result with no indication of its own uncertainty.

Related Functions

integrate-dblquadintegrate-odeintnp-exp