Friday, March 13, 2015

Cooking Up Pi

Happy Pi Day! One day out of the year we math geeks get to show off some Pi-related trivia we've dug up. Personally I love the artsy, the musical and even the food-related Pi-ponderings out there. But I'm a "Pi-thon guy" and I'm interested in the techy aspects of Pi Day.

Everybody knows Pi is the ratio of the circumference of a circle to its diameter. The Greeks were surprised that a nice perfect 1-unit wide wheel didn't roll out a beautiful whole number in one rotation. It always rolled just over 3 units. 3 and a half? Not even close. 3 and a quarter? Not quite. 3 and an eighth? Pretty close. 3 and a seventh is a good approximation, and in textbooks for centuries 22/7 stood in as the simple version of pi.

2,000 years ago Archimedes used the ratio of the perimeter of a 96-sided polygon to its diameter to get a really accurate approximation: between 3 + 10/71 and 3 + 10/70. Around 3.14185

About 600 years later in China, Zu Chongzhi used a 12,288-sided polygon (!) to improve the approximation to 3.1415929. How good is this approximation for all practical purposes? Well, the orbit of Jupiter is pretty close to a circle with a radius of 483,800,000 miles. So the 3 ten-millionths of error in Zu's value of pi corresponds to about 240 miles in arc of the orbit of Jupiter.

But at this very second a bunch of supercomputers somewhere are cranking out millions and millions more digits of pi. How do we get so precise? A history lesson is in order.

A few years ago some lucky homeschoolers attended my Math Through Technology course and they learned how Issac Newton extended Pascal's Triangle backwards (!) to get a formula for
(a + b)n

when n is negative. It was an infinite series that worked for numbers less than 1:

1/(1 + x) = 1 - x + x2 - x3 + ...

When the methods of Calculus got out, mathematicians found a formula for the derivative of the arctangent of a number:

(d/dx) atan(x) = 1/(1 +x2)

Looks similar! We can just replace x with x2 , integrate the terms one by one and we have an infinite series for arctangent:

(d/dx) atan(x) = 1/(1 +x2)

(d/dx) atan(x) = 1 - x2 + x4 - x6 + ...


If you've ever gotten up to Precalculus or Trigonometry you might wonder why all of a sudden they stop using degrees and start using radians. It's actually supposed to make finding arc lengths easier, but it can be confusing to see all those pi's. For example, instead of 45 degrees, you say "pi/4". The tangent of 45 degrees, I mean pi/4, is 1, so the arctangent of 1 is pi/4. The European mathematicians thought they were so bright in changing their series to:


Just add up a few fractions, multiply by 4 and we clever Europeans have pi to as many places as we want! Two problems: this formula was already well known to the folks in India (perhaps the Jesuits brought it back?) and it converged so slowly that it was almost useless. I illustrate this in my book Hacking Math Class Using Python. You can write a Python program to use a certain number of terms to calculate pi using the series:



Only 2 correct decimal places after 1,000 terms? A better series was needed. In the 1700s John Machin was playing around with tangent formulas. He started with the 45-degree right triangle. The tangent here is 1.




Machin inserted 4 triangles with a tangent ratio of 1/5 and nearly got to pi/4:




It was just a tiny bit off. Some tangent formulas made it easy to calculate just how far off.




Machin's improvement on the pi formula is as follows:


In Hacking Math Class I show you how to adapt the Python code above to return the arctan and then call it from a "machin" function.

def arctan(x,depth):
    x = float(x)
    runningSum = 0
    for i in range(depth):
        term = (-1)**i*(x**(2*i+1)/(2*i+1))
        runningSum += term
    return runningSum

def machin(depth):
    return 4 *( 4*arctan(1./5.,depth)- arctan(1./239.,depth))

Now using only 10 terms of the series (not an unreasonable amount of effort for an 18th century mathematician), we get 10 correct digits of pi!


Of course in the centuries since Machin there have been many more series found to approximate pi. Check them out on Wolfram Mathworld and code them in Python!

No comments:

Post a Comment