Newton Method Code for Karl’s Class

Clear[NewtonTable, NewtonList, NewtonPicture]

NewtonTable[ f_, x_, s_,steps_, n_:15]:=
Block[ {start},
start={x, f, D[f,x], x-f/D[f,x]} /. x-> N[Round[s, 10^-(2n)],n+1];
Return[ TableForm[NestList[ ({x, f, D[f,x], x-f/D[f,x]} /. x-> #[[-1]])&, start, steps], TableHeadings-> {None, {"Current Guess", "f(Current Guess)", "f'(Current Guess)", "Next Guess"}}]]
]

NewtonList[f_, x_, s_,steps_, n_:15]:= NestList[ #-( f/. x-> #)/(D[f,x]/. x-> #) &,N[s,n], steps]

NewtonPicture[f_,{x_,a_,b_},seed_,n_:20]:=Block[{xpoint,curvepoint,curveline,newpoint,graph,s},s=N[seed,n];xpoint={x,0}/. x->s;curvepoint={x,f}/. x->s;curveline=InfiniteLine[curvepoint,{1,(∂_x)f/. x->s}];newpoint={x-(f/((∂_x)f)),0}/. x->s;graph=Plot[f,{x,a,b},Epilog->{Arrow[{xpoint,curvepoint}],PointSize[Large],Black,Point[xpoint],Point[curvepoint],Dashed,curveline,Dashing[{1,0}],Arrow[{curvepoint,newpoint}],Red,Point[newpoint]},ImageSize->400];Return[Column[{graph,N[newpoint[[1]],n]}, Alignment->Center]]]

An Illustration of the Newton’s Method Idea

Sliding down the tangent line to y=sin(x) at x=2.5 to get a better estimate for π:

NewtonPicture[ Sin[x], {x,1,4},2.5]

A Newton’s Method Table

A look at building a systematic table for Newton’s Method, including the component columns; the “next guess” is easy to get at (column 1)-(column 2)/(column 3), and shows how close the value of f( current guess) is to 0:

NewtonTable[ x^3+3x-5,x,1,10]

Two Newton’s Method Problems:

Not converging to the right root:

NewtonTable[Sin[x],x,17/10,10]

A look at why it’s not going to 0 or π:

NewtonPicture[ Sin[x], {x,1,10},1.7]

A non-converging example:

NewtonTable[x^2+2x+5,x,2,20, 20]

And why not:

Solve[ x^2+2x+5==0, x, Reals]

(no real roots to find)