trapz
Numerical integral of sampled data by the trapezoidal rule.
Category: Mathematics > Numerical Integration and Differential Equations
Syntax
Q = trapz(Y)
Integrates Y assuming unit spacing between samples.
Q = trapz(X, Y)
Integrates Y against the sample points X.
Q = trapz(X, Y, dim)
Integrates along dimension dim.
Description
The curve is replaced by straight segments between samples and the areas of the resulting trapezoids are added. The method is exact for linear data and has an error that shrinks with the square of the spacing for smooth data.
It works directly on measured data, unlike integral, which needs a function. The running version is cumtrapz.
Input arguments
X- Vector of sample points, not necessarily evenly spaced.
Y- Vector, matrix or array of function values.
dim- Dimension to integrate along.
Output arguments
Q- Approximate integral. For a matrix, a row vector with one integral per column.
Example
Area under y = x
x = 0:0.25:1;
trapz(x, x)
trapz([1 2 3])
Output
ans = 0.5000
ans = 4