# 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