Newton's Amazing Method: Estimate π to How Many Places?

Introduction

OBJECTIVE:  Use Mathematica to implement Newton's Method.

To how many places can you estimate π or e or (Sqrt[2])? In this module, Isaac Newton and Mathematica team up to help you in this endeavor. Estimates to thousands of places are at your fingertips when you have such an awesome pair to help you. You will also encounter some of the hazards in using Newton's Method.

Technology Guidelines

NOTE:  If you have just finished a module, restart Mathematica or Quit Kernel under the Evaluation pull-down menu before executing a new module.
TO OPEN CELLS, click on the arrow to the left of the cell or put your cursor on the right cell bracket and double click. To open all cells, highlight the bracket on the far right and select Grouping under the Cell pull-down menu and select Open All Subgroups.
TO STOP AN EXECUTION
Select the Evaluation pull-down menu and click on Abort Evaluation.
ORDER OF EXECUTION
Execute cells in the order given. Do not skip any Input cells within a given notebook.
SAVING NOTEBOOKS
You can save anytime to any directory you choose, and it is wise to save often. However, before you do your final save to reduce the size of your file, it is a good idea to delete all your output by selecting the Delete All Output selection under the Cell pull-down menu.
EXPERIENCING MAJOR PROBLEMS
Save if appropriate, then shut down Mathematica and start it up again.

Part I: Using Newton's Method to Estimate π

Setting Up the Problem

For those of us who enjoy mathematical challenges, estimating irrational numbers like π, e, and (Sqrt[2]) to a large number - no, to a very large number of decimal places - is an exercise of enjoyment. Let's see if we can crash the computer!

To estimate π, we will use Newton's method to estimate the zero of the function f(x) = tan x , for π/2 < x < 3π/2. As you know already, the exact answer is π. First, we define the function f[x_ ]. Clear the symbol x so that there is no conflict in defining the Newton iteration function. Later on you will want to change the function f[x_ ] and clearing x now will avoid complications when you redefine the iteration function for the new f[x_ ].

Clear[f,x];
f[x_]=Tan[x];

\[MathematicaIcon] About Mathematica

Now we plot f(x) over the specified domain of interest to determine the approximate location of an x-value that makes the function zero.

Plot[f[x],{x,(π/2),((3 π)/2)},PlotRange->{-2,2},PlotStyle->{RGBColor[0,0,1]}]

The plot window is cut down by specifying PlotRange -> {-2, 2} so that we can see the root clearly.

Applying Newton's Method

We set up Newton's iteration scheme thus.

fprime[x_]=f'[x];
digits=100;
newt[x_]:=SetPrecision[x-((f[x])/(fprime[x])),digits];

We have set the precision at 100 digits for the estimate of the zero. After we go through this once, you will want to come back and change this value, but leave it at 100 for now.

\[MathematicaIcon] About Mathematica

Using the graph of the function, we specify a starting value of x=3 for the iterations. We iterate until two consecutive calculated values of x are equal to the precision specified by digits. The symbol n is used to count the number of iterations required to achieve the specified precision. For an explanation of the following commands, click on the "About Mathematica" button below the cell.

Clear[x,n,xiterates,y]
x=3;	
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

\[MathematicaIcon] About Mathematica

In the cell above, we first initialize the iteration counter, n, to 0. We want to store each iteration count and the corresponding value of x in a list called xiterates, so we initialize the list with the first pair of values, n (=0) and x (=initial guess). Then we assign the current value of x to the symbol y for later comparison with the next computed value of x. We use the newt[ ] function to calculate the next value of x and increment the iteration count n by 1 with n++ (at least one iteration will always be performed). Now we enter the While[ ] command. Inside the While[ ] command, the values of x and y are tested to see if they are equal. If they are not, the other commands are executed in order: the current value of x is again assigned to y for the next test; a new value of x is computed; n++ increments the iteration counter n by 1; and the new values for n and x are appended to the list of xiterates. This procedure is repeated until the condition is not satisfied, that is, until two consecutive estimates of x are equal to the precision specified in digits.

From the previous results, we estimate π to 100 decimal places.

ourestimateofπ=SetPrecision[x,digits]

Testing the Results

How do we know that Newton's method is working correctly and that our estimate of π is correct? We can compare it with Mathematica's estimate of π to the same number of digits.

π-SetPrecision[ourestimateofπ,100]

If Mathematica's estimate of π is correct to the specified number in digits, then so is ours. Not bad!

π-SetPrecision[ourestimateofπ,114]

What is truly amazing is the rate at which Newton's method converges to the root. We can achieve 100 digits of accuracy with so few iterations - Wow! And because it takes so few iterations, it is also very fast. But wait, it gets even better. Go back up and change digits to 1000 or even 10000. See what happens. (Be aware, however, that at some point you will run up against the limitations of your computer, specifically, RAM and cache storage on your hard drive, and it may crash.) Also, when you increase the precision, you have two choices for the initial value of x. If you don't reset it with x=initial value, the iteration will start with the last value calculated (i.e., x to 100 digits). If you reset it, then the first 100 iterations will be repeated on the way to 1000, 10000, 1000000, . . . .

You Try It: Part I

More About π

You may also want to do some research to find out what the current best estimate of π is. Research the history of the efforts that mathematicians have put into this problem.

Check out (Sqrt[2])

See if you can construct functions so that you can use the above code to estimate (Sqrt[2]).  Begin by defining a function that is zero when  x=(Sqrt[2]). We suggest that you try a polynomial. Replace the terms in red with the appropriate function, and change the bounds over which you plot f(x), if you wish.

Clear[f,x];
f[x_]=(x^3)-5;
Plot[f[x],{x,-1,5},PlotStyle->{RGBColor[0,0,1]}]

Now set up Newton's iteration scheme. You can set digits to whatever precision you wish.

fprime[x_]=f'[x];
digits=50;
newt[x_]:=SetPrecision[x-((f[x])/(fprime[x])),digits];

Using the graph of your function, specify a starting value of x (in red) for the iterations. We iterate until two consecutive calculated values of x are equal to the precision specified by digits, and we use the symbol n to count the number of iterations required to achieve the specified precision.

Clear[x,n,xiterates,y]
x=2;	
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

The output from the code above renders your estimate as the following. Compare it to the (Sqrt[2]), and see if it has given the desired precision. It clearly does not if you did not change the function f[x_] (in red).

ourestimate=SetPrecision[x,digits]
(Sqrt[2])-ourestimate

Part II: Hazards in Applying Newton's Method

Let's select a polynomial that we can't factor and begin by looking at its graph.

Clear[f,x];
f[x_]=(x^5)-5 (x^3)-2 (x^2)+1;
pf=Plot[f[x],{x,-3,3},PlotStyle->RGBColor[0,0,1]]

We set up Newton's iteration scheme and specify 20-digit precision.

fprime[x_]=f'[x];
digits=20;
newt[x_]:=SetPrecision[x-((f[x])/(fprime[x])),digits];

This function has three zeros in the domain over which the plot extends. What happens when we specify a starting value of 2?

We iterate until two consecutive calculated values of x are equal to the precision specified by digits. The symbol n is used to count the number of iterations required to achieve the specified precision. For an explanation of the following commands, click on the "About Mathematica" button below the cell.

Clear[x,n,xiterates,y]
x=2;	
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

What if we had started at x=1.5?

Clear[x,n,xiterates,y]
x=1.5;	
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

This time the iterations converged to a different value of x. Why did that happen? Can you tell precisely where the break would occur? Does it have anything to do with the derivative of the function?

What if we had started at x=0?

Clear[x,n,xiterates,y]
x=0;	
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

This time Newton's method didn't work at all. What is it that has given the (1/0) expression? It might help to look at the graph again.

Show[pf]

Let's try x = -1.5.

Clear[x,n,xiterates,y]
x=-1.5;
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

Why does this converge to the zero to the right instead of to the closer zero to the left?

You Try It: Part II

Here is a cubic polynomial. Study its graph.

Clear[f,x];
f[x_]=(x^3)-5 (x^2)-2 x+11;
pf=Plot[f[x],{x,-2,5.5`},PlotStyle->RGBColor[0,0,1]]
fprime[x_]=f'[x];
digits=20;
newt[x_]:=SetPrecision[x-((f[x])/(fprime[x])),digits];

You decide where you should begin your iterations in order to compute each of the zeros of this polynomial. By changing the term in red, you determine which starting values converge to which zero. Check it out with the given starting point of x = 0.

Clear[x,n,xiterates,y]
x=0;
n=0;
xiterates={{n,x}};
y=x;
x=newt[x];
n++;
xiterates=Append[xiterates,{n,SetPrecision[x,digits]}];
While[SetPrecision[y,digits]≠SetPrecision[x,digits],y=x;x=newt[x];n++;xiterates=Append[xiterates,{n,SetPrecision[x,digits]}]];
TableForm[xiterates,TableHeadings->{None,{"n","x"}}]
Print["The number of iterations is ", n, " and the zero is at x = ",SetPrecision[x,digits]];

What happened? It seems to have completely skipped over a zero! Why did this happen?

\[MathematicaIcon] About Mathematica

Whenever you define a new function like f[x_] in the preceding input cell, you should Clear[f, x] so that all previous definitions of the function and the independent variable are deleted. It is very important here because later we will want to redefine f[x_] with a new formula in terms of x. If numeric values were previously assigned to x and we did not clear them, our new f[x_] would be defined with the numeric value in place of x, giving a constant function. Clearing x ensures that the intended formula is assigned to f[x_] rather than a constant value. Sometimes you want Mathematica to remember a previous definition of a symbol, but many times you do not. Managing symbol definitions in Mathematica is very important because mismanaging them can produce some very misleading and confusing results. Learn more about defining symbols in Mathematica in "Overview of Mathematica: Assignment Commands in Mathematica," included in this supplement. You can also go to the Help Browser and type Clear. Go back.

The SetPrecision[ ] command allows us to specify the number of digits for the numbers in an expression. To learn more about precision and the SetPrecision[ ] command, go to the Help Browser and type Precision. Go back.

The While[ ] command is useful for performing an operation repeatedly as long as a specified condition is met. In the example above, the condition is inequality of consecutive estimates of x within the precision specified in digits. This test condition is the first argument of the While[ ] command. (Many numerical routines on calculators and computers work this way. They keep improving the numerical estimate of the value until consecutive approximations are equal to each other within the working precision limits of the calculator or computer processor.) The second argument in the While[ ] command is a string of operations to perform while the condition specified in the first argument is satisfied. Semicolons are used to separate the operations in the string. Go back.