Contents Previous Next Subchapters

Trapezoidal Approximation for Integrals
Syntax trapz(y)
trapz(xy)
trapz(yd)
trapz(xyd)
See Also gaussq , quadint

Description
Computes the trapezoidal approximation for the integral

x(N)           N-1
|                ---
|   F(xdx  ~=  >    [ x   -  x ] [ F(x ) + F(x  ) ] / 2
|                ---     i+1    i       i       i+1
x(1)           i=1

where N is the length of the vector x. The arguments x, y and d are integer, real, double-precision, or complex. The return value has the type that results from coercion between the type of x and the type of y.

If the argument x is not present, the default value
     x  =  i
      i
is used. The relationship between F(x), N, and the arguments y, d, is defined below.

trapz(
y)
If y is a row vector, N is the column dimension of y and
     y = F(i)
      i

If y is not a row vector, N is the row dimension of y and
     [y    , ... , y  ] = F(i)
       i,1          i,n
where n is the column dimension of y.

trapz(
xy)
If y is a row vector, N is the column dimension of y and
     y = F(x )
      i     i

If y is not a row vector, N is the row dimension of y and
     [y    , ... , y  ] = F(x )
       i,1          i,n      i
where n is the column dimension of y.

trapz(
yd)
trapz(
xyd)
The argument d must be equal to one or two. If d is equal to one, N is the row dimension of y and
     [y    , ... , y  ] = F(x )
       i,1          i,n      i
where n is the column dimension of y. If d is equal to two, N is the column dimension of y and
                       T
     [y    , ... , y  ]  = F(x )
       i,1          i,n       i
where n is the row dimension of y.

Example
The trapezoidal rule is exact for linear functions such as F(x) = x. In addition,
          1
     1   /   
     - = |  x  dx
     2   /
          0
If you enter
     clear
     x = linspace(0d0, 1d0, 5)
     y = x
     trapz(x, y)
O-Matrix will reply
     0.5

Reference
The arguments x and y each must have more than one element. This distinguishes them from the argument d in the syntax trapz(yd)