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? 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 -- Best Regards Faculty of Economics & Political Science Cairo University Tel:(202)35728055-(202)35728116-(202)35736608-(202)35736605 Fax:(202)35711020 Follow us on twitter:https://twitter.com/fepsnews [[alternative HTML version deleted]]
> -----Original Message----- > 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? I tried using the function integrate 2 times, but it > didn't work: > z<- function(x,y) { > > > }.....> > I didn't get any output at all!!!The function z() you provided is not doing anything; it is defined in your post as z<-function(x,y){} Surprised you didn't get at least NULL, though. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
On Dec 4, 2013, at 12:07 PM, Aya Anas wrote:> 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)Since f(x) does not depend on y, could you not do: Int( f(x) * Int( Int( g(y)*z(x,y).dy)).dx) # not R code Then use the standard approaches described in several postings to R-help over the years. I'm not a mathematician so anyone is free to correct this.> > the limits of x are(0,inf) and the limits of y are(-inf,inf). > > Could this be done using R? I tried using the function integrate 2 times, > but it didn't work: > z<- function(x,y) { > > > }What we see above is empty space where a body of a function should be.> f<-function(x){ > rr<-"put here the function in x" *integrate(function(y) z(x, y), > -Inf,Inf)$value > return(rr) > }Surely you didn't enter that!> > rr2<-integrate(function(x) f(x), 0, Inf)$value > print(rr2)Perhaps using a more modern tool would be quicker than learning how to do multiple integration with `integrate()`: http://cran.r-project.org/web/packages/cubature/index.html> > I didn't get any output at all!!!Rather that send extraneous exclamation marks, please read the Posting Guide and learn how to get your mail client to send plain text so that the full R code can flow freely onto our devices from yours.> > Thanks, > Aya > > > -- > Best Regards> > [[alternative HTML version deleted]]No, no, no.> > ______________________________________________ > R-help at 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.-- David Winsemius Alameda, CA, USA
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 >