Adaptive quadrature in from 1 to 20 dimensions.
Usage
adapt(ndim, lower, upper, minpts, maxpts, functn, eps,...)
integrate(functn, lower,upper,minpts=100,maxpts=500,eps=0.01,...)
Arguments
ndim
|
the number of dimensions of the function/integral
|
lower
|
vector of at least length ndim of the lower bounds on the integral
|
upper
|
vector of at least length ndim of the upper bounds on the integral
|
minpts
|
the minimum number of function evaluations
|
maxpts
|
the maximum number of function evaluations
|
functn
|
the name of an S function. functn should take a single vector
argument and possibly some parameters and return the function value at
that point. functn must return a single numeric value.
|
eps
|
The desired accuracy.
|
...
|
Other parameters to be passed to functn
|
Description
integrate()
integrates a function of 1 variable over a
specified interval,
adapt()
allows 1 to 20 variables and integrates over a rectangular
boxValue
structure containing finest, relerr, minpts and ifail.
finest
|
the estimated integral
|
relerr
|
the estimated relative error
|
minpts
|
the actual number of function evaluations
|
ifail
|
an error indicator. If ifail is not equal to 0, the function
warns the user of the error condition.
|
Note
This is modified from Mike Meyer's S code. The functions just
call A.C. Genz's fortran ADAPT subroutine to do all of the
calculations. A work array is allocated with the C/Fortran code. The
function may complain that maxpts is not large enough, and will
automatically make increase maxpts.
The Fortran function has been modified to use double precision, for
compatibility with R. It only works in two or more dimensions; for
one-dimensional integrals we integrate over a strip of unit width.
Examples
> fred <- function(z) { 0.3989423^length(z) * exp(-0.5 * sum(z * z))}
> adapt(3,c(-2,-2,-2),c(2,2,2),100,500,fred,.01)
Allocating a work space of size 54
$finest:
[1] 0.869198
$relerr:
[1] 2.579765e-05
$minpts:
[1] 345
$ifail:
[1] 0
In this example of a three dimensional spherical normal distribution,
integrate() took 345 function evaluations to find the integral.
integrate(dnorm,-1.96,1.96)
normloc<-function(x,mu) dnorm(x-mu)
integrate(normloc,-1.96,1.96,mu=0)
integrate(normloc,-2.96,0.96,mu=-1)