Chapter 2 Root Finding
Chapter 2 Root Finding
Root Finding
Objectives:
Develop various methods for solving r=x that satisfies f(x)=0
in which y=f(x) is a nonlinear function.
2.1 Bisection Method
2.1.1 Procedure
Step 1: updating
Set x0=a, x1=b; make sure f0*f1<0
Make own convention: f0>0, f1<0.
f 0 >0 Get x2=(x0+x1)/2
y Check sign of f2=f(x2).
f 2 >0 SetIfx0f=a,
2>0,x1replace
=b; make x0 by x2f0*f1<0
sure
a Make own convention:
b f0>0, f1<0.
fx00 >0 x2 Get x2=(x0+x1x)/2 1 x
y Check sign of f2=f(x2).
f 2 >0 f 1 <0
Since f2>0, replace x0 by x2
a Comments on how to start: b
i) One x 1)/2.
x0 must guarantee x2 f(a)*f(b)<0
**Getin order
x2=(x 0+xto1 begin x the iteration
Check: is |f2| < tolerance?
ii) If f(a)*f(b)<0, Intermediate Value Theorem
if yes, f 1 <0the
accept =>root
a root r in2.(a, b)
as x=x
y
if not, check sign of f2
Step 2: checking convergence if f2<0 replace x1 by x2,
f0 >0 **Get x =(x +x1replace
2 if f20>0 )/2. x0 by x2
Check:goto is |f**2 | < tolerance?
if yes, accept the root as x=x2.
y x2 if not, check x1 sign of x f2
x0
if f2<0 replace x1 by x2,
f0 >0 f <0 if f2>0 replace x0 by x2
2
goto **f 1 <0
y x0 x2 x1 x
f 0 >0 f2 <0
x2 f 1 <0 x
y x1 of Florida
x0 Mei, University
Notes organized by Prof. Renwei 1
f 0 >0 f 1 <0
x2 x
x0 x2 x1 x
f2 <0
f 1 <0
y
f 0 >0
x2 x
x0 x1 (=old x2)
f 1 <0
Comments on checking convergence
(for all methods of root finding):
i) It is easier to use the magnitude of |f(xn)| to see if xn can be
accepted as a root.
ii) Sometimes, however, it is inadequate. Consider f(x)=(x-1)3.
When xn =1+0.01 with an error of | xn –r|=0.01, f(xn)=10-6
which may be accepted by an inexperienced user.
0.03
f(x)
0.02
0.01
-0.01
-0.02
-0.03
0.7 0.8 0.9 1 1.1 1.2 x
a b x
f(x)
Bisection method in this case
will only give one root.
a b x
x1
x2
x0 x
f1
f ( x) = − sin( x) − e x .
Start with x0 = 0 → f(0) = 0 + 0 -1 = -1
& f (0) = 3 +1-1 = 3
x2 = x1 - f1/f1' = 0.3333333-(-0.068417728)/2.5493445
= 0.360171
Iter. # xn f(xn) en =|r-xn| en/ en2−1
0 0 -1 0.36042
1 0.3333333333 -0.6841772829E-01 2.71E-02 0.209
2 0.3601707136 -0.6279850706E-03 2.52E-04 0.346
3 0.3604216805 -0.5625155319E-07 2.25E-08 0.354
4 0.3604217030 -0.6661338148E-15
(treated as exact root r)
Note: f (r ) = 2.501814, f (r ) = -1.786603
1 f (r )
=> − = 0.357
2 f (r )
Comments:
* It may not converge if x0 is too far from x = r.
* If x0 is close to r, Newton's method converges very fast.
Notes organized by Prof. Renwei Mei, University of Florida 9
Example 2: Find the root for: f(x) = exp(x) –2 = 0
using Newton’s method: xn+1 = xn - f(xn)/f′(xn)
Sol. * Exact result: r = ln(2)
* f'(x)= exp(x) xn+1 = xn – [exp(xn)-2] /exp(xn)
1.E-03
xn-x_exact
1.E-04
1.E-05
1.E-06
f(x)=exp(x)-2
1.E-07
1.E-08
1.E-09
0 1 2 3 4 5 6 7 8 9 n
(This graph shows the quadratic behavior of the error curve in the
Log-linear scale)
1
1 − en f (r ) / f (r ) + ...
2
= -en + en
1 − en f (r ) / f (r ) + ...
x2 x3 x1
x0 x
f3
f1
f2
0
x2
-1
-2
-3
-4
-2 -1 0 1 2 3 x
If we choose x0 = 1, x1 =0, then f0 =1.123189, f1 = -1
x1 − x0 0 −1
=> x2 = x0 - f0 =1 - 1.233189 = 0.4709896;
f1 − f 0 − 1 − 1.123189
f(x2)= 0.2651588…
Iter. #. x0 x1 x2 f2
0 1 0 => 0.4709896 0.26515588
1 0 0.4709896 0.37227705 0.02953367
2 0.4709896 0.37227705 0.359904249 -0.001294787
3 0.37227705 0.359904249 0.3604239134 5.496E-6
4 0.359904249 0.360423913 0.3604217034 1.02E-9
Take r ≈ x4 (4th iteration of x2 column), we note r - x3 ≈ 2.2E-6
* Taylor series expansion for f(x) near the root for f(xn) & f(xn-1)
( xn − r ) 2
f ( xn ) = f (r ) + ( xn − r ) f (r ) + f (r ) + ...
2
1
= 0 − en f (r ) + en2 f (r ) + ... (&&)
2
( xn −1 − r ) 2
f ( xn −1 ) = f (r ) + ( xn −1 − r ) f (r ) + f (r ) + ...
2
1
= 0 − en −1 f (r ) + en2−1 f (r ) + ... (&&&)
2
xn − xn −1 = xn − r − ( xn −1 − r ) = −en + en −1
1
f n − f n −1 = − (en − en −1 ) f (r ) + (en2 − en2−1 ) f (r ) + ...
2
(&) =>:
1 − (en − en −1)
-en+1 = -en + en [ f (r ) − en f (r )...]
2 1
− (en − en −1 ) f (r ) + (en2 − en2−1 ) f (r ) + ...
2
1 1
= -en + en [ f (r ) − en f (r )...]
2 1
f (r ) − (en + en −1 ) f (r ) + ...
2
1 1
f (r ) − (en + en −1 ) f (r ) + ... − [ f (r ) − en f (r )...]
en+1 = en 2 2
1
f (r ) − (en + en −1 ) f (r ) + ...
2
1
− en −1 f (r ) + ...
f (r )
= en 2 - en en-1
f (r ) + ... 2 f (r )
k enq = A en (en / k )1 / q
q = 1+ 1/q & k = A k −1 / q
q = ( 5 + 1) / 2 =1.618 or q= (− 5 + 1) / 2 = -0.618
Expect the error to decrease as n increases take q=1.618.
k 1+1 / q = A
x0 x1 x2 x3 x
Iterat ion does not
converge
Notes organized by Prof. Renwei Mei, University of Florida 17
2.4 Method of False Position (Regula Falsi)
2.4.1 Idea and procedure
To avoid the pathology of the secant method, we can take
advantages of both the secant method and the bisection method
i) Ensure f0f1 <0 so that x0 & x1 bracket the root r. (Bisection!)
ii) Compute x2 = x0 - f0 (x1-x0)/(f1 -f0) -- ( Secant!)
iii) Check the stopping criterion (e.g. |f(x2)| < )
iv) If not converged,
if f0 f2 <0 set x1 = x2 & f 1 = f2 --(bisection)
if f1 f2 <0 set x0 = x2 & f 0 = f2
go to step ii) to compute new x2.
The method of False position guarantees the solution as long as
f0 f1 <0 to start with, but it is slower than the secant method.
y
f1
x3
x1 x2 x0 x
10
f
5
0
f
-5
-10
-3 -2 -1 0 1 2 x
Bisection
False
position
Secant
Newton’s
* Clearly, the root is at 3<r<4, NOT near x=2.3 as seen from the inset.
* The derivative f (x) has a discontinuity at x= for x<4.
* Secant method is essentially a derivative based method;
it may not work. 8
6 f
N xn f(xn) f(xn)
1 1.321349 -2.308077 4
2 1.914858 -0.548215
2
3 2.046901 -0.276157
4 2.111226 -0.177942 0
5 2.151783 -0.129541 0 1 2 3 4
6 2.180845 -0.101817 -2
7 2.203403 -8.45E-02
8 2.221934 -7.31E-02 -4
x
9 2.237829 -6.55E-02 -6
10 2.25195 -6.04E-02
n xn n xn
1 1 7 1.3247018
2 1.2599211 8 1.3247149
3 1.3122939 9 1.32471737
4 1.3223538 10 1.32471785
5 1.3242688 11 1.32471794
6 1.3246326 12 1.32471795
n xn n xn
1 -1.333333 8 7.277602
2 1.285714 9 0.019244
3 1.531252 10 -1.000371
4 0.743643 11 1349.3016
5 -2.237161 12 0.000001
6 0.249695 13 -1
7 -1.066493 14 1/0 → ∞
y y=g(x) y y=x
y=x
g(x 1)
g(x 2) y=g(x)
g(x 1)
g(x 0)
g(x 0)
r
x0 x1 x2 x x0 x1 x2 x3 x
r
Diverges Converges
1.E+00 xn-x_exact
1.E-02
linear
1.E-04
Error
1.E-06
1.E-08
x=Ln(x)+2
1.E-10
1.E-12
1.E-14
1.E-16
n
0 5 10 15 20 25 30 35
y f0 f1 y=f(x)
f2
x3 x2 x0 x1 x
◊ Procedure:
i. Let h1 = x1- x0, h2 = x2- x0,
s = x - x0; = h2/h1
parabola: y = as2 + bs + c
c = f0,
f1 − f 0 (1 + ) + f 2
a= ,
h1 (1 + )
2
f1 − f 0 − ah12
b=
h1
1
ii. Set y = 0 s = (−b b 2 − 4ac )
2a
1 (−b) 2 − (b 2 − 4ac )
so that s=
2a − b b 2 − 4ac
2c
= = x - x0
2
− b b − 4ac
2c
x3 = x0 + s = x0 - .
2
b b − 4ac
iii. Now, which sign (or root of the quadratic equation) to take?
Take "+" if b>0; take " - " if b<0
possibly largest denominator so that x3 is closer to x0.
iv. Which three points to use to construct next parabola?
If x3>x0, use current (x0, x3, x1) as NEW (x2, x0, x1).
If x3<x0, use current (x2, x3, x0) as NEW (x2, x0, x1).
v. Go to step i. to evaluate a, b, c,...
◊ Example:
f(x) = 3x+sin(x) - exp(x)
using x1=0, x2=1.0, x0=0.5
Iter. # x f(x)
1 0.3549138905 -1.380655671E-02
2 0.3604646779 1.075137341E-04
3 0.3604216977 -1.325225929E-08
4 0.3604217030 4.440892099E-16
C PROGRAM PNEWTN
C
C --------------------------------------------------------------------
C
C DRIVER FOR NEWTON'S METHOD SUBROUTINE, NEWTN
C
C --------------------------------------------------------------------
C
IMPLICIT NONE
REAL*8 FCN, FDER, X, XTOL, FTOL
INTEGER I, NLIM
EXTERNAL FCN, FDER
C
C ---------------------------------------------------------------------
C
C INITIALIZE VARIABLES FOR SUBROUTINE NEWTN
C
DATA X, XTOL, FTOL, I, NLIM / 0.5, 1.0E-14, 1.0E-17, 0, 50/
write(6,*) 'Input x0='
read(5,*) x
C
C --------------------------------------------------------------------
C
CALL NEWTN(FCN,FDER,X,XTOL,FTOL,NLIM,I)
C
C THE ROOT HAS BEEN FOUND AT X IF I EQUALS 1 OR 2
C
STOP
END
C
C -------------------------------------------------------------------
C
DOUBLE PRECISION FUNCTION FCN(X)
REAL*8 X, SIN, EXP
FCN = 3.0*X + SIN(X) - EXP(X)
C FCN= X-EXP(-X*X)
RETURN
END
C
C -------------------------------------------------------------------
C
DOUBLE PRECISION FUNCTION FDER(X)
x
A double root A trip le root
2 3
f(x) ~ (x-r) f(x) ~ (x-r)
For double root, bisection method & false position method
DO NOT WORK because we cannot bracket the root as
both require that f0f1<0.
Newton's method or Muller's method works but converges slowly.
Example: solving f(x) = (x-1)2 = 0 starting from x0 = 0.5
xn f(xn) en=r-x en/en-1
0.5 0.25 0.5
0.75 0.0625 0.25 0.5
0.875 0.015625 0.125 0.5
0.9375 0.00390625 0.0625 0.5
0.96875 0.00097656 0.03125 0.5
0.984375 0.00024414 0.01563 0.5
0.9921875 6.1035E-05 0.00781 0.5
0.99609375 1.5259E-05 0.00391 0.5
0.99804688 3.8147E-06 0.00195 0.5
0.99902344 9.5367E-07 0.00098 0.5
0.99951172 2.3842E-07 0.00049 0.5
0.99975586 5.9605E-08 0.00024 0.5
0.99987793 1.4901E-08 0.00012 0.5
0.99993896 3.7253E-09 6.1E-05 0.5
We observe that for the double root (p=2), the error, en = r-xn,
decreases linearly with n,
en+1 / en =1/2 (c.f. en+1 / en2 = constant for single root)
1 f (r )
en+1 / en2 = − + ... if f (r ) 0
2 f (r )
1 2
en f (r ) + ...
e
= -en – 2 =− n
− en f (r ) + ... 2
en+1 / en = 1/2
For a triple root (p=3) or higher,
en+1 / en = (p-1)/p
(for p=1, en/ en-1 = 0 & it simply means the convergence is much
faster than "linear"; )
Note: the error | en | = |r- xn| is reduced by a factor of 2/3 every iteration;
Iter. # x f(x)
1 0.1225399674E+01 0.5698629752E-01
2 0.1012450098E+01 0.1559738635E-03
3 0.1000038711E+01 0.1498563087E-08
4 0.1000000000E+01 0.1403493791E-18
* Consider f ( x) = ( x − r ) m & f ( x) = m( x − r ) m −1
with unknown m.
f(x) x−r
* Let u(x) = = which is a linear function with only
f (x) m
f (xn ) f (xn )
Or xn +1 = xn −
f (xn )2 − f (xn ) f (xn )
* Example: Solve f(x)=(x-1)*[exp(x-1)-1]= 0; x0 =2
Solution: (this is double root case; we do not need to know m)
n x f f' f" |r-xn|
0 2 1.718282 4.436564 8.15485 1
1 0.65569209 0.100293 -0.535304 1.17341 0.344308
2 0.97362152 0.000687 -0.051725 1.92224 0.026378
3 0.99982757 2.97E-08 -0.000345 1.99948 0.000172
4 0.99999999 5.52E-17 -1.49E-08 2 7.43E-09
2.8.2Dekker's method
2.8.3Brent's method
must hold; otherwise the midpoint m is used for the next iterate.
is used instead.