To study systematically how good an estimate is obtained in an
experiment with a given number of darts, ndarts, we can redo the experiment
several times (in each experiment using ndarts) and check the consistency of the
result. That is, we will carry out several experiments where pi is
determined by throwing darts ndarts times. This set of different,
independent estimates of pi is then analysed by calculating the spread
or variance in the values obtained. This gives an estimate of the
precision in an experiment with ndarts trials. This whole calculation
and analysis can then be repeated with a different value of ndarts
and the dependence of the precision on ndarts analysed (obviously the
experiment becomes more precise the more darts we use, but the question is
how fast does the precision improve).
To carry this out, it is
necessary to make it easy to do repeated experiments.
It is convenient to define a dart throw experiment as a callable Mathematica
module. The variables listed in the beginning of the module are local, i.e.
use of those variables outside the module does not affect their value inside
the module and vice versa. You can easily modify the five lines of code
you used before to make this dart throw experiment a self-contained module:
 | darts[ndarts_] := Module[
{xcoord,ycoord,distance2,nhits},
xcoord := Table[Random[]-0.5, {i,1,ndarts}];
ycoord := Table[Random[]-0.5, {i,1,ndarts}];
distance2 = xcoord^2 + ycoord^2 ;
nhits = Count[distance2, x_ /; x <= 0.25];
piest = 4.0*nhits/ndarts
] |
Remember to include the last closing square bracket. It completes the
module definition.
An experiment with, for example, 100 darts
can now be carried out by simply typing darts[100].
Do a couple of experiments throwing 100 darts, for example.
Do you get the same estimate of pi?
Hannes Jonsson
Modified by Thomas L. Marchioro II
and the Undergraduate Computational Engineering and Science project