Sampled-curve integration
Estimate the area under experimentally or computationally sampled curves.
SciPy · Function
Numerically integrates sampled values with the composite trapezoidal rule. It can use explicit sample points or uniform spacing and integrates along a selected axis.
The function applies the composite trapezoidal rule to each one-dimensional slice of y along the selected axis. With x supplied, it uses the sample points in their given order; otherwise, it assumes uniform spacing specified by dx.
scipy.integrate.trapezoid(y, x=None, dx=1.0, axis=-1)| Name | Type | Description |
|---|---|---|
y | array_like | Input samples to integrate. |
x | array_like, optional | Sample points corresponding to y; defaults to uniform spacing when omitted. |
dx | scalar, optional | Spacing between sample points when x is None; default 1. |
axis | int, optional | Axis along which to integrate; default is the last axis. |
float for one-dimensional y; an n-1 dimensional array for higher-dimensional y.
import numpy as np
from scipy.integrate import trapezoid
x = np.linspace(0.0, np.pi, 1001)
area = trapezoid(np.sin(x), x=x)
print(area) # approximately 2.0Computes the area under sampled sine values over the interval from 0 to π.
Estimate the area under experimentally or computationally sampled curves.
Integrate arrays of signals or measurements along rows, columns, or another dimension.
Compute signed integrals for data whose sample coordinates are explicitly known.
Evaluate integrals represented by coordinates along a parametric path.
It computes a numerical integral with the composite trapezoidal rule along the requested axis. For one-dimensional y, the result is a float; higher-dimensional input produces one fewer dimension along that axis.
Follow-up: What happens when x is omitted? The samples are treated as evenly spaced, using dx as the spacing.
x contains sample points corresponding to y and determines the interval widths. If x is None, the function instead uses a constant spacing of dx, whose default is 1.
Follow-up: Does the function sort x first? No; it integrates in the sequence supplied by the caller.
axis selects the dimension over which integration occurs, and its default is the last axis, represented by -1. This lets the same function reduce rows, columns, or another dimension of an array.
Follow-up: What shape results from integrating an n-dimensional array? The selected axis is removed, so the result has one fewer dimension.
scipy.integrate.simpsonscipy.integrate.cumulative_trapezoid