This Pi Day, Calculate the Value of Pi for Yourself
It is once again Pi Day (March 14—which is like the first digits of pi: 3 and 14). Before getting into this year's celebration of pi, let me just summarize some of the most important things about this awesome number.
- Outside the US, Pi Day should probably be July 22 (22/7)—this fraction is a surprisingly good estimate of pi.
- You can find the value of pi with a mass and a spring.
- The value of pi is related to the local gravitational field.
- You can find the value of pi using random numbers (this one is my favorite).
- And finally—there is a relationship between pi, e, 1, 0 and i (the imaginary number).
But today, I am going to calculate pi with a numerical integral. What does that even mean? Let me start with an example—how do you find the area of a half-circle?
The area of a circle is pi times the radius squared. This is half of a circle with a radius of 1 (no units) such that it would have an area of pi/2. If I find the area with some other method, I can just multiply this area by 2 and get pi. That's the plan.
But how do you find the area of some shape—or any shape for that matter? This is where calculus comes in handy. I can find the area of the half circle by adding up the area of a bunch of rectangles. It turns out that it's pretty easy to find the area of a rectangle. Let me just draw a few rectangles in that half-circle so you can see what I mean.
The area of each of these skinny rectangles can be found with the formula "base times height." A rectangle has a height of "y" and a base of "dx" where the dx is just some arbitrary length along the x-axis. I can find the actual value of the height because the top of the rectangle hits the circle where this height can be found from the equation of a circle.
Now I just need to add up all these rectangles—boom, that's the area of half a circle. I can write this as a sum of areas like this:
But wait! Isn't this a poor approximation to the actual area of a circle (half-circle)? Yes, that is indeed true—but it really depends on the width of these tiny area rectangles. In fact, if I take the limit as the width (dx) goes to zero then I will get the exact area. This is actually the definition of the integral in calculus—but I will save that for another day. Instead we will do a numerical calculation by simply adding up the area of a bunch of rectangles. You could of course do this by hand—but it might get boring. Instead, let's do this with a computer program. Yup.
Here is numerical calculation in python. You can go ahead and run the code by pressing the "play" button, but I will give some code comments below.
You can change the code if it makes you happy—here are a few things to consider.
- This is a numerical calculation. That means the program only deals with numbers. Technically, the area should have units of m2 or something like that but not here. Only numbers.
- For loops in python, it includes everything that is tab-indented as part of the loop. Once you dedent, it's no longer in a loop.
- Line 18 should look weird because it is. If you consider this to be an algebraic equation, the A should cancel since it is on both sides of the equation—but this is not an equation. In python (and most other languages), the "=" means "make equal to". This line takes the old value of A, adds the new stuff and then makes it the new value of A.
This initial calculation has a dx of 0.1. That means there will be just 20 rectangles to add up and get the area of the half-circle. With this, I get an approximate pi value of 3.10452—which is clearly not exact pi. Of course I can make a better estimate by making smaller width rectangles. You should try this by changing the code above (hint: change the value for dx). However, since I can't let this go here is a plot of the value of pi for different step sizes.
Perhaps that's not the best plot—but it's good enough for now. If you want to check out the code for this plot, here you go. But in the end, the value does approach the expected value of pi. This method might not get you one million digits of pi, but maybe you at least can learn something about integration.
The Fascinating Math Behind Why You Won't Win The Powerball
The Powerball jackpot is over a billion dollars but what are your chances?