Learn R Programming

Rdistance (version 4.1.1)

simpsonCoefs: Simpson numerical integration coefficients

Description

Return a vector of Simpson's Composite numerical integration coefficients.

Usage

simpsonCoefs(n)

Value

A vector of Simpson Composite rule coefficients suitable for numeric integration. The return is a vector of integers alternating between 4 and 2, with 1's on each end.

Arguments

n

Number of coefficients, which is the number of points at which the function of interest is evaluated. The number of intervals is (n-1)/2. This number must be odd.

Details

Let x be an vector of equally spaced points in the domain of a function f (equally spaced is critical). Let y = f(x). The numeric integral of f from min(x) to max(x) is sum(simpsonCoefs(length(y)) * y) * (x[2] - x[1]) / 3.

Examples

Run this code

x <- seq(0, 9, length=13)
y <- x^2

scoefs <- simpsonCoefs(length(x))

# exact integral is 9^3/3 = 243
sum( scoefs*y ) * (x[2] - x[1]) / 3

Run the code above in your browser using DataLab