Newton's Method for solving equations.

Important:  To view this notebook, first go to the "kernel" menu, choose "evaluation" and then "evaluate notebook".  I have removed all the output from this notebook to conserve space.

Here is a little program called  SingleStep for doing one step of Newtons Method.  It requires that the function (f)  whose root is to be found has been defined and that an initial guess called  guess  has been initialized.  It calculates the next approximation to the root of f, and updates the variable guess to the new approximation.

I've also hidden a program called NewtonStep, which does exactly the same thing, but also draws graphs illustrating the method.

Needs["Graphics`Colors`"]NewtonStep[f_] :=Module[
		{next,  realroot,  line,  curve},
		next =  guess - N[((f[guess])/(f'[guess]))];
	
		
	Print["guess =  ",  guess,  "   next  = ",  next]
                ;
realroot  =  x /. FindRoot[f[x],  {x,  guess}];
scale  =  1.5* Abs[guess  - realroot];
\(line\  = \ 
                Plot[\ f[guess]\  + \ \(f'\)[guess]\ *\((x\  - \ guess)\), 
                  \ {x, \ realroot - scale, \ realroot\  + \ scale}, 

                  				PlotStyle\  -> {AbsoluteThickness[2], \ Red}, 

                  			DisplayFunction\  -> Identity]\);
\(curve\  = \ 
                Plot[\ f[x], 
                  \ {x, \ realroot - scale, \ realroot\  + \ scale}, 

                  				PlotStyle\  -> {AbsoluteThickness[2], Black}, 

                  			DisplayFunction\  -> Identity]\);
\(points\  = \ 
                Graphics[

                  		{PointSize[ .02], \ Blue, 
                    \ {Point[{guess, \ 0}], 
					
                      Point[{guess, \ f[guess]}], \ 
				
                      Point[{next, \ 0}]}}, \ Color\  -> \ Blue
				]\);
		\(Show[{line, \ curve, \ points}, \ 	
                DisplayFunction\  -> \ $DisplayFunction]\);
		guess  =  next;
		]

SingleStep[f_] :=Module[
		{next},
		next =  guess - N[((f[guess])/(f'[guess]))];Print["guess =  ",  guess,  "   next  = ",  next]
                ;
		guess  =  next;
		]

Let's define a function to test the single step Newton's method.

\(
Clear[f]\)\(f[x_]\  := \ x^3\  - \ 6  x\  + \ 1; 

    Plot[f[x], \ {x, \ \(-3\), \ 3}]; 

\)

As we can see from the plot, there is a root of f  between 2 and 3. Let's try to find the root of f[x] = 0 which is between 2 and 3, by using the initial guess  x = 3.

\(guess\  = \ 3; 
NewtonStep[f]\)

The program NewtonStep updates the variable "guess" with the value of the "next" approximation to the root.  So to take another sstep, we just run NewtonStep[f] again:

\(\(
NewtonStep[f]\)\)

And again.

\(\(
NewtonStep[f]\)\)

And again.

\(\(
NewtonStep[f]\)\)

If we had used the program SingleStep, we would have got the same results, but without the graphs.

\(guess\  = \ 3; 
SingleStep[f]\)SingleStep[f]SingleStep[f]\(SingleStep[f]
\)

Here is a Newton's method program, which repeats the basic step until the difference between the current guess and the next value of x is very small, and then stops.   The program is called NaiveNewton.
That is, the program consists of a loop which repeats one step of Newton's Method, and a test, which determines when the program should stop.  As you no doubt already know, a loop with a test is  a very basic programming technique.
(We will discuss a flowchart for the program in class.)

Clear[NaiveNewton]NaiveNewton[f_, guess_]:=
	Module[{newtstep, root},
		newtstep = 1;
		root = guess;
		While[Abs[newtstep] > 10^(-6),
			newtstep=N[-((f[root])/(f'[root]))];
			root = root + newtstep;
		Print[root];
		];
		N[root, 6]]

The built-in Newton's method procedure in Mathematica is called FindRoot.  Let us compare our Naive Newton's method with the built-in method, for find the root of  of f[x] = 0 which lies between 2 and 3.

\(\(
NaiveNewton[f, 3]\)\)

FindRoot[f[x],  {x,  3}]

Let's use Newton's method to compute the square root of a number.

\(OurSqrt[a_]\  := \ 
      Module[{f, \ guess}, 
		guess\  = \ a; 
				
        f[x_]\  = \ x^2 - a; 
	NaiveNewton[f, \ guess]
	]\)

Example:  compute the square root of 2.

OurSqrt[2]N[Sqrt[2],  6]

Here's a method of computing cube roots.

\(OurCubeRoot[a_]\  := \ 
      Module[{f, \ guess}, 
		guess\  = \ a; 
				
        f[x_]\  = \ x^3 - a; 
	NaiveNewton[f, \ guess]
	]\)

OurCubeRoot[ .3]\(N[\@ .3\%3, \ 6]\)

Let's solve the equation \(\(
x\^2 = Cos[x]\)\)

Plot[x^2  - Cos[x],  {x,  -3, 3}]

There appears to be a root a little less than 1  (and of course, its opposite as well, as  (x^2) - Cos[x] is an even function.

f[x_]  :=  x^2  -  Cos[x]NaiveNewton[f,  1]

Let's solve the equation \(\(
x/2 = Sin[x]\)\)

Plot[x/2  - Sin[x],  {x,  -3, 3}]

f[x_]  :=  x/2  -  Sin[x]NaiveNewton[f,  1.5]