next up previous
Next: Exercises Up: Newton's Method Previous: Purpose

Background

Many problems in mathematics, science, engineering, and business eventually come down to finding the roots of a nonlinear equation. It is a sad fact of life that many mathematical equations cannot be solved analytically. You already know about the formula for solving quadratic polynomial equations. You might not know, however, that there are formulas for solving cubic and quartic polynomial equations. Unfortunately, these formulas are so cumbersome that they are hardly ever used. Even more unfortunately, it has been proven that no formula can exist for finding roots of quintic or higher polynomials. Furthermore, if your equations involve trig functions, then it is even easier to find equations that do not have analytical solutions. For example, the following simple equation cannot be solved to give a formula for x.

\begin{displaymath}
\sin(x) = \frac{x}{2} \end{displaymath}

The need to solve nonlinear equations that cannot be solved analytically has led to the development of numerical methods. One of the most commonly used numerical methods is called Newton's method or the Newton-Raphson method. The idea of Newton's method is relatively simple. Suppose you have a nonlinear equation of the form

f(x) = 0

where f is a differentiable function. Then the idea of Newton's method is to start with an initial guess x0 for the root and to use the tangent line to f at x=x0 to approximate f. The equation for the tangent line appears below.

y = f(x0)+f'(x0) (x-x0)

If the tangent line is a good approximation to f, then the x intercept of the tangent line should be a good approximation to the root. Call this value of x where the tangent line intersects the x axis x1. We can solve the equation above to get the following formula.

\begin{displaymath}
x_1 = x_0-\frac{f(x_0)}{f'(x_0)} \end{displaymath}

In practice, unless the starting point x0 is very close to the root, the value x1 is not close enough to the root we are seeking, so Newton's method is applied again using the tangent line at x=x1. This process can be repeated, leading to a sequence of values $x_0,x_1,x_2,\ldots$ where the value of xn+1 is determined from the equation

\begin{displaymath}
x_{n+1} = x_n-\frac{f(x_n)}{f'(x_n)}, \mbox{ $n=0,1,2,3,\ldots$} \end{displaymath}

For example, consider the equation from above,

\begin{displaymath}
\sin(x) = \frac{x}{2} \end{displaymath}

If you want to solve this equation using Newton's method, the first thing to do is to write it in standard form as

\begin{displaymath}
\sin(x) - \frac{x}{2} = 0\end{displaymath}

Then, plot the expression to get an idea of where the roots are. You may have to adjust the plot range to locate all of the roots. The following commands show that there are exactly three roots, one at x=0, and two others at about 2 and -2.
  > f := x -> sin(x)-x/2;

\begin{maplelatex}
\begin{displaymath}
{f} := {x} \rightarrow {\rm sin}(\,{x}\,) - {\displaystyle 
\frac {1}{2}}\,{x}\end{displaymath}\end{maplelatex}
  > plot(f(x),x=-6..6);

It isn't very hard to write a Maple command that will do one step of Newton's method. The examples below show a very simple method for doing so, using the function f defined above and a starting value of x=2.2.

  > newt := x -> x-f(x)/D(f)(x);

\begin{maplelatex}
\begin{displaymath}
{\it newt} := {x} \rightarrow {x} - {\dis...
 ...{{\rm f}
(\,{x}\,)}{{\rm D}(\,{f}\,)(\,{x}\,)}}\end{displaymath}\end{maplelatex}
  > newt(2.2);

\begin{maplelatex}
\begin{displaymath}
1.932197247\end{displaymath}\end{maplelatex}
More steps can be obtained by using composition, as shown below. One can even get a little fancier by using the Maple seq command and the Maple composition operator, as the last example below shows.
  > newt(newt(2.2));

\begin{maplelatex}
\begin{displaymath}
1.896235713\end{displaymath}\end{maplelatex}
  > (newt@@3)(2.2);

\begin{maplelatex}
\begin{displaymath}
1.895494585\end{displaymath}\end{maplelatex}
  > seq((newt@@i)(2.2),i=1..4);

\begin{maplelatex}
\begin{displaymath}
1.932197247, 1.896235713, 1.895494585, 1.895494267\end{displaymath}\end{maplelatex}
However, to simplify things for you, two commands, Newton and NewtonPlot, have been programmed for you. The Newton command takes three arguments: the function, the starting value, and the number of iterations. See the example below for three iterations. Note that these commands are part of the CalcP package, so you must load the package first.
  > with(CalcP):
  > Newton(f,x=2.2,3);

\begin{maplelatex}
\begin{displaymath}
{{x}_{0}}=2.2, {\rm f} \left( \! \,{{x}_{0}}\, \! \right) =
-.2915035962\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{1}}=1.932197247, {\rm f} \left( \! \,{{x}_{1}}\, \! 
 \right) =-.0306962261\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{2}}=1.896235713, {\rm f} \left( \! \,{{x}_{2}}\, \! 
 \right) =-.0006075214\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{3}}=1.895494585, {\rm f} \left( \! \,{{x}_{3}}\, \! 
 \right) =-.2604\,10^{-6}\end{displaymath}\end{maplelatex}
The NewtonPlot command gives a graphical representation of Newton's method by plotting the function f and the sequence of tangent lines on the same graph. It also shows vertical lines from the x intercept for each tangent line to the graph of f. Here is an example.
  > NewtonPlot(f,x=2.2);

One of the problems with Newton's method is knowing when to stop. With a numerical method, you can never get a root exactly, but only a numerical approximation to the root. There are basically two measures of how good your approximation to the root is. One is the absolute value of f(xn). If this number is less than a certain tolerance, then you should have a good approximation to the root. The other measure is the change in the value of xn. If the value of xn and xn+1 are very close, then this can also be a criterion for stopping. You should go back to the example above and experiment with changing the number of iterations.

The worst thing about Newton's method is that it may fail to converge. Here is an example that doesn't seem to be converging after five iterations. Try increasing the number of iterations and see what happens.

  > g := x -> x^5-12*x^4+3*x^3+7*x^2-2*x-1;

\begin{maplelatex}
\begin{displaymath}
{g} := {x} \rightarrow {x}^{5} - 12\,{x}^{4} + 3\,{x}^{3} + 7\,{x
}^{2} - 2\,{x} - 1\end{displaymath}\end{maplelatex}
  > plot(g(x),x=-2..2,y=-10..10);
  > Newton(g,x=0,5);

\begin{maplelatex}
\begin{displaymath}
{{x}_{0}}=0, {\rm g} \left( \! \,{{x}_{0}}\, \! \right) =-1\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{1}}=-.5000000000, {\rm g} \left( \! \,{{x}_{1}}\, \!
 \right) =.593750000\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{2}}=.8571428570, {\rm g} \left( \! \,{{x}_{2}}\, \! 
 \right) =-1.696852501\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{3}}=.7017005501, {\rm g} \left( \! \,{{x}_{3}}\, \! 
 \right) =-.659376399\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{4}}=.4901458996, {\rm g} \left( \! \,{{x}_{4}}\, \! 
 \right) =-.6096392122\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{5}}=.8572623766, {\rm g} \left( \! \,{{x}_{5}}\, \! 
 \right) =-1.698157667\end{displaymath}\end{maplelatex}
The key to getting Newton's method to converge is to select a good starting value. The best way to do this is to plot the function and determine approximately where the roots are. The use these values to start Newton's method. For example, the following commands help locate a root of g near x=-0.6 and then show that Newton's method converges rapidly.
  > plot(g(x),x=-2..2,y=-10..10);
  > Newton(g,x=-0.6,5);

\begin{maplelatex}
\begin{displaymath}
{{x}_{0}}=-.6, {\rm g} \left( \! \,{{x}_{0}}\, \! \right) =
.43904\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{1}}=-.7138589212, {\rm g} \left( \! \,{{x}_{1}}\, \! 
 \right) =-.398075095\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{2}}=-.6787928159, {\rm g} \left( \! \,{{x}_{2}}\, \! 
 \right) =-.047078893\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{3}}=-.6733924562, {\rm g} \left( \! \,{{x}_{3}}\, \! 
 \right) =-.001026418\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{4}}=-.6732693663, {\rm g} \left( \! \,{{x}_{4}}\, \! 
 \right) =-.527\,10^{-6}\end{displaymath}\end{maplelatex}

\begin{maplelatex}
\begin{displaymath}
{{x}_{5}}=-.6732693030, {\rm g} \left( \! \,{{x}_{5}}\, \! 
 \right) =-.1\,10^{-8}\end{displaymath}\end{maplelatex}

next up previous
Next: Exercises Up: Newton's Method Previous: Purpose

William W. Farr
12/2/1997