Mathematical Principles for Scientific Computing and Visualization  
http://www.farinhansford.com/books/scv
by Gerald Farin and Dianne Hansford

This notebook demonstrates finding roots with Newton-Raphson method.
Given is a function f and a guess x-value of where the root might live.
This method attempts to find where f intersects the line y=0 within a given number of iterations.

In the first cell, we have an  example where Newton's method is successful in finding the root. In the second cell, we have an example where Newton's method fails.

Figures 10.5 and 10.6 were generated with this notebook.

See Chapter 10: Finding Roots for details.

Updated July 2008

(* Specify the output folder *)

SetDirectory[ToFileName[Extract["FileName"/.NotebookInformation[EvaluationNotebook[]],{1},FrontEnd`FileName]]];

(* Create a function -- we want the roots of this *)
f[x_]:=N[(x^2)-0.5];

(* Set the number of iterations *)
n=3;

(* Establish the domain [a,b] of interest for the funciton f *)
(* This is used for plotting *)
a=0;
b=2;

(* Create a guess x-value for the root *)
xold=3.4;

(* Create storage for the iterations *)
points=Table[{0.0,0.0},{i,1,2 n}];

(* Newton-Raphson method -- see Section 10.2 *)
Do[
xnew=xold-((f[xold])/(f'[xold]));
ynew=f[xnew];
xold=xnew;
points[[2 i-1,1]]=xnew;
points[[2 i-1,2]]=0;
points[[2 i,1]]=xnew;
points[[2 i,2]]=ynew,
{i,1,n}];

(* Plot the function and the y=0 axis *)
fctplot=Plot[{f[x],0},{x,a,b},PlotStyle->{Thickness[0.01],Thickness[0.001]},Axes->False, PlotRange->All];

(* Plot the points joined by line segments *)
polyplot=ListPlot[points,Joined->True,PlotStyle->{PointSize[0.03], Red},Axes->False, PlotRange->All];

(* Plot the points generated by the N-R method *)
pointplot=ListPlot[points,PlotStyle->{PointSize[0.03], Red},Axes->False, PlotRange->All];

(* Put the plots together *)
pic=Show[fctplot,pointplot,polyplot]


Export["Newton.eps",pic];

Newton's method fails in the next example.

(* Create a function -- we want the roots of this *)

f[x_]:=N[(x^3)-2 (x^2)+1.5];

(* set the number of iterations *)
n=7;

(* Establish the domain of interest for the funciton f *)
(* This is used for plotting *)
a=-1.3;
b=3;

(* Create a guess  x-value for the root's location *)
xold=1.4;

(* Create a table of 2n points (0,0) *)
points=Table[{0.0,0.0},{i,1,2 n}];

(* Newton's method *)

Do[
xnew=xold-((f[xold])/(f'[xold]));
ynew=f[xnew];
xold=xnew;
points[[2 i-1,1]]=xnew;
points[[2 i-1,2]]=0;
points[[2 i,1]]=xnew;
points[[2 i,2]]=ynew,
{i,1,n}];


(* Plot the input function and the line y=0 *)
fctplot=Plot[{f[x],0},{x,a,b},PlotStyle->{Thickness[0.01],Thickness[0.001]},Axes->False, PlotRange->All];

(* Plot the points connected by line segments *)
polyplot=ListPlot[points,Joined->True,PlotStyle->{PointSize[0.03], Red},Axes->False, PlotRange->All];


(* Plot the  points *)
pointplot=ListPlot[points,PlotStyle->{PointSize[0.03], Red},Axes->False, PlotRange->All];

(* Put all the plots together *)
pic=Show[fctplot,pointplot,polyplot]

(* Output to a postscript file *)
Export["Newton_fails.eps",pic];