Numerical Integration Example with SciPy
Numerical integration is the process of finding the approximate value of an integral when it cannot be computed analytically.
In $SciPy$, the quad function from the scipy.integrate module is commonly used for this purpose.
It uses adaptive quadrature methods to calculate definite integrals efficiently.
Example Problem: Integrating a Function
Problem Statement:
We want to compute the integral of the following function over a specific interval:
$$
f(x) = e^{-x^2}
$$
We are asked to calculate the definite integral of this function from $(x = 0)$ to $(x = 1)$:
$$
I = \int_0^1 e^{-x^2} , dx
$$
This is a Gaussian-like function, and its integral does not have a simple analytical solution, so we will use numerical methods to compute it.
Solution Approach:
- Define the Function: Create a $Python$ function for $(f(x) = e^{-x^2})$.
- Use SciPy’s
quadFunction: This function computes the integral using an adaptive quadrature method. - Display the Result: We will compute the value of the integral and display it along with the estimated error.
Implementation in Python:
1 | import numpy as np |
Explanation:
Defining the Function:
- The function we are integrating is $(f(x) = e^{-x^2})$, a rapidly decaying Gaussian-like function.
- We define this function in $Python$ using $NumPy$’s
expfunction to handle the exponential.
Using
quadfor Numerical Integration:- $SciPy$’s
quadfunction is used for computing the definite integral. Thequadfunction returns two values:- The result of the integration (the estimated value of the integral).
- The error estimate, which tells us how accurate the numerical approximation is.
- The function takes three arguments: the integrand (function to be integrated), the lower bound $(0)$, and the upper bound $(1)$ of the integral.
- $SciPy$’s
Result and Error:
- The result is the approximate value of the definite integral, and the error estimate gives an indication of how close the approximation is to the actual value. In most cases, the error will be very small.
Output:
1 | Integral of exp(-x^2) from 0 to 1: 0.7468241328124271 |
Analysis of the Result:
- The integral of $(e^{-x^2})$ from $0$ to $1$ is approximately 0.7468.
- The estimated error in the computation is extremely small, about $(8.29 \times 10^{-15})$, which indicates that the result is very accurate.
Visualization (Optional):
We can visualize the function $(e^{-x^2})$ and the area under the curve from $(x = 0)$ to $(x = 1)$ to better understand what the integral represents.
1 | import matplotlib.pyplot as plt |

Key Takeaways:
- Numerical Integration: A method used to approximate the value of definite integrals when an exact analytical solution is difficult or impossible to obtain.
- SciPy’s
quadfunction: A powerful tool for performing numerical integration, capable of handling a wide variety of functions with high accuracy. - Error Estimation:
quadalso provides an estimate of the error in the approximation, helping to ensure the reliability of the result.
This example demonstrates how to use $SciPy$ to compute a definite integral numerically and shows how the integral of the function $(e^{-x^2})$ is approximated over a specific range.


