disp(' ')
disp(' pi_nr.m  ver 1.0  December 1, 2009 ');
disp(' by Tom Irvine  Email: tomirvine@aol.com');
disp(' ');

disp(' Calculate pi using a series expansion of ');
disp(' cos(theta) and by applying the Newton-Raphson method. ');  
disp(' The goal is to find the root of cos(theta)=-1. ');
disp(' ');
%
x=3;  % starting guess
%
for(j=1:100)
%
    sum=1;
    for(i=1:30)
        n=2*i;
        p=((-1)^i);
        sum=sum+p*(x^n/factorial(n));
    end
    sum=sum+1;
%
    fx=sum;
%
    sum=0;
    for(i=1:30)
        n=2*i;
        p=((-1)^i);
        sum=sum+p*n*(x^(n-1)/factorial(n));
    end
%
    dfx=sum;
    x=x-fx/dfx;
    out1=sprintf('%d  %12.10f ',j,x);
    disp(out1);
end