fun <- function(x, y) cos(x) * cos(y)
integral2(fun, 0, 1, 0, 1, reltol = 1e-10)
# $Q: 0.708073418273571 # 0.70807341827357119350 = sin(1)^2
# $error: 8.618277e-19 # 1.110223e-16
## Compute the volume of a sphere
f <- function(x, y) sqrt(1 -x^2 - y^2)
xmin <- 0; xmax <- 1
ymin <- 0; ymax <- function(x) sqrt(1 - x^2)
I <- integral2(f, xmin, xmax, ymin, ymax)
I$Q # 0.5236076 - pi/6 => 8.800354e-06
## Compute the volume over a sector
I <- integral2(f, 0,pi/2, 0,1, sector = TRUE)
I$Q # 0.5236308 - pi/6 => 3.203768e-05
## Integrate 1/( sqrt(x + y)*(1 + x + y)^2 ) over the triangle
## 0 <= x <= 1, 0 <= y <= 1 - x. The integrand is infinite at (0,0).
f <- function(x,y) 1/( sqrt(x + y) * (1 + x + y)^2 )
ymax <- function(x) 1 - x
I <- integral2(f, 0,1, 0,ymax)
I$Q + 1/2 - pi/4 # -3.247091e-08
## Compute this integral as a sector
rmax <- function(theta) 1/(sin(theta) + cos(theta))
I <- integral2(f, 0,pi/2, 0,rmax, sector = TRUE, singular = TRUE)
I$Q + 1/2 - pi/4 # -4.998646e-11
## Compute a triple integral
fun <- function(x, y, z) y*sin(x) + z*cos(x)
integral3(fun, 0,pi, 0,1, -1,1) # - 2.0 => 0.0
Run the code above in your browser using DataLab