Calculating the value of \pi

As part of a course on computational physics, one of the questions that might appear on our final paper is regarding the estimation of \pi using monte carlo methods. It was puzzling when I read this question at first.

The method is pretty straight forward. Take a square of side 'a' in which we inscribe a quadrant of a circle of the same radius 'a'. The ratio of the area of the quadrant to that of the square is \pi/4. Now, if we choose a point at random within the square, the probability of that point lying within the quadrant of the circle is equal to the ratio of the areas which is \pi/4.

Looking at this problem computationally now, let's discretize the square into unitary squares, denoted by the x,y coordinates of their center. Now, let's choose x and y values within the square i.e < 'a'. If the value x**2 + y**2 is less than or equal to the value of 'a' i.e it lies within the circle, we raise the value of a counter . If it doesn't, well drat! We keep track of the total number of such iterations. The ratio of the value of the counter to that of the number of iterations is now the value of \pi.

If one tries implementing this code, it will become obvious that the larger the value of 'a' within which we x and y, the better the estimate of \pi is. Or the larger the number of values x and y can be chosen from, the better our estimates. This is inherent to the fact that we are sampling the space within the quadrant or the square better. I'm speculating here since I haven't read up enough about this but I think the reason the error decreases because the ratio of the number of unit squares that lie exactly at a distance of 'a' reduces in comparison to the total number of unit squares in the square! These unit squares that lie at a distance of 'a' are the problem as part of their area lies within the quadrant and part lies outside! I will implement this and get back to you regarding how fast the value converges to \pi, the error and it's dependence on the value of 'a' and how the estimate grows towards the value of \pi over the course of multiple iterations. Hint : the initial estimate of \pi will either be zero or 4. 

Popular posts from this blog

Animation using GNUPlot

Pandas download statistics, PyPI and Google BigQuery - Daily downloads and downloads by latest version

Adaptive step size Runge-Kutta method