Message Boards Message Boards

3 Replies
3 Total Likes
View groups...
Share this post:

Monte Carlo Multiple Integration

Posted 11 years ago
I am working on a problem and have gotten stuck on trying to find the volume of the solid that lies over a region and is between surfaces. I am trying to write the code for randomly putting dots inside and outside the object and the using the ratio of inside to outside to find the volume? I have attached a notebook of my progress so far as well as a word ducument of the problem. Any help would be greatly appreciated.
POSTED BY: college student
3 Replies
I think there is an error in the Mathematica notebook attached to the original question -- fun5 is defined with lower case variables (x and y) instead of upper case variables (X and Y) as the rest of the functions fun1, fun2, etc.

You can use NIntegrate to verify the implementations in this project. Below is some code that might help.
Clear[fun1, fun2, fun3, fun4, fun5]
fun1[X_, Y_] := X^2 + Y^3;
fun2[X_, Y_] := X^2/4 + Y^2;
fun3[X_, Y_] := X - Y^2;
fun4[X_, Y_] := Y + 3 X^2;
fun5[x_, y_] := Cos[y^2] + E^-x^2;

First let us use NIntegrate's MonteCarlo integration method:
In[468]:= NIntegrate[
Boole[fun1[X, Y] >= 4 || fun2[X, Y] >= 2 || fun3[X, Y] >= 1 ||
   fun4[X, Y] <= -1/4 || fun5[X, Y] <= 0], {X, -2, 2}, {Y, -2, 2}, {Z,
   0, 2}, Method -> "MonteCarlo"]

Out[468]= 16.1861

The result is close to the result given by the default non-Monte Carlo integration method:
In[463]:= NIntegrate[
Boole[fun1[X, Y] >= 4 || fun2[X, Y] >= 2 || fun3[X, Y] >= 1 ||
   fun4[X, Y] <= -1/4 || fun5[X, Y] <= 0], {X, -2, 2}, {Y, -2, 2}, {Z,
   0, 2}, PrecisionGoal -> 2]

Out[463]= 16.2698

Here is how we can see the sampling points:
res = Reap[
    Boole[fun1[X, Y] >= 4 || fun2[X, Y] >= 2 || fun3[X, Y] >= 1 ||
      fun4[X, Y] <= -1/4 || fun5[X, Y] <= 0], {X, -2, 2}, {Y, -2,
     2}, {Z, 0, 2}, Method -> "MonteCarlo",
    EvaluationMonitor :> Sow[{X, Y, Z}]]];

Using Select we can plot the points for which the condition given to Boole gives True:
Point[Select[res[[2, 1]],
   Block[{X, Y, Z}, {X, Y, Z} = #;
     fun1[X, Y] >= 4 || fun2[X, Y] >= 2 || fun3[X, Y] >= 1 ||
      fun4[X, Y] <= -1/4 || fun5[X, Y] <= 0] &]]]
POSTED BY: Anton Antonov
Posted 11 years ago
So far I haven't really goten any wrong answers as far as I know. On the first part where I am simply trying to find the area of the object, I believe the code is good. When it comes to adding the upper surface I realy don't know where to begin actually trying to add this element into the code. So far I have tried adding a Z plane and giving it limits. Would any one know if this is even the right approch?
POSTED BY: college student
Posted 11 years ago
Your post hasn't precisely said what incorrect result you are seeing, unless it might be in your Word document.

I am guessing it might be in your last calculation.

Suggestion: I find I make fewer errors if I simply copy the original inequalities into the If,
rather than trying to manually reverse the direction of the inequalities, make extra equations, etc.
In your code this would then mean I would have to swap the position of Pin and Pout,
but I would hopefully make fewer errors doing it that way.

Suggestion: I might look really carefully at the volume of the box you are using in your last calculation.
You are also calculating NumberInside/NumberOutside*VolumeOfBox. Is that really correct?
That is not the same calculation you were doing further up in your earlier examples.

Suggestion: If you find and correct some mistakes I urge you not to say "that was a stupid mistake"
and try to as quickly as possible erase every trace of that. Instead I suggest you try to think of why
that sort of mistake might have happened and what rule or practice or habit you might follow in the
future which would try to ensure that this class or mistake not happen again. With careful observation
and thought you might be able to avoid some or even most these kinds of errors in the future.
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract