Dot Physics

On the 8th Day, God made Pi

Pi day is March 14th – get it? (3.14) I am a big fan of Pi. Here is my first post to celebrate the awesomeness of Pi (I know this is early, but I was too excited to wait).

How can you determine Pi?

Oh sure, tons of high schools do the classic experiment. Measure the circumference and diameter of as many round things as possible. Plot diameter vs. circumference. The slope will be Pi. Really, this is a great lab to do for all sorts of ages. The key thing is that students can see what Pi really means. I am not going to talk about this lab, I am going to do some thing cooler.

What if I had a 1 meter by 1 meter square taped out in the grass near the physics building and shot ping pong balls off the roof at this square? Ping pong balls are very difficult to aim, so you could easily imagine that they would fall in a random pattern in the square. I don’t really want to set this up, so I am going to do it with a program instead. Here is a program that will generate random points in a 1 x 1 square.

i-b83fb985b5204c51515b50bce1d62dd0-2010-03-09_randomplot_1.jpg

This is what the output would look like:

i-c3e55754caf8b27e5ae3390639178a25-2010-03-09_scatter_1png.jpg

Now, here is the key. What if I calculate the distance of each of these point from the origin? This would simply be:

i-afe34bada14b49c4955325e103680e2a-2010-03-09_la_te_xi_t_1_7.jpg

Doing this, I can separate all the points into those that are more than 1 unit away from the origin and those that are less than 1 unit away. In this plot, (of 1000 points) the red dots are more than one unit away and the blue are 1 unit or less.

i-4f5bb1e5b5123e8655150a0d929f64d2-2010-03-10_redbluedotspng.jpg

Note that this plot does not have exactly the same horizontal and vertical scale – no idea why it came out like that. However, this is enough dots that maybe you can see a pattern. The blue dots are filling up 1/4th of a circle. The full area of this circle would be:

i-04a3f03d461961a05810de76000828b9-2010-03-10_la_te_xi_t_1_8.jpg

Since I am a physicist, I have trouble leaving the area as unitless. What if I look at the ratio of blue dots to total dots? This should be the same as the ratio of the area of the quarter circle to the whole square, or:

i-aec70bf277e01a6002f8f19a3f32775b-2010-03-10_la_te_xi_t_1_9.jpg

Doing this for my last random number run, I get pi = 3.04. What happens as I increase the number of random dots? This a plot of the estimation of Pi for numbers of dots starting at 1000 up to 100,000 dots. I did this 5 times because each time is different.

i-b0d9c33928f8613b484e19b65c7bd0e8-2010-03-10_pimultirun_4png.jpg

So, you can see that as the number of random points increases, the estimated value of Pi is a little less spread out and closer to 3.14-something.

P.S. Thanks to Dave for this idea. You know who you are.

Update

I also made a Scratch program version of this calculation. (Scratch is a graphical programming language made at MIT – you know, for kids).

Comments

  1. #1 Nachiket Gokhale
    March 10, 2010

    IIRC this could be thought of as a Monte Carlo method for evaluating Pi.

  2. #2 Rhett Allain
    March 10, 2010

    @Nachiket,

    Yes – good point, I guess I should have mentioned that.

  3. #3 MattW
    March 10, 2010

    Also reminiscent of one of the first documented usages of a Monte Carlo method (from http://en.wikipedia.org/wiki/Asaph_Hall):

    On June 5, 1872 Hall submitted an article entitled “On an Experimental Determination of Pi” to the journal Messenger of Mathematics. The article appeared in the 1873 edition of the journal, volume 2, pages 113-114. In this article Hall reported the results of an experiment in random sampling that Hall had persuaded his friend, Captain O.C. Fox, to perform when Fox was recuperating from a wound received at the Second Battle of Bull Run. The experiment involved repetitively throwing at random a fine steel wire onto a plane wooden surface ruled with equidistant parallel lines. Pi was computed as 2ml/an where m is the number of trials, l is the length of the steel wire, a is the distance between parallel lines, and n was the number of intersections. This paper is a very early documented use of random sampling (which Nicholas Metropolis would name the Monte Carlo method during the Manhattan Project of World War II) in scientific inquiry.

  4. #4 Chris Granade
    March 10, 2010

    Dude! Someone else that uses matplotlib! Awesome!

  5. #5 Keith Harwood
    March 10, 2010

    But Hall cheated. He stopped when the result was very close to the known value of pi. One more trial and the result would have gone a long way from pi in one direction or another. While the procedure is valid the number of trials to be confident of getting a good result makes it infeasable.

  6. #6 ObSciGuy
    March 11, 2010

    Nice :)

    I decided to add a little more pie to your pi estimates. There is a screen shot and other details (including the R code) here: How to make pi… using R.

  7. #7 simon
    March 11, 2010

    Note that this plot does not have exactly the same horizontal and vertical scale – no idea why it came out like that.

    In order to square the axes, you need to insert the following code after you plot the points:
    ax = gca()
    ax.set_aspect(1.0 / ax.get_data_ratio())

  8. #8 Rhett Allain
    March 12, 2010

    @Simon

    Thanks! That worked.

  9. #9 Gonçalo
    March 15, 2010

    Hey Rhett. Super cool post.

    What about the rest of the code in Python. Can we see it?

  10. #10 Rhett Allain
    March 16, 2010

    @Goncalo,

    Here is the code (sloppy code warning).

    random_pi.py

  11. #11 Gonçalo
    March 16, 2010

    Thanks Rhett.

    Let’s have some fun with Pi. ;)