You are on page 1of 12

MEC3101: Numerical & Analytical Methods B

Project 1: Numerical Method Programming (Python – Monte Carlo)

Project 1: Numerical Method


Programming (Python - Monte Carlo)
MEC 3101: Numerical & Analytical Methods B

For:
Dr Edmund Liew

Date of Submission: 12th December 2014


Surianah Binte Sulaiman Sujai B30659819
Nadeen Abu Musawwir B30659864
Kok Wai Geng B30659831
Chor Kangwei Philemon B30659130
Izwan Ismail B30659314

1
MEC3101: Numerical & Analytical Methods B
Project 1: Numerical Method Programming (Python – Monte Carlo)

Abstract
This report explains the calculation of PI historically, with the support of mathematical
software known as Python. PI will be initially estimated using Taylor’s Series based on
Machin’s formula followed by an approximation based on Monte-Carlo technique which
calculates an area of a quarter circles. Lastly, comparing the results using the Python
software, this will proves the validity of data obtained.

3
MEC3101: Numerical & Analytical Methods B
Project 1: Numerical Method Programming (Python – Monte Carlo)

Contents
Abstract..................................................................................................................................................i
1. Introduction...................................................................................................................................1
2. Manual Calculations (Machin’s Formula and Taylor’s Series)......................................................1
3. Python code and results (Machin’s Formula and Taylor’s Series)..................................................2
4. Monte Carlo calculations and Simulations.....................................................................................3
5. Discussion.....................................................................................................................................4
6. Conclusion.....................................................................................................................................5
7. Acknowledgement.........................................................................................................................6
References.............................................................................................................................................6
Appendix...............................................................................................................................................7
Appendix A: Calculating Individual n value with π as the subject..................................................7
Appendix B: Flowchart of Python Programming............................................................................8
Appendix C: Comparison of Pi results with different terms...........................................................8
Appendix D: Error versus Number of Samples...............................................................................9

3
MEC3101: Numerical & Analytical Methods B
Project 1: Numerical Method Programming (Python – Monte Carlo)

1. Introduction
The fascinating history of ‘’ parallels virtually throughout the entire millennia of
mathematics world. Through this history, it had provides advancement of science and
technology and of mathematical computations and programming. A ‘’ is a number which
arises in many mathematical computations involving circles. The most basic properties of ‘’
were understood during the Greek period (est.250BC) by Archimedes of Syracuse. He uses a
geometrical scheme based on inscribed and circumscribed polygons to obtain the initial value
of PI [CITATION Dav96 \l 1033 ]. Since then, the search and improvements of infinite values of
PI had tremendously improved. Some of the well-known mathematician who discovered the
values of ‘’are Euler, Machin and Gregory in the 17th and the 18th century.
PI, ‘’, are widely used in computing system and in some new techniques for performing the
Fast Fourier transform (FFT) which is heavily used in modern science and engineering
computing. Since Pi is the ratio of a circle's circumference to its diameter, it occurs wherever
periodic or trigonometric structures are studied, as well as spherical structures which occur
such as contour integration, measure theory (thermodynamics, analysis, probability and
statistics), and differential geometry. It is thus also a natural way of measuring angles.
There are many mathematical approaches in obtaining PI. Some of the known formulas are as
follow [CITATION Bor00 \l 1033 ]:
- Archimedes (250BC)
- Francois Viete (1579)
- John Wallis (1650)
- William Brouncker (1650)
- Madhava , James Gregory, Gotfield Wilhelm Leibneitz (1450-1671)
- Issac Newton
- Machin Type Formula (1706-1776)
- Leonard Euler (1748)
- Srinivasa Ramanujan (1914)
- Louis Comtet (1974)
- Eugene Salamin, Richard Brent (1976)
- Jonathan Borwein and Peter Borwein (1991)
- David Chudnovsky and Gregory Chudnovsky (1989)
- David Bailey, Peter Borwein and Simon Plouffe (1996)

2. Manual Calculations (Machin’s Formula and Taylor’s Series)


Machin’s Formula and making π the subject [ CITATION Mat14 \l 1033 ]:
π 1 1
=4 arctan −arctan
4 5 239
1 1
π=16 arctan −4 arctan
5 239
Taylor Series [ CITATION Hav14 \l 1033 ]:

x2 n +1
n x3 x5 x7
arctan x=∑ (−1) ∙ = x− + − + …
n=0 2n+ 1 3 5 7

1
When ¿ ;
5
3 5 7
arctan
1 1 1 1
()
¿ − ×
5 5 3 5
+
1 1
[ ( )] [ ( )] [ ( )]
×
5 5

1 1
×
7 5
arctan ( 15 )=0.1973971599
1
When ¿ ;
239
3 5 7
1 1 1 1 1 1 1 1 1
arctan ( )
¿ − ×
239 239 3 239 [ ( )] [ ( )] [ ( )] ( )
+ ×
5 239
− ×
7 239
arctan
239
=4.184076002e−3

Substitute the calculated values into Machin’s formula:


1 1
π=16 arctan −4 arctan π=16 ( 0.1973971599 ) −4 ( 4.184076002 e−3 ) π=3.141591773
5 239
The values obtained through hand calculation is up to 9 decimal places due to the logarithm
pre-built in the calculator and its finite precision in computation. Its accuracy and precision in
calculating the exact value of Pi is affected. Each scientific calculator may not return the
exact results as produced above. A similar approach is shown in Appendix A where the n
values are calculated individually to obtain Pi.

3. Python code and results (Machin’s Formula and Taylor’s Series)

Figure 1: Screenshot of Codes and Results with n = 3

The code produced in Figure 1 can be depicted in the flowchart as shown in Appendix B. The
x1 and x2 value is assigned with its respective value as stated in the Machin’s formula. The
program will prompt the user for an n value upon running. In the computation, the n-term was
assigned with a value of 3 and a total of 9 decimal places was set to ensure a fair comparison
with the hand calculated Pi. The truncation error observed between the two values is 1 x 10-9.
When the error is calculated at a higher significant figures, the value obtained is much more
precise. The errors obtain also differs with an increase in n-term. A higher n-term, which has
more iterations, produces a better accuracy in results as shown in Appendix C. Results can
further be improved by decreasing the step size which leads to a decreased error. Though this
is feasible, it may reach a round off threshold [CITATION EKi07 \l 1033 ].

4. Monte Carlo calculations and Simulations


Monte Carlo method is a mathematical model that depends on chances of having repeated
random numbers in a loop as well as the ranges specified to find a solution to the problem.
This method is very useful because it is one of the most realistic ways of explaining
uncertainty in variables which are too complex to solve. Monte Carlo method solves this
problem by selecting random points within the square and to find out the points that will fall
in the quarter circle.

Figure 2: Quadrant within a Square

1 2
πr
˚¿ 4
Areaof quarter = 2 ¿
Areaof square r
2
By cancelling r ,
Estimated π=4 x Area of quarter ¿˚ ¿
Area of square
This equation will be formed in python as shown in Figure 3.
Using Pythagorean Theorem,
x 2+ y 2=r 2
r = radius of the quarter circle = 1
Pythagorean Theorem is applied to find out whether the points fall in the quarter circle or out
of it.
if x 2 + y 2< 1
Therefore, if the length of r, value is less than 1, the point falls in the quarter circle. Hence,
otherwise it falls out of the quarter circle.
Figure 3: A Python program was implemented of a Monte Carlo algorithm for estimating the value of 𝜋

Figure 4: Results shown at a sample size of 5,000,000

The python program in Figure 3 will run 500 times from the range of 10,000 to 5,000,000
points at a constant step size of 10,000. A constant step size of 10,000 was selected because it
completes its computation in thirty minutes without having memory error. The
random.random() function which has a value of 0 to 1 by default in python will be randomly
generated to the (x, y) coordinates. By running this process, the estimated 𝜋 and the error of 𝜋
are shown in Figure 4.

There are a total of 500 samples shown in Appendix D. As the number of sample increases,
the error deviation decreases and eventually, it reaches almost close to 0. This shows that as
more points are being generated, the estimated 𝜋 gets closer to the actual 𝜋 that will result in
having the smallest error. In conclusion, as the step size decreases, the better the accuracy by
increasing the number of random sample points.

5. Discussion
As there are numerous methods in obtaining 𝜋, the accuracy of each method will results in a
big difference between one another. As demonstrated in this report, 2 relatively simple
methods in obtaining the value of 𝜋 had been done. It begins with a manual calculation using
the Machin and Taylor’s series equations which results were later confirmed through the use
of Python Software. To compare the accuracy of this method, Monte Carlo programming
method is simulated to get an estimated 𝜋 at 5 million random samples. This test was
conducted to obtain the optimum result of all iterations for a smallest error.

Figure 5: Optimum Result of all Iterations

At the end of the program as shown in Appendix D and Figure 5, it will store these results as
the best results if (1>error) and compute the estimated best 𝜋, smallest absolute error, exact
value of 𝜋, best sample size and the number of samples that falls in the circle for the best
sample size. The Monte Carlo method is able to have an estimated 𝜋 with an error of
0.0000016880402912 which is lesser than 1E-5. However it does not compute the same error
every single time because it is based on random sampling of points.

6. Conclusion
A summary of the results obtained are shown below.
Machin and Taylor’s Series (nth term = 3): 3.141591773
Monte Carlo (5 Million samples): 3.141593822
Actual 𝜋 value: 3.141592653
Comparing the results obtained, it can be concluded that using Machin and Taylor’s Series
formula is equally accurate with Monte Carlo’s method of obtaining 𝜋. Both methods
managed to obtain the value of 𝜋 to an accuracy of up to its 5 th decimal place though Monte
Carlo shows a closer range of result than Machin and Taylor’s Series results.

The Monte Carlo simulation uses random sample points to estimate the value of 𝜋 using the
area of a square and an area of a quadrant inside the square. The accuracy of the results in
terms of decimal places will increase when the number of samples used is bigger. In addition,
several tests have to be done to just to obtain the average result as each test will give a
different result. This will be a disadvantage because as the samples and number of test gets
bigger, the time it consumes to simulate the result will be longer and hence causing
inefficiency of the system.
Yet, if liberty is given to do an additional Monte Carlo test with a higher number of samples,
a better result could be achieved where the estimated π value obtained will be much better
than that of the Taylor’s and Machin method.
The Machin’s and Taylor’s Series formula is a simple way of computing π. With a higher n th
term, a better accuracy value of 𝜋 can be obtained. Yet just like Monte Carlo, the
disadvantages is that it will require a larger number of terms to achieve a better estimated
accurate 𝜋 value that will take a longer time to compute. This shows that both methods of
accuracy are determined by the number of samples used.
Therefore, it can be concluded that both method has its own advantages and disadvantages
and further deeper studies can be done to learn the secrets of obtaining a more accurate
results of π [ CITATION Gio11 \l 1033 ].
7. Acknowledgement
 Dr Edmund Liew - For assisting us with our queries and clarifying our doubts.

Each Member Roles and Contributions

Member’s names Contributions


S.S , N.M , W.G. , P.C , I.I Background Research Study
S.S , N.M , W.G. , P.C , I.I Manual Calculations
S.S , N.M , W.G. , P.C , I.I Python’s software programming
S.S , N.M , W.G. , P.C , I.I Discussion & Conclusion of report
S.S , N.M , W.G. , P.C , I.I Approved Final Version of Report

References
Borwein, P., 2000. The Amazing Number Pi, Burnaby: Department of Mathematics and Statistics,
Simon Fraser University.

D. H. Bailey, J. M. B. P. B. B. S. P., 1996. The Quest for Pi, Burnaby: NASA Ames Research Center.

Giovannis, J., 2011. Calculating π. [Online]


Available at: http://www.johngiovannis.com/content/calculating-pi
[Accessed 10 December 2014].

Haverford, 2014. Taylor Series Expansions and Approximations. [Online]


Available at: http://www.haverford.edu/physics/MathAppendices/Taylor_Series.pdf
[Accessed 12 12 2014].

Kirr, E., 2007. Errors in Numerical Methods, Illinois: University of Illinois at Urbana-Campaign.

MathBrown, 2014. Machin’s Method of Computing the Digits of π. [Online]


Available at: http://www.math.brown.edu/~res/M10/machin.pdf
[Accessed 9 12 2914].
Appendix
Appendix A: Calculating Individual n value with π as the subject
n=0;

(−1)n 2n +1 (−1)0 2 (o )+1 0 1


arctan x= ∙x = ∙x = ∙x
2 n+1 2(0)+1 1

so:

1 1
π=16 ( )−4 =3.183263598
5 239

n=1;

(−1)n 2n +1 (−1)1 2 (1)+1 −1 3


arctan x= ∙x = ∙x = ∙x
2 n+1 2(1)+1 3

so:
3 3
1 1
π=
−1
3 [ ( )] [ ( )]
∙ 16
5

−1
3
∙4
239
=−0.042666569

n=2;

(−1)n 2n +1 (−1)2 2 (2) +1 1 5


arctan x= ∙x = ∙x = ∙x
2 n+1 2(2)+1 5

so:
5 5
1 1 1 1
π= ∙ 16
5 [ ( )] [ ( )]
5

5
∙4
239
=1.023999999 x 10−3

n=3;
n 3
(−1) 2n +1 (−1) −1 7
arctan x= ∙x = ∙ x 2 (3 ) +1= ∙x
2 n+1 2(3)+ 1 7

so:
7 7
π −1 1 1 1
4
=
7
∙ 16[ ( )] [ ( )]
5

7
∙4
239
=−2.925714286 x 10−5

Summation of n=0 to n=3;

π=3.183263598+ (−0.042666569 )+1.023999999 x 10−3+ 2.925714286 x 10−5 π=3.141591772


Appendix B: Flowchart of Python Programming

Appendix C: Comparison of Pi results with different terms


Appendix D: Error versus Number of Samples

You might also like