Monte Carlo Volume Calculation and Speed Up - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-23T10:02:11Z https://mathematica.stackexchange.com/feeds/question/69050 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/69050 4 Monte Carlo Volume Calculation and Speed Up Amzoti https://mathematica.stackexchange.com/users/5074 2014-12-15T03:58:00Z 2014-12-15T06:06:11Z <p>I am trying to do a Monte Carlo area calculation for an irregular area defined by:</p> <p>$$0 \le x \le 1, 10 \le y \le 13, y \ge 12 \cos(x), y \ge 10 + x^3$$</p> <p>I used the code with modifications (so I can actually read it) from: <a href="https://mathematica.stackexchange.com/questions/35683/finding-the-volume-of-a-sphere-using-the-monte-carlo-algorithm?lq=1">Finding the volume of a sphere using the Monte Carlo algorithm</a></p> <pre><code> vol[num_] := Module[{hit, miss, index, x, y}, hit = 0; miss = 0; For[index = 1, index &lt;= num, ++index, x = RandomReal[{0, 1}]; y = RandomReal[{10, 13}]; If[y &gt;= 12 Cos[x] &amp;&amp; y &gt;= 10 + x^3, ++hit]]; hit/num] Print["time and value...... :", Timing[N[vol]]] </code></pre> <p>It appears to be giving incorrect results (off by a factor of $\approx 3$):</p> <p>$$\text{time and value...... :}\{0.015600,0.663\}$$</p> <p>Any ideas why off by a factor of $\approx 3$ (the book claims $2.000346869$).</p> <p>Can we easily print the hit and miss ratios?</p> <p>Also, what is the best way to speed this up to do a several million trials? Certainly the code works for that value of $n$, but it is likely slow compared to what is possible.</p> <p><strong>Update</strong></p> <p>Curiously, if you look at: <a href="https://books.google.com/books?id=ZUfVZELlrMEC&amp;pg=PA759&amp;dq=use+the+Monte+Carlo+method+to+find+the+volume+of+an+ice+cream+cone&amp;hl=en&amp;sa=X&amp;ei=VGeOVNnjHonroATJiILIBQ&amp;ved=0CCgQ6AEwAA#v=onepage&amp;q=use%20the%20Monte%20Carlo%20method%20to%20find%20the%20volume%20of%20an%20ice%20cream%20cone&amp;f=false" rel="nofollow noreferrer"><em>Cheney and Kincaid: Ice Cream Cone Problem</em></a>, a similar program is giving different results. Here is the program in Fortran (<a href="http://www.ma.utexas.edu/CNA/cheney-kincaid/f90code/CHP13/cone.f90" rel="nofollow noreferrer">http://www.ma.utexas.edu/CNA/cheney-kincaid/f90code/CHP13/cone.f90</a>)</p> <pre><code> vol3[num_] := Module[{hit, miss, index, x, y, z}, hit = 0; miss = 0; For[index = 1, index &lt;= num, ++index, {x, y} = RandomReal[{-1, 1}, 2]; z = RandomReal[{0, 2}]; If[x^2 + y^2 &lt;= z^2 &amp;&amp; x^2 + y^2 &lt;= z (2 - z), ++hit]]; 8 hit/num] Print["time and value...... :", Timing[N[vol3]]] </code></pre> https://mathematica.stackexchange.com/questions/69050/-/69054#69054 5 Answer by njpipeorgan for Monte Carlo Volume Calculation and Speed Up njpipeorgan https://mathematica.stackexchange.com/users/22599 2014-12-15T05:25:01Z 2014-12-15T05:34:07Z <p>For loop is always slow.</p> <p>You may try this:</p> <pre><code>f = Compile[{{x, _Real}, {y, _Real}}, If[y &gt;= 12. Cos[x] &amp;&amp; y &gt;= 10 + x^3, 1, 0]]; vol[n_Integer /; n &lt;= 10^6] := 3.* Total[f@@@Transpose@{RandomReal[{0, 1}, n], RandomReal[{10, 13}, n]}]/n; </code></pre> <p>The calculation of 1000000 samples takes 1.1 s on my i5-3210M.</p>