Estimating the area of a unit circle


How do we know that pi = 3.14159... ? There are many ways to estimate pi to varying degrees of accuracy. My favorite ways use probability. This page demonstrates one such way: 'throwing darts'.

The only mathematics that you need to remember is how to calculate the distance a point is from the origin. A point in 2-dimensional space (an "x-y plane") is denoted by (x,y). The origin is (0,0). The distance (x,y) is from (0,0) is found by applying the Pythagorean Theorem to get (x-0)2+(y-0)2 = r2. Because we have a unit circle, r = 1, and we get distance from origin = sqrt(x2 + y2). Then, just keep track of the number of times the point falls inside the circle, that is, when sqrt(x2+y2) < 1.

On this page I present an R script for calculating the area of a unit circle (ie. pi) by 'throwing' random points at a quarter of a circle. Using the ratio (#points in circle/#total points), we can estimate the area of the entire circle, pi, as pihat = 4*(#points in circle/#total points).

Here is my R script with comments

#to estimate the area of a unit circle using random numbers
#points are thrown randomly (uniformly) at the upper right quadrant of a unit circle
#pi is estimated as pihat = 4*(#points in circle/#total points)
vec<-vector(mode="numeric", length=n)
#calculates the distance a point is from the origin
for (i in 1:n) {
if (DistFromOrigin[i]<1) (a<-a+1)
#outputs the number of points and the estimate of pi
#graphs the quarter circle
#graphs the points
title("pi~4*(#points in circle/#total points)")

And here are some graphs this script produced for various n.

n=100, pihat=2.64

n = 1000, pihat = 3.108

n=10000, pihat=3.162

The others scripts I ran for n=100,000 and n=1,000,000, produced pihat=3.13944 and pihat=3.142488 respectively. I've omitted those graphs, as they were essentially all black.

If you enjoyed any of my content, please consider supporting it in a variety of ways: