Skip to content

Integration in R

7 messages · Naser Jamil, David Winsemius, Berend Hasselman

#
Please reply on list.
On Jan 8, 2013, at 10:27 AM, Naser Jamil wrote:

            
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
 >
#
On Jan 8, 2013, at 10:51 AM, Naser Jamil wrote:

            
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
#
On 08-01-2013, at 19:51, Naser Jamil <jamilnaser79 at gmail.com> wrote:

            
David implemented the condition by multiplying by x[1]<x[2] which in a numeric context is 0 when x[1]<x[2] en 1 when x[1]>=x[2]. That is what your requirement does.
The condition 0<x1<x2<7 has been split into three separate parts

0<x1<7
0<x2<7
x1<x2

The first two can be converted to arguments of adaptIntegrate.
The last is inserted into the function definition effectively making the function return 0 when x1>x2 which is what your inequality implies.


Berend
#
On 08-01-2013, at 22:00, Berend Hasselman <bhh at xs4all.nl> wrote:

            
OOPS!! Reverse the 0 and the 1 in that sentence (TRUE becomes 1 and FALSE becomes 0)

Berend
#
On Jan 8, 2013, at 1:07 PM, Berend Hasselman wrote:

            
I didn't worry about which direction was used for the inequality in  
this case because of consideration of symmetry. (Thinking about it  
now, I still think I managed to choose the correct direction.)   
Obviously such a cavalier attitude should not be carried over to the  
general situation.
#
On Jan 8, 2013, at 1:31 PM, David Winsemius wrote:

            
I hit the send button by mistake. (I originally misread Berend's  
correction.) I'm only posting this to make clear that it was myself,  
and not Berend, that I was accusing of having been cavalier.