Navigation

Operators and Keywords

Function List:

C++ API

: q = trapz (y)
: q = trapz (x, y)
: q = trapz (…, dim)

Numerically evaluate the integral of points y using the trapezoidal method.

trapz (y) computes the integral of y along the first non-singleton dimension. When the argument x is omitted an equally spaced x vector with unit spacing (1) is assumed. trapz (x, y) evaluates the integral with respect to the spacing in x and the values in y. This is useful if the points in y have been sampled unevenly.

If the optional dim argument is given, operate along this dimension.

Application Note: If x is not specified then unit spacing will be used. To scale the integral to the correct value you must multiply by the actual spacing value (deltaX). As an example, the integral of x^3 over the range [0, 1] is x^4/4 or 0.25. The following code uses trapz to calculate the integral in three different ways.

x = 0:0.1:1;
y = x.^3;
q = trapz (y)
  ⇒ q = 2.525   # No scaling
q * 0.1
  ⇒ q = 0.2525  # Approximation to integral by scaling
trapz (x, y)
  ⇒ q = 0.2525  # Same result by specifying x

See also: cumtrapz.

Package: octave