Skip to main content
2 of 3
added 250 characters in body
Stéphane Laurent
  • 19.7k
  • 5
  • 76
  • 109

The integral2 function of the pracma package is a possibility:

library(pracma)

f <- function(x ,y) x + y

integral2(f, xmin = 0, xmax = 3, ymin = function(x) x, ymax = function(x) x+2)

If you are not allowed to use a package, you can nest the integrate function:

inner <- function(x) integrate(function(y) x+y, lower = x, upper = x + 2)$value
integrate(Vectorize(inner), lower = 0, upper = 3)

Both methods give 24.

Stéphane Laurent
  • 19.7k
  • 5
  • 76
  • 109