Allen CDNE

Computational Design in Nuclear Engineering · Allen

Download the PDF

Computational Design in Nuclear Engineering, second edition, by Kenneth S. Allen, Ph.D., P.E.

Download the PDF

Chapter 4 · The Monte Carlo Method for Transport

Example Monte Carlo Calculation of π\pi

On this page

Chapter 4 · 2 / 54 min readPDF page 106

Worked examples

One of the earliest examples of the Monte Carlo process was performed by Georges-Louis Leclerc, Comte de Buffon in the 18th century. Buffon’s experiment was conducted by dropping a needle onto a surface randomly. The surface was bisected by a series of equally spaced parallel lines. The length of the needle was exactly one half the distance between the parallel lines. Through some math, he was able to determine a relationship to when the needle touched one of the lines to the ratio of the circumference of a circle to its diameter (i.e. π\pi ) (Dupree and Fraley; 2002)

Figure 4.3. A circle radius R just inside a square tangent to 4 points.
Figure 4.3. A circle radius R just inside a square tangent to 4 points.

Another method for estimating π\pi using a random process involves using square and a circle. Say we have a unit circle (radius = R), inside a square of sides 2R x 2R where the sides of the square are tangent to the circle as presented in Figure 4.3.

The area for the square is:

Equation 4.1
Asquare=side2=2R2=4R2A_{square} = side^2 = 2R^2 = 4R^2

The area for the circle is:

Equation 4.2
Acircle=πR2A_{circle} = \pi R^2

If we randomly sample points within the square, some of those points will fall within the circle. The number of points sampled will be proportional to the area. We can then set up a ratio where:

Equation 4.3
NcircleNsquare≅πR24R2=π4\frac{N_{circle}}{N_{square}}\cong \frac{\pi R^2}{4 R^2}=\frac{\pi}{4}

Solving for π\pi:

Equation 4.4
π≅4NcircleNsquare\pi \cong 4 \frac{N_{circle}}{N_{square}}

Because all four quadrants of the circle are the same, we can simplify the problem by just focusing on the positive (top-right) quadrant. If we plot the positive quadrant on x and y axis the result is the left side of Figure 4.4.

Figure 4.4. The positive quadrant of the circle in Figure 4.3 on the left. The same quadrant on the right with the radius set to 1 and a right triangle consisting of the x and y distances to the intersection of the arc and radius lines.
Figure 4.4. The positive quadrant of the circle in Figure 4.3 on the left. The same quadrant on the right with the radius set to 1 and a right triangle consisting of the x and y distances to the intersection of the arc and radius lines.

If we set the radius of the arc = 1, we see that, by the Pythagorean Theorem, the radius is given by:

Equation 4.5
R=1=x2+y2R = 1 = x^2 + y^2

We can then use a random sample of points with coordinates of x and y between 0 and 1 and evaluate if they are inside the circle if the following condition is true:

Equation 4.6
x2+y2<1x^2 + y^2 < 1

Now we can use Equations 4-4 and 4-6 to get a relationship.

Equation 4.7
π≅4Nx2+y2<1NTotal\pi \cong 4\frac{N_{x^2+y^2 <1}}{N_{Total}}

This relationship is an approximation because the area is being represented by points. The more samples taken, the closer the approximation will be. As with any random process, we must evaluate the precision of the result. We can do this by measuring the standard deviation of Equation (4-7). The standard deviation is:

Equation 4.8
σ=41NTotal∑i=1NTotal(Ni−πNTotal)2\sigma=4 \sqrt{\frac{1}{N_{Total}}\sum_{i=1}^{N_{Total}}\left(N_i - \frac{\pi}{N_{Total}} \right)^2}

Where NiN_i represents each individual calculation performed using Equation 4-6. This estimate for π\pi can be easily written into a Julia script that allows the user to vary the number of samples to get a value and a standard deviation. This script and the analysis of the results is presented in Example 4.1. (Dupree and Fraley; 2002)

Example 4.1

Write a Julia program that uses the positive quadrant for a unit circle inside a square with an area of 1 to estimate the value of π\pi. The script should calculate the estimate using random points selected from x = 0 to x = 1 and y = 0 to y =1. It should prompt the user for the number of samples and output the value for π\pi and the standard deviation of the measurement. Report the value and standard deviation for sample sizes from 1.0E4 to 1.0E10 and plot the results compared to the known value.

Solution: Open a new Jupyter Notebook with the Julia kernel. Use the procedures outlined in Chapter 2 to develop the following input:

Example 4.1, listing 1Example 4.1, listing 2

Note that the relationship x=rand() uses an internal Julia function to assign a random number between 0 and 1 to the x variable. The program will prompt for the number of trials and output the value of π\pi and its standard deviation. The results for the trials from 1.0E4 to 1.0E10 are as follows:

NumberEstimateStandard
of Trialsof π\piDeviation
1.00E+043.13121.6494E-02
1.00E+053.138725.1993E-03
1.00E+063.1400081.6433E-03
1.00E+073.1416925.1928E-04
1.00E+083.141834081.6420E-04
1.00E+093.1417278685.1927E-05
1.00E+103.1416051631.6422E-05

If we plot the results and compare them to the known value of π\pi and use the standard deviation as the error bars we get the following graph.

The results show how the number of trials increases the accuracy of the value and greatly reduces the error associated with the value. As the number of trials approaches 1.0E8, the error is so small, it cannot be seen beyond the size of the marker on the graph. Note that even at 1.0E4, the accepted value is within the error bar. It is also noted that the calculation time for 1.0E9 and 1.0E10 was significantly longer than for the lower order calculations.

Example 4.1, listing 3

Computational Design in Nuclear Engineering, second edition, by Kenneth S. Allen, Ph.D., P.E. — the complete book as a 394-page PDF.

Download the PDF