simpson.RdNumerically evaluate an integral by Simpson's rule.
simpson(f, a, b, n = 100, ...)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.
Numerical scalar, the value of the integral.
This version has been generated by DeepSeek. For a non-vectorized function f use Vectorize(f)!
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