calculus (version 0.1.0)

integral: Monte Carlo Integration

Description

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

Usage

integral(f, ..., err = 0.01, rel = TRUE, coordinates = "cartesian",
  verbose = TRUE)

Arguments

f

function, expression or characters.

...

integration bounds.

err

acuracy requested.

rel

logical. Relative accuracy? If FALSE, use absolute accuracy.

coordinates

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

verbose

logical. Print on progress?

Value

list with components

value

the final estimate of the integral.

abs.error

estimate of the modulus of the absolute error.

Examples

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

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

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

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

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

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

##################################
# 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], r = 1, theta = c(0,pi), phi = c(0,2*pi), coordinates = 'spherical')


# }

Run the code above in your browser using DataCamp Workspace