Monte Carlo methods: Difference between revisions
m
→{{header|Mathematica}}
Drkameleon (talk | contribs) |
|||
Line 1,641:
3.1420188</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
We define a function with variable sample size:
▲ MonteCarloPi[samplesize_Integer] := N[4Mean[If[# > 1, 0, 1] & /@ Norm /@ RandomReal[1, {samplesize, 2}]]]
Example (samplesize=10,100,1000,....10000000):
<lang Mathematica>{#, MonteCarloPi[#]} & /@ (10^Range[1, 7]) // Grid</lang>
gives back:
<pre>10 3.2▼
▲10 3.2
100 3.16
1000 3.152
Line 1,658 ⟶ 1,653:
100000 3.14872
1000000 3.1408
10000000 3.14134</pre>
<lang Mathematica>monteCarloPi = 4. Mean[UnitStep[1 - Total[RandomReal[1, {2, #}]^2]]] &;
monteCarloPi /@ (10^Range@6)</lang>▼
▲monteCarloPi /@ (10^Range@6)
=={{header|MATLAB}}==
|