Learn R Programming

pracma (version 1.4.5)

trapz: Trapezoidal Integration

Description

Compute the area of a function with values y at the points x.

Usage

trapz(x, y)

Arguments

x
x-coordinates of points on the x-axis
y
y-coordinates of function values

Value

  • Approximated integral of the function from min(x) to max(x).

Details

The points (x, 0) and (x, y) are taken as vertices of a polygon and the area is computed using polyarea.

This approach matches exactly the approximation for integrating the function using the trapezoidal rule with basepoints x.

See Also

polyarea

Examples

Run this code
# Calculate the area under the sine curve from 0 to pi:
  x <- seq(0, pi, len=101)
  y <- sin(x)
  trapz(x, y)  #=> 1.999836

Run the code above in your browser using DataLab