Saturday, March 14, 2015

5 Methods for Approximating Pi

Animated pi symbol
Happy super-π day! π-day falls on March 14 every year, because the month-day combination results in 3-14, which are the first 3 digits of π. But this π-day is particularly special: include the year, and you get 3-14-15, the first 5 digits of π. We won't get another π-day like this for another 100 years, so you better enjoy this one!

A particularly special time will be at 9:26:53 PM tonight, when we'll get 3-14-15 9:26:53, or the first 10 digits of π.

In honor of the occasion, I'm going to post 5 simple methods for approximating the value of π.

1. String and Tape Measure

Draw a circle with a diameter of 1 foot and put a piece of string around the edge. Cut the string until it's the right length, and then measure it with a tape measure. The string should be about π feet long.

This can be implemented more accurately using a computer program: take the arc length of √(1 - x2) on the interval (-1, 1). The result should be π.

2. Monte Carlo

Using the Monte Carlo method, the value for π can be approximated by drawing a square with a circle fitting perfectly inside it, and throwing rice high over the square. Let a equal the total number of grains that fall inside the square, and let b equal the number of grains that fall inside the circle. Assume the circle's radius is 1 unit, so the area of the circle is π square units. The square's area is 4 square units. On average, b / a = π / 4, so π = b * 4 / a.

This can also be implemented using a computer program. Pick random points with x-values between -1 and 1 and y-values between -1 and 1. Count the number of points within a distance of 1 from the origin, and proceed with the math as before.

3. cos(π/2) = 0

When most people talk about an angle, they use degrees - as in 90° (a right angle), 180° (a U-turn), and 360° (1 full turn, or very dizzy). But mathematicians use radians, which involves looking at the circumference of a unit circle, so instead of a full circle being 360°, it's equal to 2π (the circumference of a unit circle). A U-turn, or 180°, is equal to π, and a right-angle (90°) is equal to π/2.

Using radians, cosine of π/2 is equal to 0. So to figure out what π is, just plug some angles in and see what works. When you get an angle that works, multiply by 2 and you have π. (For my post about sine and cosine, click here.)

If you use an angle that is too big, the cosine is negative; if use an angle that's too small, cosine is positive. So, instead of making random guesses, you can actually improve your guesses over time.

In fact, because cosine is negative when the angle is too big, and cosine is positive when the angle is too small, the following method that I discovered works really well (where θ is the angle being tested):
A. Start with θ = 0
B. Add cos(θ) to θ
C. Go back to step B

After repeating those steps only 4 times, and multiplying your angle by 2, you'll get a surprisingly good approximation of π.

Alternatively, if you know the Taylor series for arcsin(x), you could use that to find the value for arcsin(1) and double it to get π.

4. Integral

We want to find the area of a circle, right? So why not just, well, find the area?

The formula for the top half of a circle is √(1 - x2). If we take the integral of that function from -1 to 1, and multiply by 2, then bingo! We get pi. (For those who haven't reached calculus, the integral is the area under the curve.) To do this, a numerical approximation works well: start with x = -1, and gradually increase x until it reaches 1; meanwhile, evaluate √(1 - x2) at each point, multiply by the step size, multiply by two, and add the values all together. (You can also use the indefinite integral, but that involves arcsin(x), which would require a Taylor series to solve.)

This technique works best when implemented using a computer program. In JavaScript, you would use some code like this (try changing "step" to something smaller than 0.1!):

5. Many-sided polygon

A circle is like a polygon with tons of sides, right? So if you can find the area of a 1000-sided polygon, that should be pretty close to π. Now how might we do this?

Suppose we have an n-sided polygon. The angle of a single sector of the polygon is equal to 360°/n degrees. Divide the sector into 2 right-angled triangles, and each triangle has an angle of 180°/n degrees. Now assume that the base of each triangle has a length of 1. We can get the area of each triangle by taking 1*tan(180°/n)/2. Since we divided each sector into 2 parts, we multiply by 2 to get the area of the sector: tan(180°/n). Multiply by the number of sectors to get n*tan(180°/n) for the area of the polygon.

But tan(θ) = sin(θ) as θ approaches zero, so the area of an n-sided polygon is equal to n*sin(180°/n).

Now, when x is very large, we can get a reasonable approximation for π. There's one problem, though. When a computer calculates sin(θ) in degrees, it converts to radians using a built-in value of π. In other words, our formula relies on somebody else's value of π in order to compute its own value of π. Which is stupid.

However, we can use geometry to prove that the value of sin(45°) = cos(45°) = 1/(√2). Based on that, we can use the half-angle formulas for sin and cos to compute values of sin(45/2°), sin((45/2)/2°), and smaller angles. To make sure our angles are half values of 45°, we can only work with polygons that have 8*2m sides (with m an integer).

I'll leave it at that.

New posts every month - subscribe for free!

No comments:

Post a Comment