Aya Anas <aanas <at> feps.edu.eg> writes: > Hello all, > > I need to perform the following integration where the integrand is the > product of three functions: > f(x)g(y)z(x,y) > > the limits of x are(0,inf) and the limits of y are(-inf,inf). > > Could this be done using R?
There is a saying: Don't ask Can this be done in R?, ask How is it done? Extracting function f(x) from the inner integral may not always be the best idea. And applying package 'cubature' will not work as adaptIntegrate() does not really handle non-finite interval limits. As an example, let us assume the functions are f <- function(x) x g <- function(y) y^2 h <- function(x, y) exp(-(x^2+y^2)) Define a function that calculates the inner integral: F1 <- function(x) { fun <- function(y) f(x) * g(y) * h(x, y) integrate(fun, -Inf, Inf)$value } F1 <- Vectorize(F1) # requested when using integrate() We have to check that integrate() is indeed capable of computing this integrand over an infinite interval. F1(c(0:4)) # looks good ## [1] 0.000000e+00 3.260247e-01 3.246362e-02 3.281077e-04 3.989274e-07 Now integrate this function over the second (infinite) interval. integrate(F1, 0, Inf) ## 0.4431135 with absolute error < 2.4e-06 Correct, as the integral is equal to sqrt(pi)/4 ~ 0.44311346... If we extract f(x) from the inner integral the value of the integral and the computation times will be the same, but the overall handling will be slightly more complicated. > I tried using the function integrate 2 times, but it didn't work: > z<- function(x,y) { > > } > f<-function(x){ > rr<-"put here the function in x" *integrate(function(y) z(x, y), > -Inf,Inf)$value > return(rr) > } > > rr2<-integrate(function(x) f(x), 0, Inf)$value > print(rr2) > > I didn't get any output at all!!! > > Thanks, > Aya > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.