Saturday, March 14, 2015

Happy Pi Day!

Before I get to my post, let me say happy birthday Wacław Sierpiński!

Recently, I watched an Aperiodical video where a group of mathematicians used a variety of methods to estimate the decimal value of π under the constraints of a π-hour time limit and a ban on electronic calculating devices. Needless to say, none of these methods was particularly efficient. This got me thinking of how I might celebrate this mathematical "holiday" by estimating the value of π in my own inefficient manner. What could be more tedious than performing Newton's method on the sine curve? Newton's Method: first step

The concept is fairly straightforward. Imagine that we're driving along a winding road at night. Since the  headlights are affixed to the front of our car and facing forward, the beam of light always points in the direction that the car is moving. Now think of that road as the sine curve, our car as the point on the curve corresponding to our current guess of the value of π, and the headlight beam is what's called the tangent line, which always points in the direction of movement along the curve. Since the sine curve is continuous and crosses the x-axis at π, then as our x-coordinates approach π, our y-coordinates will likewise approach 0. Newton's method basically says that if we're close enough to π, then our car's/guess's tangent line will point towards an x value that's closer to π than our current guess is.

So if our initial guess is 3, then the tangent line through the point (3, sin(3)) will cross the x-axis at a value closer to π than 3 is. That value becomes our next guess, and then we repeat the process until satisfied. The closer that the sine of our guess is to 0, the closer that our guess is to π.

Determining the iteration formula makes for a good first semester calculus problem. I've gone ahead and done that as well as writing a short MATLAB routine* to perform it.
* see Appendix A

The guesses converged to 10 digits of accuracy in only two iterations.

initial guess 3
1st iteration 3.14254654307428
2nd iteration 3.14159265330048
pi 3.14159265358979

Of course, this is sort of like putting the cart before the horse since the procedure presumes that evaluating trigonometric functions is more basic than evaluating π. We can keep with the spirit of the original challenge by evaluating the sines and cosines with means which can be replicated with paper and pencil. This is where second semester calculus gives us the Maclaurin series. I've also taken the liberty of writing another MATLAB routine* that calculates the trigonometric functions using the first eight terms of their respective series. What else would I be doing on Pi Day? :-)
* see Apendix B

This time the accuracy was to 6 digits.

initial guess 3
1st iteration 3.14254589805078
2nd iteration 3.14159188052770
pi 3.14159265358979

I certainly hope that my Pi Day diversions have amused you. Thank you for reading!

Appendix A:

% Script File : Newtons_Pi
% by Javier Pazos
% March 12, 2015
% Estimates the value of pi using Newton's method on the sine curve.
% Initial guess is p = 3.

PiGes = 3;
eps = 1e-4;
SiPi = sin(PiGes);
CoPi = cos(PiGes);
format long

% Main loop.
% Each iteration finds the x-intercept of the tangent line of the
% sine function at the previous estimate.

while abs(SiPi) > eps

% Iteration loop.
% Sets PiGes = (PiGes*cos(PiGes) - sin(PiGes))/cos(PiGes)

PiGes = (PiGes*CoPi - SiPi)/CoPi;
SiPi = sin(PiGes);
CoPi = cos(PiGes);
PiGes % Prints latest guess.

end

Appendix B:

% Script File : Newtons_Pi_Taylor
% by Javier Pazos
% March 12, 2015
% Estimates the value of pi using Newton's method on the sine curve.
% Sine and Cosine are estimated using MacLaurin series.
% Initial guess is p = 3.

PiGes = 3;
eps = 1e-4;
SiPi = sinTay(PiGes);
CoPi = cosTay(PiGes);
format long

% Main loop.
% Each iteration finds the x-intercept of the tangent line of the
% sine function at the previous estimate.

while abs(SiPi) > eps

% Iteration loop.
% Sets PiGes = (PiGes*cos(PiGes) - sin(PiGes))/cos(PiGes)

PiGes = (PiGes*CoPi - SiPi)/CoPi;
SiPi = sinTay(PiGes);
CoPi = cosTay(PiGes);
PiGes % Prints latest guess.

end

function SiEst = sinTay(x)
% by Javier Pazos
% March 13, 2015
% Estimates sine using the MacLaurin series.

% Set number of iterations and initial sum.

n = 7;
sum = 0;
format long

% Main loop
% Sums first n+1 terms of the MacLaurin series for sine.

for j=0:n
sum = sum + ((-1)^j)*(x^(2*j+1))/factorial(2*j+1);
end

% Returns value.

SiEst = sum;
end

function CoEst = cosTay(x)
% by Javier Pazos
% March 13, 2015
% Estimates cosine using the MacLaurin series.

% Set number of iterations and initial sum.

n = 7;
sum = 0;
format long

% Main loop
% Sums first n terms of the MacLaurin series for cosine.

for j=0:n
sum = sum + ((-1)^j)*(x^(2*j))/factorial(2*j);
end

% Returns value.

CoEst = sum;
end