cumtrapz

Cumulative integral by the trapezoidal rule.

Category: Mathematics > Numerical Integration and Differential Equations

Syntax

Q = cumtrapz(Y)

Cumulative integral of Y with unit spacing.

Q = cumtrapz(X, Y)

Integrates against the sample points X.

Q = cumtrapz(X, Y, dim)

Integrates along dimension dim.

Description

Each output element is the area under the piecewise-linear interpolant from the first sample up to that point. The last element therefore equals trapz(X, Y).

It is exact for linear data and has error proportional to the square of the spacing for smooth data. It is a quick way to get a running integral, such as distance from sampled speed.

Input arguments

X
Vector of sample points, same length as Y along the integration dimension.
Y
Vector or array of function values.
dim
Dimension to integrate along.

Output arguments

Q
Array of the same size as Y; its first element along dim is 0.

Example

Running integral of x

x = 0:0.5:2;
cumtrapz(x, x)
cumtrapz([1 2 3])
Output
ans = 1×5

         0    0.1250    0.5000    1.1250    2.0000
ans = 1×3

         0    1.5000    4.0000

See also

trapz cumsum integral diff