h1

Calculating Pi for fun and profit

May 19, 2007

This one is for my grandchildren :P. I found a nice intuitive way of calculating Pi. The only thing you need to understand is the Pythagoras Theorem. Consider the figure below:

math.jpg

To calculate the area of the circle, we take small steps.

  • First we calculate the area of the grey part. It’s simple to see that this area is 0.5(1×1).
  • Then the area of the red part. This is a little more tricky. To calculate the area, we first want to know the length of S. We see that the length from A to B equals 2S. With the use of Pythagoras, we find that 2S equals the square root of 2. So we know S: 0.5*sqrt(2). If we would also know T (and hence 1-T), then we could find the red area. But T is equal to S. A different way of finding T, is using Pythagoras again; we know OA=1, and we know S, hence we know T: the square root of 0.5. Thus the area of the red part equals: 0.5*0.5*sqrt(2)*(1-sqrt(0.5))=0.104.
  • Now the green area. Well, we know (1-T) and S. So we know V. And W is 0.5*V. Now we only need to know the small side of the green triangle. This is where it gets tricky. (And messy). If we know O-fi then we’re all set. But OA=1, and we know Afi=W! Using Pythagoras (remember him?) we find filambda=1-fiO=1-sqrt(OA^2-Afi^2)=0.0439. So the area is approximately 0.0439*W=0.0439*0.29289=0.01286.

Note: wordpress doesn’t allow Greek symbols. So that explains “fi” and “lambda”
The only thing that needs to be mentioned, is that we have to multiply the grey are with 4 for a complete circle (we only considered 1 quadrant), the red area with 8, the green area with 16, etc.

Of course it’s possible to make a small script for this. I did all the fun for you, and coded it in Matlab. The code is a mess, but it’s posted here as a reference. If you understood the above (or if you came to this point without weeping), the code shouldn’t be hard to follow.

The real Pi is 3.1415926535897932384626433832795
# of iterations             found value

1                                    2.0000000000000000000000000000000
2                                    2.4142135623730949234300169337075
3                                   3.0614674589207182551770244842800
4                                   3.1214451522580524351896677430859
5                                   3.1365484905459392823695871570607
10                                 3.1415877252771597505203060598746
25                                 3.1415926535897902256560013790759
30                                 3.1415926535897951086718985911613
50                                 3.1415926535897951086718985911613

This means there’s a difference of 1.7764e-015=0.0000000000000017764, or a percentage of 0.5953e-13%=0.0000000000005953%. Not bad.

After about 30 iterations, we don’t see any change in the calculated Pi anymore. This is due to my coding skills, and the precision of the script. But I think it’s clear it does what it needs to do; the method works.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: