Learn R Programming

calculus (version 0.2.0)

integral: Numerical Integration

Description

Integrates multidimensional functions, expressions, and characters in arbitrary orthogonal coordinate systems.

Usage

integral(f, bounds, relTol = 0.01, absTol = 0.001,
  coordinates = "cartesian", method = "mc", verbose = TRUE, ...)

Arguments

f

function, expression or character.

bounds

list of integration bounds.

relTol

relative accuracy requested.

absTol

absolute accuracy requested.

coordinates

coordinate system to use. One of: cartesian, polar, spherical, cylindrical, parabolic, parabolic-cylindrical or a character vector of scale factors for each varibale.

method

the method to use. Should be one of "mc", "hcubature", "pcubature", "cuhre", "divonne", "suave" or "vegas". Naive Monte Carlo integration by default. The additional methods require the cubature package to be installed (efficient integration in C).

verbose

logical. Print on progress?

...

additional arguments passed to the cubintegrate function, when method "hcubature", "pcubature", "cuhre", "divonne", "suave" or "vegas" is used.

Value

list with components

value

the final estimate of the integral.

abs.error

estimate of the modulus of the absolute error.

cuba

cubature output when method "hcubature", "pcubature", "cuhre", "divonne", "suave" or "vegas" is used.

Examples

Run this code
# NOT RUN {
# integrate character
integral('sin(x)', bounds = list(x = c(0,2*pi)))

# integrate expression
integral(parse(text = 'x'), bounds = list(x = c(0,1)))

# integrate function
integral(function(x) exp(x), bounds = list(x = c(0,1)))

# multivariate integral
integral(function(x,y) x*y, bounds = list(x = c(0,1), y = c(0,1)))

# surface of a sphere
integral('1', 
         bounds = list(r = 1, theta = c(0,pi), phi = c(0,2*pi)), 
         coordinates = 'spherical')

# volume of a sphere
integral('1', 
         bounds = list(r = c(0,1), theta = c(0,pi), phi = c(0,2*pi)), 
         coordinates = 'spherical')
         
# }
# NOT RUN {
# efficient integration in C (requires the cubature package to be installed)
integral('1',
        bounds = list(r = c(0,1), theta = c(0,pi), phi = c(0,2*pi)),
        coordinates = 'spherical',
        method = 'cuhre',
        relTol = 1e-06,
        absTol = 1e-12)
# }
# NOT RUN {
##################################
# Electric charge contained in a region of space
# (see divergence theorem and Maxwell's equations)
# 

# electric potential of unitary point charge 
V <- '1/(4*pi*r)'

# electric field
E <- -1 %prod% gradient(V, c('r', 'theta', 'phi'), coordinates = 'spherical')

# electric charge
integral(E[1], 
         bounds = list(r = 1, theta = c(0,pi), phi = c(0,2*pi)), 
         coordinates = 'spherical')


# }

Run the code above in your browser using DataLab