The π Project

Image by Gerd Altmann from Pixabay

It all started with me doing long division, and Mama asked me to calculate 22/7, which is the commonly used approximation for π. So I did, and here is what I got:

Calculating π using long division

Then I looked up the known value of π on google and found that it was 3.14159265359, and I wasn’t happy with the approximation. So I asked Mama how to calculate π more accurately.

I was learning programming in python with Papa. So she suggested I write a program that calculates π.

π is a number that can be calculated using the Monte Carlo method. Now I will demonstrate.

  1. We will begin with drawing a circle.
  2. Then we have to take this circle and draw a square around it.
  3. Now find the area of the circle of radius.
  4. It is π r2.
  5. Now find the area of the square.
  6. It should be 4 r2.
  7. Now imagine that you randomly drop a few quadrillion grains of sand on the circle and the square.
  8. What are the chances that the sand particles fell in the circle.
  9. Give each grain an address.
  10. Now make two columns one with grains in the circle and one with grains that are not in the circle.
  11. Next divide the grains inside by the total number of grains.
  12. Then the answer is π/4.
  13. Lastly, to get π, we multiply the fraction, four times.

Now I will explain how to check if a point lies inside or outside the circle.

We draw a unit circle on an x y plane so that the center of the circle is at the origin of the plane. Next, we draw a square around the circle as shown in the figure below.

Unit circle in a square

Every point plane can be marked exactly with the two known coordinates of the xy plane. For example, the square’s corners each are at the points:

Top right:(1,1)

Top left:(-1,1)

Bottom right:(1, -1)

Bottom left:(-1,-1)

The unit circle touches the square at 4 points namely: (-1,1), (1,0), (0,1), (-1,-1)

We find the radius of the circle. We figure the answer out by x2 + y2 = r. And since r is is one r2 is also one.

The area of the unit circle is π cm2.

Next we have to find the area of the square. So we have to multiply 2 cm2 by 2 cm2 . So when these two numbers are multiplied they equal 4cm2.

After that, we consider dropping particles in the square. Then we find where in the square the particles are by noting the coordinates. For them to be in the square the x and y coordinates of the points have to anywhere between -1 and 1.

But for the circle there is yet another condition, x2 + y2 <=1.

Going back to points 11,12 and 13 we calculate π.

With the help of my father and mother, I wrote a short program in python 3 that calculates π using this method. Here is the code.

from random import uniform
import sys
for x in range (10, 90, 5):
    circle = 0
    j = int(10 ** (x / 10))
    for i in range(j):
        x = uniform(-1, 1)
        y = uniform(-1,1)
        if x*x + y*y <= 1:
            circle += 1

    print(f'{j} {4*circle/i}')
    sys.stdout.flush()

I ran the program with increasing total number of points in powers of ten. And I saw the greater total number of points the more accurate your answer is when compared to the known value of π.

Now I will show you a plot chart on how close the program I wrote got to the actual value of π in each power of ten with numbers in between from 1000 to 10000000.

Pi value calculated by Monte Carlo method. X axis corresponds to number of points used in Monte Carlo calculation and Y axis shows value of pi obtained. Green line is known value of pi.

This is a fun video, where pi is calculated using the Monte Carlo method but instead of using a computer program to figure out where the random points go, Physics Girl uses darts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s