SciPy Integration (original) (raw)
Last Updated : 5 Jul, 2025
**Integration is a fundamental concept in calculus used to calculate areas under curves, volumes and in solving differential equations. In Python, the **SciPy library provides tools to perform both definite and indefinite integration using **scipy.integrate module.
Finding Integration using scipy.integrate
Numerical integration means calculating the value of an integral using approximation methods instead of solving it analytically. Common methods in scipy.integrate are:
- **quad : General purpose integration (for most 1D integrals)
- **dblquad : Double integration (for 2D integrals)
- **nquad : Multi-dimensional integration (n-fold integrals)
- **fixed_quad : Uses fixed order Gaussian quadrature
- **cumulative_trapezoid : Cumulative trapezoidal integration
- **simpson : Simpson’s rule (more accurate than trapezoidal rule)
Let's explore Examples of each method to understand it better.
**1. quad
**quad function computes definite integral of a function with respect to a single variable over given interval, points can be +infinite or - infinite to indicate infinite limits.
It returns two values:
- The integrated result
- An estimate of the absolute error
**Example:
Python `
from scipy.integrate import quad
def f(x): return 3 * x**2 + 1
I, err = quad(f, 0, 1) print(I) print(err)
`
**Output
2.0
2.220446049250313e-14
**2. dblquad
**dblquad function computes double integral of a function with two variables over a rectangular or curved region.
It takes:
- The integrand function (in terms of x and y)
- Limits for the outer variable (x)
- Lower and upper limits for the inner variable (y) as functions of x
**Example:
Python `
from scipy.integrate import dblquad
A = dblquad(lambda x, y: x * y, 0, 0.5,
lambda x: 0, lambda x: 1 - 2*x)
print(A)
`
**Output
(0.010416666666666668, 4.101620128472366e-16)
**3. nquad
**nquad function computes integrals of functions with multiple variables. It is a flexible method for performing n-dimensional integration over specified limits.
**Example:
Python `
from scipy.integrate import nquad
def f(x, y, z): return x * y * z
I = nquad(f, [[0, 1], [0, 5], [0, 5]]) print(I)
`
**Output
(78.12499999999999, 8.673617379884033e-13)
**4. fixed_quad
**fixed_quad function performs definite integration using Gaussian quadrature with a fixed number of points (defined by the n parameter).
**Example:
Python `
from scipy import integrate
def func(x): return 3 * x**3
B = integrate.fixed_quad(func, 1.0, 2.0, n=2) print(B)
`
**Output
(11.25), None
**5. cumulative_trapezoid
**cumulative_trapezoid() function computes cumulative integral of discrete data using the trapezoidal rule. Returns the running total of area showing how the integral accumulates step by step.
**Example:
Python `
from scipy.integrate import cumulative_trapezoid import numpy as np
x = np.arange(0, 5) y = np.arange(0, 5)
S = cumulative_trapezoid(y, x) print(S)
`
**Output
[0.5 2. 4.5 8. ]
**6. simpson
**simpson() function estimates area under a curve using Simpson’s rule. It works with discrete data points and gives more accurate results than the trapezoidal rule especially for smooth curves.
**Example:
Python `
import numpy as np from scipy.integrate import simpson
x = np.arange(0, 5) y = np.arange(0, 5)
S = simpson(y, x) print(S)
`