Numerically evaluate an integral by Simpson's rule.

simpson(f, a, b, n = 100, ...)

Arguments

f

function of one variable, the integrand.

a, b

left and right endpoint of the interval.

n

number of sub-intervals, default 100.

...

additional parameters to be passed to the integrand.

Details

Simpson's rule calculates an integral of a function of one variable by approximating it quadratically. It is an easy to get and often quite accurate estimate.

Value

Numerical scalar, the value of the integral.

Note

This version has been generated by DeepSeek. For a non-vectorized function f use Vectorize(f)!

See also

Examples

simpson(sin, 0, pi, n=100)      # 2.0000000108245
#> [1] 2
simpson(sin, 0, pi, n=1000)     # 2.0000000000011
#> [1] 2

# Can also be used for a discretized function ...
xs <- seq(0, pi, length.out = 101)
ys <- sin(xs)
# ... by a linear approximation of that function
f <- approxfun(xs, ys, method = "linear")
simpson(f, 0, pi, n = 100)      # 2.0000000108245
#> [1] 2
simpson(f, 0, pi, n = 1000)     # 1.9998355038874
#> [1] 1.999836
trapz(xs, ys)                   # 1.9998355038875
#> [1] 1.999836