3

I want to calculate the following integral in R. enter image description here

I tried to use Vectorize and integrate functions but I got error
Error in (log(z)) * (InIntegl2) : non-numeric argument to binary operator

  fxyz= function(x,y,z) { (x*y*z)+z+x+2*y}
  InIntegl1 = Vectorize(function(x) { integrate(fxyz, 0,5)$value})
  InIntegl2 = Vectorize(function(y) { integrate( InIntegl1, 0,3)$value})
  InIntegl3 = Vectorize(function(z) { integrate((log(z))*(InIntegl2), 2,6)$value})
  Integral = integrate(InIntegl3 , 2, 6)$value
Adam
  • 131
  • 6

3 Answers3

4

The first integral must be parameterized by y and z and the second by z. Then we can perform the final integration.

int1 <- Vectorize(function(y, z) integrate(fxyz, 0, 5, y = y, z = z)$value)
int2 <- Vectorize(function(z) integrate(int1, 0, 3, z = z)$value)
integrate(function(z) log(z) * int2(z), 2, 6)$value
## [1] 2071.71
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
3

In the spirit of Numerical Triple Integration in R

integrate(Vectorize(function(z) { 
    log(z)*integrate(Vectorize(function(y) { 
        integrate(function(x) { x*y*z +x + 2*y + z}, 0, 5)$value }), 0,3)$value }), 2,6)
G5W
  • 36,531
  • 10
  • 47
  • 80
3

Package cubature can solve triple integrals with one call.

library(cubature)

f <- function(X){
  x <- X[1]
  y <- X[2]
  z <- X[3]
  log(z)*(x*y*z + x+ 2*y + z)
}
loLim <- c(0, 0, 2)
hiLim <- c(5, 3, 6)
tol <- .Machine$double.eps^0.5

hcubature(f, loLim, hiLim, tol = tol)
#$integral
#[1] 2071.71
#
#$error
#[1] 2.059926e-05
#
#$functionEvaluations
#[1] 165
#
#$returnCode
#[1] 0

If only the integral's value is needed,

hcubature(f, loLim, hiLim, tol = tol)$integral
#[1] 2071.71
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66