Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

integrate.cumulative_trapezoid

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.

How it works

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.

Syntax

scipy.integrate.cumulative_trapezoid(y, x=None, dx=1.0, axis=-1, initial=None)

Parameters

NameTypeDescription
yarray_likeSample values representing y(x).
xarray_like, optionalCoordinates along the integration direction; if None, dx is used.
dxfloat, optionalSpacing between consecutive y values when x is None.
axisint, optionalAxis along which to accumulate; defaults to the last axis.
initialscalar, optionalValue inserted at the beginning; only 0 or None are accepted.

Returns

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.

Key points

  • It produces running numerical integrals rather than only one total integral.
  • x provides the sample coordinates for integration.
  • Omitting x selects uniform spacing based on dx.
  • axis determines which dimension receives the cumulative operation.
  • The last axis is selected unless axis is explicitly changed.
  • Supplying initial preserves the input shape along the integrated dimension.

Common mistakes

  • initial is restricted to 0 or None; other starting values are invalid.
  • The default output is shorter by one element along the integration axis.
  • dx has no effect when explicit x coordinates are supplied.

Example: Cumulative integral of sampled data

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.

When to use it

Cumulative area from sampled data

Estimate accumulated area from measurements sampled at known, possibly nonuniform coordinates.

Axis-wise numerical integration

Integrate a multidimensional array independently along a selected dimension.

Shape-preserving cumulative results

Generate an output aligned element-for-element with the original samples.

Interview questions

QHow does cumulative_trapezoid perform the integration?

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.

QWhen should x be supplied instead of dx?

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.

QHow do axis and initial affect the result?

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.trapezoid
  • scipy.integrate.quad

Source: cumulative_trapezoid — SciPy v1.18.0 Manual. Checked 2026-10-15.