trapz

Purpose

Trapezoidal numerical integration.

Synopsis

z = trapz(x,y)
z = trapz(y)

Description

z = trapz(x,y) computes the integral of y with respect to x using trapezoidal integration. x and y must be vectors of the same length, or x must be a column vector and y a matrix with as many rows as x. trapz computes the integral of each column of y separately. The resulting z is a scalar or a row vector.

z = trapz(y) computes the trapezoidal integral of y assuming unit spacing between the data points. To compute the integral for spacing other than one, multiply z by the spacing increment.

Examples

The exact value of

is 2. To approximate this numerically on a uniformly spaced grid, use

    x = 0:pi/100:pi;
    y = sin(x);
Then both

z = trapz(x,y)
and

z = pi/100*trapz(y)
produce

z =
    1.9998
A nonuniformly spaced example is generated by

x = sort(rand(1,101)*pi);
y = sin(x);
z = trapz(x,y);
The result is not as accurate as the uniformly spaced grid. One random sample produced

z =
    1.9984

See Also

cum, quad, quad8, sum

(c) Copyright 1994 by The MathWorks, Inc.