Approximating Pi

About the number $\pi$

The number $\pi$ represents the ratio of a circle’s circumstance to its diameter:

$$\pi = \frac{\mathrm{circumference}}{\mathrm{diameter}}\approx 3.1415926.$$

It’s an irrational number, meaning that it cannot be written as a ratio of two whole numbers. Thus, either a circle’s circumference or its diameter cannot be either.

Because of its relationship to the circle, the number $\pi$ has a deep relationship with angles. In particular, the sum of all angles in any triangle adds up to $\pi$.

Note that $\pi$ is traditionally represented in radians, which are a slightly different unit than degrees:

$$360\,\mathrm{degrees} = 2\pi, \mathrm{radians}.$$

Exercise: Show that the sum of all the angles in any polygon can be written in terms of $\pi$. In particular, show that the sum of angles for an $n$-sided polygon is $(n-2)\pi$. (Hint: cut that polygon into triangles).

Approximating $\pi$ with statistics

Consider a square with sides of length $L$. The biggest circle that can fit in that square clearly has a diameter of $L$.

That circle will have an area given by the typical area formula for a circle:

$$\mathrm{Area}(\bigcirc) = \pi \frac{L^{2}}{4}.$$

Here the radius of the circle is of course half the diameter.

The area of the square itself will just be the product of length and width, or $L^{2}$,

$$\mathrm{Area}(\square) = L^{2}.$$

Therefore, the ratio of the two areas is,

\begin{equation}\label{ratio}\frac{\mathrm{Area}(\bigcirc)}{\mathrm{Area}(\square)} = \frac{\pi}{4}.\end{equation}

A way to experimentally determine the value of $\pi$ is to “throw darts” at a square with a circle inscribed upon it. If we stand far enough away from it so that all the darts land in a random spot on the square, the ratio of darts inside the circle to the total number of darts should approximate the ratio of areas in \eqref{ratio}.

Notice that this strategy takes a while to converge to a reasonable approximation of $\pi$.

Other approximations to $\pi$

A famous formula for computing $\pi$ is attributed to the 14th century mathematician Madhava of Sangamagrama, and involves the alternating sum of the reciprocal of odd numbers:

$$\pi = 4\sum_{n=0}^{\infty}\frac{(-1)^{n}}{2n+1}\approx 4\left(1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} + \cdots\right).$$

This formula was rediscovered in the 17th century by Leibniz.

There are other, more complex approximations that approach $\pi$ much more quickly.

Throwing Darts with p5.js

It might be impractical to throw darts - especially given how many darts we will need to even approximate $\pi$ as $3.14$. We can therefore us p5.js to help.

Copy the code below into the p5.js editor. Note that each mouse click counts as a dart toss. As an exercise, adjust the code to automate the throwing of darts.

Click Me!

The Code

let x = 200;
let y = 200;
let tracking = 0;
let counting = 0;
function setup() {
var canvas = createCanvas(400, 400);
  canvas.parent('sketch-holder');
}

function draw() {
background(250);
strokeWeight(1.5);
noFill()
rect(100,100,200,200);
ellipse(200,200,200);

fill(220,10,10);
strokeWeight(0);
ellipse(x,y,15);

let ratio = tracking/counting;
ratio = round(ratio,4)
fill(0,0,0);
textSize(20);
text('Count: '+ tracking.toString(), 100, 350);
text('Total: '+ counting.toString(), 250, 350);
text('Ratio: '+ ratio.toString(), 100, 375 );
text('Pi: '+ (4*ratio).toString(), 250, 375 );
text('Percent Error: '+ round(100*(4*ratio - PI)/PI,3).toString(), 100, 400 );

}

function mouseClicked() {

// randomly place the x and y coordinate of the dot

x = random(100,300);
y = random(100,300);

// at what radius is the dot?
let r = sqrt((x-200)*(x-200) + (y-200)*(y-200));

// check to see if the dot is in the circle.
if ( r <= 100 ) {

// if it is, great! add one to tracking.
tracking += 1 ; }

else {
// if not, don't.
tracking += 0;

}

// update the number of counts
counting += 1;

}
Sean Downes

Theoretical physicist, coffee and outdoor recreation enthusiast.

https://www.pasayten.org
Next
Next

Modelling a Trajectory