Newton's Method: Finding the Root of a Function, Study notes of Mathematics

How to use newton's method to find the root of a given function. It includes examples using mathematica and graphical illustrations. The process involves finding the intersection of the tangent line with the x-axis and repeating the process with the new point.

Typology: Study notes

Pre 2010

Uploaded on 03/19/2009

koofers-user-563
koofers-user-563 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Newton’ s Method
Example : Find the root of f (x) =
ex+x-3
= 0
If we draw the graph of f (x), we can see that the root of f (x) = 0 is the x - coordinate of the point where the curve intersects with
the x - axis.
In[1]:=
f=Exp@xD+x-3;
Plot@f, 8x, -4, 4<D
Out[2]=
-4
-2
2
4
The derivative and tangent line at a given point (x0,y0) = (x0, f [x0]) is given by f ’[x0] * (x-x0) + y0
In[3]:=
df =D@f, xD;
slope =df . x ®x0;
y0 =f. x ®x0;
tangentline =slope *Hx-x0L+y0
Out[6]=
-3+ ãx0 +I1+ ãx0MHx-x0L+x0
To find the root, we will use the Newton’ s iteration. First, choose a starting point, for example, x0=2. Clearly this is not the root.
Draw the tangent line at x0=2:
pf3
pf4
pf5

Partial preview of the text

Download Newton's Method: Finding the Root of a Function and more Study notes Mathematics in PDF only on Docsity!

Newton’ s Method

Example : Find the root of f (x) = e

x

+ x - 3 = 0

If we draw the graph of f (x), we can see that the root of f (x) = 0 is the x - coordinate of the point where the curve intersects with

the x - axis.

In[1]:= f^ =^ Exp @ x D^ +^ x^ -^ 3; Plot @ f, 8 x, - 4, 4 <D

Out[2]=

  • 4 - 2 2 4

10

20

30

The derivative and tangent line at a given point (x0,y0) = (x0, f [x0]) is given by f ’[x0] * (x-x0) + y

In[3]:= df = D @ f, x D ; slope = df ê. x Æ x0; y0 = f ê. x Æ x0; tangentline = slope *** H** x - x0 L + y

Out[6]= -^3 + „x0^ +^ I^1 + „x0M Hx^ -^ x0L^ +^ x

To find the root, we will use the Newton’ s iteration. First, choose a starting point, for example, x0=2. Clearly this is not the root.

Draw the tangent line at x0=2:

In[7]:= plotf = Plot @ f, 8 x, 0, 2.5 <D ; plotp0 = Graphics @8 Red, Dashed, Line @88 2, 0 < , 8 2, f ê. x Æ 2 <<D < D ; tangentline0 = tangentline ê. x0 Æ 2 plott0 = Plot @ tangentline0, 8 x, 0, 2 < , PlotStyle Æ 8 Red <D ; Show @ plotf, plotp0, plott0 D

Out[9]= -^1 + „^2 +^ I^1 + „^2 M H-^2 +^ xL

Out[11]=

0.5 1.0 1.5 2.0 2.

  • 2

2

4

6

8

10

The tangent line intersect with the x - axis at a point. Let us find the coordinate of this point :

In[12]:= NSolve @ tangentline0^ ä^ 0,^ x D

Out[12]= 88 x^ Æ^ 1.23841<<

From the graph, we can see that 1.23841 is closer to the solution of f(x)=0 than 2. Now repeat the above process, draw the tangent

line at 1.23841.

In[13]:= plotp1^ =^ Graphics @8 Green,^ Dashed,^ Line @88 1.23841, 0 < ,^^8 1.23841,^ f^ ê. x^ Æ^ 1.23841 <<D < D ; tangentline1 = tangentline ê. x0 Æ 1. plott1 = Plot @ tangentline1, 8 x, 0, 1.23841 < , PlotStyle Æ 8 Green <D ; Show @ plotf, plotp0, plott0, plotp1, plott1 D

Out[14]= 1.68853 + 4.45012 H-1.23841 + xL

Out[16]=

0.5 1.0 1.5 2.0 2.

  • 2

2

4

6

8

10

Find the intersection point of the second tangent line with the x - axis :

The intersection of the magenta tangentline with the x - axis is very close to the actural solution of f (x) = 0. We can expect that

repeating the above process will give us even better approximation to the solution. In Mathematica , there’s a neat way to complete

the entire process in one single command. We first define a function called NewtonsMethodList :

In[23]:= NewtonsMethodList @ f _ ,^^8 x _ , x0 _< , n _D^ : = N @ NestList @ - Function @ x, f D@D ê Derivative @ 1 D@ Function @ x, f DD@D &, x0, n DD

In the above definition, (f_, x_, x0_, n_) are input parameters. f_ is the expression to be solved, x_ is the name of the unknown

variable, x0_ is the starting point, n_ is the number of iterations (repeat the tangent line process n_ times). Now let’ s use this

function to find the root of f (x) = ex^ + x - 3 = 0.

In[24]:= values^ =^ NewtonsMethodList @ Exp @ x D^ +^ x^ -^ 3,^^8 x, 2 < ,^5 D

Out[24]= 8 2., 1.23841, 0.858974, 0.793598, 0.792061, 0.79206<

The values given in the above list are exactly the intersection of tangentlines with the x - axis, as we have seen earlier. They

converge to 0.79206, which is the solution of f(x)=0. We can draw the graph of the approximation.

In[25]:= Show @8 plotf, Table @ Plot @ tangentline,^^8 x,^ - 1, x0 < , PlotStyle^ Æ^8 Hue @ x0^ ê^^2 D<D ,^^8 x0, values <D , Table @ Graphics @8 Hue @ x0 ê 2 D , Dashed, Line @88 x0, 0 < , 8 x0, f ê. x Æ x0 <<D < D , 8 x0, values <D<D

Out[25]=

0.5 1.0 1.5 2.0 2.

  • 2

2

4

6

8

10

Mathematica also provide a built - in function "FindRoot" to solve the problem.It gives the same answer as what we have seen in

the above.

In[26]:= FindRoot @ Exp @ x D^ +^ x^ -^^3 ä^ 0,^^8 x, 2 <D

Out[26]= 8 x^ Æ^ 0.79206<

Example : Choosing the first point is sometimes important.

Consider the root of f HxL = x^3 - 2 x + 2 = 0. From the following graph, it should be between - 2 and - 1.

In[27]:= f = x ^ 3 - 2 ***** x + 2; plotf = Plot @ f, 8 x, - 3, 3 <D tangentline = H D @ f, x D ê. x Æ x0 L * H x - x0 L + H f ê. x Æ x0 L ;

Out[28]=

  • 3 - 2 - 1 1 2 3
    • 10
      • 5

5

10

If we use the starting point x0 = -3, we will get the correct solution around -1.76929.

In[30]:= values^ =^ NewtonsMethodList @ x ^ 3^ -^^2 *****^ x^ +^ 2,^^8 x,^ -^3 < ,^5 D Show @8 plotf, Table @ Plot @ tangentline, 8 x, - 1, x0 < , PlotStyle Æ 8 Hue @ x0 ê 2 D<D , 8 x0, values <D , Table @ Graphics @8 Hue @ x0 ê 2 D , Dashed, Line @88 x0, 0 < , 8 x0, f ê. x Æ x0 <<D < D , 8 x0, values <D< , PlotRange Æ 88 - 3, - 1.5 < , 8 - 10, 1 <<D

Out[30]= 8 - 3.,^ - 2.24,^ - 1.87537,^ - 1.77656,^ - 1.76933,^ - 1.76929<

Out[31]=

  • 3.0 - 2.8 - 2.6 - 2.4 - 2.2 - 2.0 - 1.8 - 1.

However, if we choose the starting point x = 3, we won’ t get the correct solution. Because the Newton’s iteration can not pass

through the local minimum near x=1. The tangentlines will bouncing back and forth over this point.