z = trapz(x,y)
z = trapz(y)
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.
is 2. To approximate this numerically on a uniformly spaced grid, use
Then bothx = 0:pi/100:pi;
y = sin(x);
z = trapz(x,y)
and
producez = pi/100
*trapz(y)
A nonuniformly spaced example is generated byz =
1.9998
The result is not as accurate as the uniformly spaced grid. One random sample producedx = sort(rand(1,101)
*pi);
y = sin(x);
z = trapz(x,y);
z =
1.9984
cum
,quad
,quad8
,sum
(c) Copyright 1994 by The MathWorks, Inc.