caTools (version 1.17.1)

trapz: Trapezoid Rule Numerical Integration

Description

Computes the integral of Y with respect to X using trapezoid rule integration.

Usage

trapz(x, y)

Arguments

Value

Integral of Y with respect to X or area under the Y curve.

concept

integration

Details

The function has only two lines: idx = 2:length(x) return (as.double( (x[idx] - x[idx-1]) %*% (y[idx] + y[idx-1])) / 2)

References

D. Kincaid & W. Chaney (1991), Numerical Analysis, p.445

See Also

  • integrate
  • Matlab'strapzfunction (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/trapz.html)

Examples

Run this code
# integral of sine function in [0, pi] range suppose to be exactly 2.
  # lets calculate it using 10 samples:
  x = (1:10)*pi/10
  trapz(x, sin(x))
  # now lets calculate it using 1000 samples:
  x = (1:1000)*pi/1000
  trapz(x, sin(x))

Run the code above in your browser using DataLab