I would like to perform numerical integration in one dimension , where the integrand is a vector value . integrate() only allows scalar integrands, so I will need to call it several times. The cubature package seems well suited, but it seems to work pretty poorly for 1D integrals. Consider the following example (scalar integrand and 1D integration),
library(cubature) integrand <- function(x, a=0.01) exp(-x^2/a^2)*cos(x) Nmax <- 1e3 tolerance <- 1e-4
Be that as it may, adaptIntegrate seems to use many more functions for similar accuracy. Both methods seem to use the Gauss-Kronrod quadratic form (1D case: 15-point Gaussian quadrature rule), although ?integrate adds the “Wynn Epsilon algorithm”. Will this explain the big time difference?
I am open to suggestions on alternative ways to handle vector integrands such as
integrand <- function(x, a = 0.01) c(exp(-x^2/a^2), cos(x)) adaptIntegrate(integrand, -1, 1, tol=tolerance, fDim=2, maxEval=Nmax) $integral [1] 0.01772454 1.68294197 $error [1] 2.034608e-08 1.868441e-14 $functionEvaluations [1] 345
Thanks.
source share