Search code examples
rintegral

Triple integration using R when each inner integral depends on the others


My question is similar to "double integral in R.", but in my case, I want to do a triple integration where each limit depends on the outer integral.

I wnat to calculate

enter image description here

How do I define the inner dependencies?


Solution

  • I think, that basing on the suggestion from the post you have attached the key is to add the Vectorize function and we come up with something like this:

    # Example function 
    phi <- function(t){t}
    
    integrate(Vectorize(function(x) { 
      integrate(Vectorize(function(y) { 
        integrate(function(z) { 
          phi(x)*phi(y)*phi(z)   ## function you want to calculate
        }, -10, 6-x-y)$value     ## 2nd level interval
       }), -5, 3-x)$value        ## 1st level interval
    }), -5,4)                    ## top interval
    
    # 16480.2 with absolute error < 1.8e-10
    

    the above approach is quite general, so you can test on other functions. (The answer is the same as provided by WolframAlpha)