Newton Raphson

The Newton Raphson method is fixed point iteration designed so as to make convergence as quick as possible.  Here is the first example.  We will figure the answer to 50 significant figures to illustrate the speed of convergence.

Clear[f,g,x,a,b,p0]

f[x_]=(x^2)-2;
p0=3;
a=3;
b=1;
g[x_]=x-((f[x])/(f'[x]))

Clear[p]
p[0]=p0;
Do[p[n]=N[g[p[n-1]],50],{n,1,100}]

TableForm[Table[{n,p[n]},{n,0,10}]]

Here is a nice plot to illustrate what the Newton Raphson method is doing.

ppp=Plot[f[x],{x,a,b}];

FixedPointSegments[g_,x0_,its_]:=Module[
{l=NestList[g[#]&,x0,its]},
Partition[Flatten[Transpose[{l,l}]],2,1]
];
MakeMovie[g_,p0_,a_,b_]:=
Module[{p2=Partition[FixedPointSegments[g,p0,15],2,1]},
movie=Table[Plot[{g[x],x},{x,a,b},PlotStyle->{{},Dashing[{.02}]},
Prolog->MapIndexed[{Hue[#2[[1]]/Length[p2]],Line[#]}&,Take[p2,i]],
AspectRatio->Automatic,
AxesLabel->TraditionalForm/@{(Subscript[x, n]),(Subscript[x, n+1])}
],{i,Length[p2]}];
ListAnimate[movie]
]

MakeMovie[g,p0,a,b]

Now lets look at the Secant Method.

Clear[p]
p[0]=3;
p[1]=2;
Do[p[n]=N[p[n-1]-((f[p[n-1]] (p[n-1]-p[n-2]))/(f[p[n-1]]-f[p[n-2]])),50],{n,2,100}]

TableForm[Table[{n,p[n]},{n,0,14}]]

Now let us illustrate a situation when the Newton Raphson method does not work so well.

f[x_]=Exp[2 x]-2 Exp[2+x]+Exp[4];
p0=3;
a=3;
b=1;
g[x_]=x-((f[x])/(f'[x]));

Clear[p]
p[0]=p0;
Do[p[n]=N[g[p[n-1]],50],{n,1,100}]

TableForm[Table[{n,p[n]},{n,0,20}]]

If we want to get rid of the divide by zero errors, try using the Simplify command to simplify some of the expressions:

f[x_]=Exp[2 x]-2 Exp[2+x]+Exp[4];
p0=3;
a=3;
b=1;
g[x_]=x-Simplify[((f[x])/(f'[x]))];

Clear[p]
p[0]=p0;
Do[p[n]=N[g[p[n-1]],50],{n,1,100}]

TableForm[Table[{n,p[n]},{n,0,50}]]

MakeMovie[g,p0,a,b]

The reason for the slow convergence is that f[x] has a double root at x=2, that is, a root of order 2.