romberg.RdRomberg Integration
romberg(f, a, b, maxit = 25, tol = 1e-12, ...)Simple Romberg integration with an explicit Richardson method applied to a series of trapezoidal integrals. This scheme works best with smooth and non-oscillatory functions and needs the least number of function calls among all integration routines.
The function does not need to be vectorized.
List of value, number or iterations, and relative error.
Chapra, S. C., and R. P. Canale (2006). Numerical Methods for Engineers. Fifth Edition, McGraw-Hill, New York.
Using a trapezoid formula Romberg integration will use
2*(2^iter-1)+iter function calls. By remembering function values
this could be reduced to 2^iter+1 calls.
romberg(sin, 0, pi, tol = 1e-15) # 2 , rel.error 1e-15
#> $value
#> [1] 2
#>
#> $iter
#> [1] 6
#>
#> $rel.error
#> [1] 0
#>
romberg(exp, 0, 1, tol = 1e-15) # 1.718281828459044 , rel error 1e-15
#> $value
#> [1] 1.718282
#>
#> $iter
#> [1] 7
#>
#> $rel.error
#> [1] 0
#>
# 1.718281828459045 , i.e. exp(1) - 1
f <- function(x, p) sin(x) * cos(p*x)
romberg(f, 0, pi, p = 2) # 2/3 , abs.err 1.5e-14
#> $value
#> [1] -0.6666667
#>
#> $iter
#> [1] 7
#>
#> $rel.error
#> [1] 5.995204e-15
#>
# value: -0.6666667, iter: 7, rel.error: 1e-12