Home > Blockchain >  Double Integral with infinite and function bounds
Double Integral with infinite and function bounds

Time:01-08

I am trying to integrate double integrals with 0 to infinity and 0 to x as bounds in R. However, adaptIntegrate() cannot take function as bounds, and integral2 does not take infinite as bounds. Is there other functions that can integrate with these specific bounds?

Here’s the code for adaptIntegrate:

f <- function(x){exp((-(1/2)*x[1] - (1/3)*x[2]))}
adaptIntegrate(f, lowerLimit = c(0,0), upperLimit = c(Inf,x)

Also for integral2:

xmin <- 0; xmax <- 1
ymin <- 0; ymax <- function(x) sqrt(1 - x^2)
I <- integral2(f, xmin, xmax, ymin, ymax)

CodePudding user response:

One may nest two integrate to calculate double integrals, the outer partial function integrates the inner one. Since integrate evaluates f as a vectorized function, another function nested in sapply is needed in order to vectorize integrate itself, calculate each integral point (and to set the upper bound).
Does this answer you question?

f <- function(x, y){
  exp((-(1/2)*x - (1/3)*y))}

partial_x <- integrate(
  function(x) sapply(x, function(x) {
      partial_y <- integrate(
        function(y) sapply(y, function(y) 
          f(x, y)), 
        lower = 0, 
        upper = x)
      return(partial_y$value)
    }), 
  lower = 0,  
  upper = Inf)

partial_x$value
#> [1] 2.4

you can also use Vectorize instead sapply for the same purpose and not need of function nested inside sapply-

integrate(
  Vectorize(\(x) integrate(
    Vectorize(\(y) f(x, y)), 
    lower = 0, upper = x)$value),
  lower = 0, upper = Inf)$value

Same approach for arbitrary nested integrals.

Created on 2023-01-06 with reprex v2.0.2

  • Related