Lab & Writing Assignment 2B

Instructor: Michael Pelsmajer
Teaching Assistant: Francisco Hernandez
Monday November 3 or Thursday November 5, 2014

Newton’s Method, step by step.

Start by running the following code using Shift-Enter (and continue like this).

f[x_]:=(x^3)-x^2-x -7;
Plot[f[x],{x,0,4}]

You can see that there is a root between x = 2 and x = 3, right?

We'll use Newton's Method to find it.
Start with a rough estimate to use as (Subscript[x, 1]), which I'll call "initialGuess".

initialGuess = 3

Each step: Find the tangent line at (Subscript[x, i]) and then find its x-intercept and set it equal to (Subscript[x, i+1]).

Run the code below, and use the slider to see it play out.

x0=initialGuess;
frames0=ConstantArray[0,4];
thecurve0=Plot[f[x],{x,2.2,3.5}];
firstguess0=Graphics[{PointSize[Large],Green,Point[{x0,0}]}];
frames0[[1]]=Show[thecurve0,firstguess0];
xline0=Graphics[{Dashed,Gray,Line[{{x0,0},{x0,f[x0]}}]}];
pointoncurve0=Graphics[{PointSize[Large],Blue,Point[{x0,f[x0]}]}];
frames0[[2]]=Show[frames0[[1]],xline0,pointoncurve0];
tangent0=f'[x0]*(x-x0)+f[x0];
tplot0=Plot[tangent0,{x,2,4},PlotRange->{{2,3.1},{-5,15}},PlotStyle->Red];
frames0[[3]]=Show[frames0[[2]],tplot0];
xintercept0=Graphics[{PointSize[Large],Green,Point[{x0-f[x0]/f'[x0],0}]}];
frames0[[4]]=Show[frames0[[3]],xintercept0];
Manipulate[frames0[[n]],{n,1,4,1},FrameLabel->"Newton's Method, Step 1: from x1 to x2"]

Now, repeat the previous step until you have (hopefully) reached the root.

Newton’s Method: The whole thing, in a form that you can copy, modify, and use.

In the following code, you can modifty: 
   numsteps (the number of steps), 
   intLeft and intRight (left and right endpoints of visible domain), 
   initialGuess (also called (Subscript[x, 1])),
   and the function, f[x].

Run the code below and use the slider to see the steps.

f[x_]:=(x^3)-x^2-x -7;
xn=initialGuess;
numsteps=3;
intLeft = 2.3; 
intRight = 3.2;
frames=ConstantArray[0,numsteps];
thecurve=Plot[f[x],{x,intLeft,intRight}];
firstguess=Graphics[{PointSize[Medium],Green,Point[{xn,0}]}];
frames[[0]]=Show[thecurve,firstguess];
For[n=1,n≤numsteps,n++,
xline=Graphics[{Gray,Line[{{xn,0},{xn,f[xn]}}]}];
xnpointoncurve=Graphics[{PointSize[Medium],Blue,Point[{xn,f[xn]}]}];
tangent=f'[xn]*(x-xn)+f[xn];
lessRed = 1-n/numsteps;
tplot=Plot[tangent,{x,intLeft,intRight},PlotStyle->RGBColor[1,lessRed ,lessRed ]];
xnext = xn-f[xn]/f'[xn];
xintercept=Graphics[{PointSize[Medium],Green,Point[{xnext,0}]}];
frames[[n]]=Show[frames[[n-1]],xnpointoncurve,tplot, xintercept,xline];
xn=xnext;
]
Manipulate[frames[[n]],{n,0,numsteps,1},FrameLabel->"Newton's Method: a few steps"]

You can see that (Subscript[x, 3]) and (Subscript[x, 4]) are quite close to each other.

Newton’s Method, more quickly.

This time we’ll use Newton’s formula to compute successive approximations.
We’ll start with the initial guess (Subscript[x, 1]) = 3, as before.
Then compute (Subscript[x, 2]), then (Subscript[x, 3]), and so on, until we seem to be settled on a root.

g[x_]:=x-f[x]/f'[x] ;
N[g[3],15]

N[g[%],15]

N[g[%],15]

N[g[%],15]

N[g[%],15]

N[g[%],15]

Did it work?  Did we find a root of f[x]?

f[%]

Exercises

See Writing Assignment Guidelines (on web page or handout) for more details about how to write up the results.  Due in class Friday next week.

(Contrary to what I previously announced, you will not be revising Lab 2A.)

Exercise 1

Find all roots of this function, using Newton’s Method (the version with Newton’s formula, which is quicker).

h[x_]:=(x^3)-4x-1
Plot[h[x],{x,-5,5}]

Exercise 2

(a) Look at these calculations, which produce two numbers.  Writing: What will happen if you try to use either of these values as the initial guess in Newton’s Method - and why?

h'[x]
Solve[h'[x]==0,x]

(b) From the next calculation, you’ll can see that one of those values is approximately 1.1547.  Try 1.15 as an initial guess in Newton’s Method, and run it until it finds a root.  (It will take longer than usual.)  Writing: What caused Newton’s Method to take longer to finish this time?

N[2/Sqrt[3]]

Exercise 3

Run Newton’s method on the function (x^3)-4x-1 using these four initial values:
  0.75
  0.8
  0.81
  0.85 
The different initial values will yield different roots.  Writing: Discuss how/why this happens.

This time, you’ll need to use the version of Newton’s Method where you can see all the tangent lines and x-intercepts with the graph.  To see what’s happening, you’ll need to modify “intLeft” and “intRight”; I suggest that you set the interval to be [-2,2] or [-3,3].  You’ll also need to change “numSteps” to be larger; 10 or so.  (And of course, you’ll have to modify “f[x]”, too.)

Exercise 4

Since tan((π/4))=1, we can calculate π by using finding a root of tan((x/4))-1.
Do that, using Newton’s Method.