Learn R Programming

oce (version 0.9-18)

integrateTrapezoid: Use trapezoidal integration

Description

Estimate the integral of one-dimensional function using the trapezoidal rule.

Usage

integrateTrapezoid(x, y, type=c("A", "dA", "cA"))

Arguments

x
x values, or a single value that is taken as the (constant) difference between x values.
y
y values, with length (n, say) that must match that of x. If y is not given, the integration is done with the x values taken to be y, and $dx$ taken to be 1.
type
Flag indicating the desired return value (see Value).

Value

  • If type="A" (the default), a single value is returned, containing the estimate of the integral of y=y(x). If type="dA", a numeric vector of the same length as x, of which the first element is zer0, the second element is the integral between x[1] and x[2], etc. If type="cA", the result is the cumulative sum (as in cumsum) of the values that would be returned for type="dA". See Examples.

Bugs

There is no handling of NA values.

Examples

Run this code
x <- seq(0, 1, length.out=10) # try larger length.out to see if area approaches 2
y <- 2*x + 3*x^2
A <- integrateTrapezoid(x, y)
dA <- integrateTrapezoid(x, y, "dA")
cA <- integrateTrapezoid(x, y, "cA")
print(A)
print(sum(dA))
print(tail(cA, 1))
print(integrateTrapezoid(diff(x[1:2]), y))
print(integrateTrapezoid(y))

Run the code above in your browser using DataLab