integ: Simple numerical integration routine

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/integ.R

Description

Estimates the integral of a real-valued function using Simpson's or the Trapezoid approximation

Usage

1
2
integ(y, x = NULL, a = NULL, b = NULL, method = c("simpson",
  "trapezoid"))

Arguments

y

Vector of f(x) values

x

Numeric vector of sorted x values, each element of x should have a corresponding element in y. Only required for the trapezoid method. Not required for the Simpson method.

a

The lower limit of integration, only required for the Simpson method.

b

The upper limit of integration, only required for the Simpson method.

method

The method of integration (can use just the first letter). Defaults to simpson.

Details

For the Simpson method, y is a numeric vector of f(x), evaluated at an odd number of evenly spaced x's in the interval [a,b].

For the trapezoid method, the elements of x and y should correspond to one another, and x must be sorted in ascending order. The lengths or x and y should be the same, and they may be odd or even. The elements of x may be irregularly spaced.

Value

A single numeric estimate of the integral of f(x) over [a, b] (Simpson) or over the range of x (trapezoid).

Author(s)

Landon Sego

References

Ellis R, Gulick D. "Calculus: One and Several Variables," Harcourt Brace Jovanovich, Publishers: New York, 1991; 479-482.

See Also

integrate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# The Beta density from 0 to 0.7
integ(dbeta(seq(0, 0.7, length = 401), 2, 5), a = 0, b = 0.7)

# Checking result with the cdf
pbeta(0.7, 2, 5)

# f(x) = x^2 from 0 to 3
integ(seq(0, 3, length = 21)^2, a = 0, b = 3)

# A quadratic function with both methods
x <- seq(0, 3, length = 51)
integ(x^2, x = x, method = "t")
integ(x^2, a = 0, b = 3, method = "s")

# Now a linear function
x <- seq(0, 2, length = 3)
y <- 2 * x + 3
integ(y, x = x, method = "t")
integ(y, a = 0, b = 2)

Smisc documentation built on May 2, 2019, 2:46 a.m.