Wednesday, June 10, 2009

Pi

Pi I have been always excited about basic mathematical calculations implemented on computers. One of them is calculating PI. I’ve tried out an example algorithm from Roman E. Meader: Computer Science with Mathematica. You can see the results in the image on the left side of the text. We choose random points in a unit square and check how often it lies inside a unit circle as well.

  1. Choose random points
  2. Count the points in the circle (x^2+y^2 <=1)
  3. Compare this number to the total number of points (circle_points/number_of_points)
  4. You should get about PI/4 as a result

Why don’t you get PI as a result? Because you have just a quarter of a circle.
Why can we calculate PI this way? Because the area of a circle is PI*r^2. If r=1, the area of the square is PI.

See the image for Mathematica details.