trapz

Syntax

Description

Q = trapz(Y) returns
the approximate integral of Y via the trapezoidal method with
unit spacing. The size of Y determines the dimension
to integrate along:

If Y is a vector, then trapz(Y) is
the approximate integral of Y.

If Y is a matrix, then trapz(Y) integrates
over each column and returns a row vector of integration values.

If Y is a multidimensional array,
then trapz(Y) integrates over the first dimension
whose size does not equal 1. The size of this dimension becomes 1,
and the sizes of other dimensions remain unchanged.

Q = trapz(X,Y) integrates Y with
spacing increment X. By default, trapz operates
on the first dimension of Y whose size does not
equal 1. length(X) must be equal to the size of
this dimension . If X is a scalar, then trapz(X,Y) is
equivalent to X*trapz(Y).

Q = trapz(___,dim) integrates
along the dimension dim using any of the previous
syntaxes. You must specify Y, and optionally can
specify X. The length of X,
if specified, must be the same as size(Y,dim).
For example, if Y is a matrix, then trapz(X,Y,2) integrates
each row of Y.

When the spacing between points is constant, but not equal to
1, you can multiply by the spacing value, in this case pi/100*trapz(Y).
The answer is the same if you pass the value directly to the function
with trapz(X,Y).

The columns of Y represent velocity data,
taken at the times contained in X, for several
different trials.

Use trapz to integrate each column
independently and find the total distance traveled in each trial.
Since the function values are not evaluated at constant intervals,
specify X to indicate the spacing between the data
points.

Q = trapz(X,Y)

Q =
82.8000 85.7250 83.2500 80.7750

The result is a row vector of integration values, one for each
column in Y. By default, trapz integrates
along the first dimension of Y whose size does
not equal 1.

Alternatively, you can integrate the rows of a matrix
by specifying dim = 2.

In this case, use trapz on Y',
which contains the velocity data in the rows.

dim = 2;
Q1 = trapz(X,Y',dim)

Q1 =
82.8000
85.7250
83.2500
80.7750

The result is a column vector of integration values, one for
each row in Y'.

trapz integrates numeric data rather than
functional expressions, so in general the expression does not need
to be known to use trapz on a matrix of data.

Use trapz to approximate the double
integral

I=∫−55∫−33(x2+y2)dxdy.

To perform double or triple integrations on an array of numeric
data, nest function calls to trapz.

I = trapz(y,trapz(x,F,2))

I =
680.2000

trapz performs the integration over x first,
producing a column vector. Then, the integration over y reduces
the column vector to a single scalar. trapz slightly
overestimates the exact answer of 680 because f(x,y) is
concave up.

trapz reduces the size of the dimension
it operates on to 1, and returns only the final integration value. cumtrapz also
returns the intermediate integration values, preserving the size of
the dimension it operates on.