Quantcast
Channel: blog.dexy.it
Viewing all articles
Browse latest Browse all 14

Happy Pi Day

$
0
0

It was pretty late last night when I finished my post about blogging with WordPress and Dexy, so I didn’t even notice it had turned into π day. This morning of course twitter reminded me, and in particular this caught my eye:

Lots of people have done cool graphs using spreadsheets. Here’s a google spreadsheets version:

As Matt says, Excel is recommended here because it’s very easy to set up the formulas and graph them. It makes it very intuitive. But, I’m going to try this out using Python just because I feel like playing with some math today and spreadsheets are pretty awkward on my little netbook.

Here’s the formula, made a little easier to read thanks to LaTeX and MathJax:

$$\sqrt{6\left(1+\frac{1}{2^2}+\frac{1}{3^2}+\frac{1}{4^2}\dots\right)}$$

Here’s my first implementation:

>>> sum_fractions = 0.0
>>> values = []
>>> for i in range(1,20):
...     sum_fractions += 1.0/(i**2)
...     value = numpy.sqrt((6.0 * sum_fractions))
...     values.append(value)
...     print "After adding 1/%s^2 value is %0.10f" % (i, value)
...
After adding 1/1^2 value is 2.4494897428
After adding 1/2^2 value is 2.7386127875
After adding 1/3^2 value is 2.8577380332
After adding 1/4^2 value is 2.9226129861
After adding 1/5^2 value is 2.9633877010
After adding 1/6^2 value is 2.9913764947
After adding 1/7^2 value is 3.0117739478
After adding 1/8^2 value is 3.0272978567
After adding 1/9^2 value is 3.0395075896
After adding 1/10^2 value is 3.0493616360
After adding 1/11^2 value is 3.0574815067
After adding 1/12^2 value is 3.0642878178
After adding 1/13^2 value is 3.0700753719
After adding 1/14^2 value is 3.0750569156
After adding 1/15^2 value is 3.0793898260
After adding 1/16^2 value is 3.0831930203
After adding 1/17^2 value is 3.0865580258
After adding 1/18^2 value is 3.0895564350
After adding 1/19^2 value is 3.0922450523

We’ve stored the first 20 values in an array, so let’s graph them:

>>> pyplot.axhline(numpy.pi, linewidth=2, color='r')
<matplotlib.lines.Line2D object at 0xa2bb8ac>
>>> pyplot.plot(range(1,20), values, color='b')
[<matplotlib.lines.Line2D object at 0xa21938c>]

graph of approximation of pi using sum of inverse squares

Not bad, but we’ll want to go a little farther out. Also, a fun addition to this was also posted:

*Oops, @outofthenorm2 meant to say π/4

So, let’s try this again defining some functions:

>>> def sqrt_six_times_sum_inv_squares(n):
...     return numpy.sqrt(6.0*sum(1.0/k**2 for k in range(1,n)))
... 
>>> def four_times_sum_inv_odds(n):
...     return 4.0*sum(((-1.0)**(k+1))/(2*k-1) for k in range(1,n))
... 
>>> for i in range(1, 10):
...     value = sqrt_six_times_sum_inv_squares(i)
...     print "At step %s value is %0.10f" % (i, value)
...
At step 1 value is 0.0000000000
At step 2 value is 2.4494897428
At step 3 value is 2.7386127875
At step 4 value is 2.8577380332
At step 5 value is 2.9226129861
At step 6 value is 2.9633877010
At step 7 value is 2.9913764947
At step 8 value is 3.0117739478
At step 9 value is 3.0272978567
>>> for i in range(1, 10):
...     value = four_times_sum_inv_odds(i)
...     print "At step %s value is %0.10f" % (i, value)
...
At step 1 value is 0.0000000000
At step 2 value is 4.0000000000
At step 3 value is 2.6666666667
At step 4 value is 3.4666666667
At step 5 value is 2.8952380952
At step 6 value is 3.3396825397
At step 7 value is 2.9760461760
At step 8 value is 3.2837384837
At step 9 value is 3.0170718171

Now we can graph it:

>>> pyplot.clf()
>>> N = 30
>>> xs = range(1,N)
>>> pyplot.axhline(numpy.pi, linewidth=2, color='r')
<matplotlib.lines.Line2D object at 0xa38060c>
>>> pyplot.plot(xs, [sqrt_six_times_sum_inv_squares(i) for i in xs], color='b')
[<matplotlib.lines.Line2D object at 0xa1a92cc>]
>>> pyplot.plot(xs, [four_times_sum_inv_odds(i) for i in xs], color='g')
[<matplotlib.lines.Line2D object at 0xa38096c>]
>>> pyplot.axis([0, N, 2, 4])
[0, 30, 2, 4]

graph of approximation of pi using sum of inverse squares and Gregory-Leibniz series

There’s more information about approximating pi on this wikipedia page. You can get the source code for this blog post and play around with it on github.


Viewing all articles
Browse latest Browse all 14

Trending Articles