trapz: Trapezoidal Integration

Description Usage Arguments Value Examples

View source: R/fitTimeSeries.R

Description

Compute the area of a function with values 'y' at the points 'x'. Function comes from the pracma package.

Usage

1
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)'. Or a matrix of the same size as 'y'.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Calculate the area under the sine curve from 0 to pi:
 n <- 101
 x <- seq(0, pi, len = n)
 y <- sin(x)
 trapz(x, y)          #=> 1.999835504

# Use a correction term at the boundary: -h^2/12*(f'(b)-f'(a))
 h  <- x[2] - x[1]
 ca <- (y[2]-y[1]) / h
 cb <- (y[n]-y[n-1]) / h
 trapz(x, y) - h^2/12 * (cb - ca)  #=> 1.999999969

metagenomeSeq documentation built on Nov. 8, 2020, 5:34 p.m.