Cumulative area from sampled data
Estimate accumulated area from measurements sampled at known, possibly nonuniform coordinates.
SciPy · Function
cumulative_trapezoid computes running integrals of sampled data with the composite trapezoidal rule. It supports explicit coordinates or uniform spacing and can preserve the input length with initial=0.
The function accumulates numerical integrals along one selected axis using the composite trapezoidal rule. Provide explicit coordinates with x, or use a uniform spacing dx when x is omitted; initial controls whether the result keeps the input length.
scipy.integrate.cumulative_trapezoid(y, x=None, dx=1.0, axis=-1, initial=None)| Name | Type | Description |
|---|---|---|
y | array_like | Sample values representing y(x). |
x | array_like, optional | Coordinates along the integration direction; if None, dx is used. |
dx | float, optional | Spacing between consecutive y values when x is None. |
axis | int, optional | Axis along which to accumulate; defaults to the last axis. |
initial | scalar, optional | Value inserted at the beginning; only 0 or None are accepted. |
res ndarray: the result of cumulative integration of y along axis; its integrated axis is one element shorter when initial is None, and has the same length as y when initial is given.
import numpy as np
from scipy.integrate import cumulative_trapezoid
x = np.linspace(0.0, 2.0, 5)
y = x**2
area = cumulative_trapezoid(y, x, initial=0)
print(area)Computes cumulative trapezoidal integrals of sampled \(x^2\) values and keeps the output aligned with x by inserting zero at the start.
Estimate accumulated area from measurements sampled at known, possibly nonuniform coordinates.
Integrate a multidimensional array independently along a selected dimension.
Generate an output aligned element-for-element with the original samples.
It applies the composite trapezoidal rule cumulatively along the chosen axis, returning the accumulated integral after each interval. With x omitted, consecutive samples are assumed to be separated by dx.
Follow-up: What happens to the output shape by default? It has one fewer element along the integration axis because no value is returned at the first sample.
x supplies the coordinates associated with y and permits nonuniform spacing. If x is None, the function instead uses the uniform spacing dx, whose default is 1.0.
Follow-up: Which spacing argument wins if both are supplied? x is used, so dx is ignored.
axis selects the dimension over which cumulative integration occurs and defaults to the last axis. Setting initial to 0 inserts a starting value, making the result the same shape as y.
Follow-up: What other initial value is accepted? None; the documentation permits only 0 or None.
scipy.integrate.trapezoidscipy.integrate.quad