Learn R Programming

cwhmisc (version 3.0)

adaptlob: Numerically evaluate integral using adaptive rule.

Description

adaptsim and adaptlob approximate the integral of the function f using adaptive Simpson and Lobatto rule. Both methods can deal with discontinuous functions. adaptlob is more efficient than adaptsim when the accuracy requirement is high. For lower tolerances, adaptsim is generally (but not always) more efficient than adaptlob, but less reliable.

Both routines show excellent response to changes in the tolerance. The function f must return a vector of output values if given a vector of input values.

adapt...(f,a,b) approximates the integral of f(x) from a to b to machine precision.

adapt...(f,a,b,tol) integrates to a relative error of tol.

adapt...(f,a,b,tol,trace=TRUE) displays the stepwise left end point of the current interval, the interval length, and the partial integral. adapt...(f,a,b,tol,trace,P1,P2,...) allows coefficients P1, ... to be passed directly to the function f: g <- f(x,P1,P2,...)

Usage

adaptsim(f, a,b,tol=.Machine$double.eps,trace=FALSE,...)
adaptlob(f, a,b,tol=.Machine$double.eps,trace=FALSE,...)

Arguments

f
function to be integrated.
a
starting abscissa of integral.
b
ending abscissa of integral.
tol
error tolerance > 0 of the final result.
trace
if TRUE then trace will be displayed.
...
additional coefficients for function f if necessary.

Value

  • List (Q, term) with Q = the approximate value of the integral and term = the information, whether the tolerance given was too small.

source

Walter Gautschi, 08/03/98. Reference: Gander, Computermathematik, Birkhaeuser, 1992.

References

Gander, W., Gautschi, W., 2000. Adaptive Quadrature - Revisited. ETH Zurich, DI IWR technical report 306. BIT 40, 1, 84--101.

Examples

Run this code
options(digits=7)
FexGander <- function(xx) ifelse(xx < 1,xx+1,ifelse(xx <= 3, 3 - xx, 2 ))
adaptsim(sin,0,pi,2.0e-3,TRUE)$Q - 2.0 #   -1.686905e-05
adaptsim(sin,0,pi,2.0e-23)$Q - 2.0  # 0
adaptsim(FexGander,0,5)$Q - 7.5 # -7.993606e-15 instead of 0
adaptlob(FexGander,0,5,2.0e-6,TRUE) # 7.500002 instead of 7.5
adaptlob(FexGander,0,5,2.0e-6)$Q - 7.5 #  1.781274e-06 instead of 0
adaptlob(FexGander,0,5)$Q-7.5 #   instead of  -8.881784e-16, with warnings
#  that required tolerance is too small.
adaptlob(FexGander,0,5,5.0*.Machine$double.eps)$Q-7.5 #  -5.329071e-15

Run the code above in your browser using DataLab