[R] Integration in R

David Winsemius dwinsemius at comcast.net
Tue Jan 8 19:55:32 CET 2013


On Jan 8, 2013, at 10:51 AM, Naser Jamil wrote:

> Thanks. But then how to implement condition like 0<x1<x2<7? I would  
> be happy to know that.

Multiply the function by the conditional expression:

 > f<-function(x) { 2/3 * (x[1] + x[2] )*(x[1] < x[2]) }
 > adaptIntegrate(f, lowerLimit = c(0, 0), upperLimit = c(7,7))
$integral
[1] 114.3333

-- 
David.
>
> On 8 January 2013 18:41, David Winsemius <dwinsemius at comcast.net>  
> wrote:
> Please reply on list.
>
>
> On Jan 8, 2013, at 10:27 AM, Naser Jamil wrote:
>
> Hi David,
> x[2] is the second variable, x2. It comes from the condition  
> 0<x1<x2<7.
>
> No, it doesn't come from those conditions. It is being grabbed from  
> some "x"-named object that exists in your workspace.
>
> If your limits were 7 in both dimensions, then the code should be:
>
> adaptIntegrate(f, lowerLimit = c(0, 0), upperLimit = c(7,7))
> #----
> $integral
> [1] 228.6667
>
> (At this point I was trusting R's calculus abilities more than  
> yours. I wasn't too trusting of mine either, and so tried seeing if  
> Wolfram Alpha would accept this expression:
>  integrate 2/3 (x+y) over 0< x<7,  0<y<7
>
> ; which it did and calculating the decimal expansion of the exact  
> fraction:
>
> > 686/3
> [1] 228.6667
> >
>
> -- 
> David.
>
>
>
>
> Thanks.
>
> On 8 January 2013 18:11, David Winsemius <dwinsemius at comcast.net>  
> wrote:
>
> On Jan 8, 2013, at 9:43 AM, Naser Jamil wrote:
>
> Hi R-users.
>
> I'm having difficulty with an integration in R via
> the package "cubature". I'm putting it with a simple example here.   
> I wish
> to integrate a function like:
> f(x1,x2)=2/3*(x1+x2) in the interval 0<x1<x2<7. To be sure I tried it
> by hand and got 114.33, but the following R code is giving me  
> 102.6667.
>
> -------------------------------------------------------------------
> library(cubature)
> f<-function(x) { 2/3 * (x[1] + x[2] ) }
> adaptIntegrate(f, lowerLimit = c(0, 0), upperLimit = c(x[2],7))
>
>
> What is x[2]?  On my machine it was 0.0761, so I obviously got a  
> different answer.
>
> -- 
> David Winsemius, MD
> Alameda, CA, USA
>
>
>
> David Winsemius, MD
> Alameda, CA, USA
>
>

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list