chap. 5 bracketing methods

46
Roots of Equations Roots of Equations Our first real numerical method – Root finding Finding the value x where a function y = f(x) = 0 You will encounter this process again and again

Upload: trantuyen

Post on 03-Jan-2017

255 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Chap. 5 Bracketing Methods

Roots of EquationsRoots of Equations Our first real numerical method – Root finding Finding the value x where a function y = f(x) = 0 You will encounter this process again

and again

Page 2: Chap. 5 Bracketing Methods

Two Fundamental ApproachesTwo Fundamental Approaches

Bracketing Methods Bisection False Position ApproachOpen Methods Fixed-Point Iteration Newton-Raphson Secant Methods Roots of Polynomials

Page 3: Chap. 5 Bracketing Methods

Chapter 5Chapter 5

Bracketing Methods

Page 4: Chap. 5 Bracketing Methods

Bracketing MethodsBracketing Methods

5.1 Introduction and Background 5.2 Graphical Methods 5.3 Bracketing Methods and Initial Guesses 5.4 Bisection 5.5 False-Position

Page 5: Chap. 5 Bracketing Methods

Graphical Graphical methodsmethods No root

(same sign)

Single root (change sign)

Two roots (same sign)

Three roots (change sign)

Page 6: Chap. 5 Bracketing Methods

Multiple Roots

Discontinuity

Special CasesSpecial Cases

Page 7: Chap. 5 Bracketing Methods

Graphical Method - Graphical Method - Progressive Progressive

EnlargementEnlargement

Two distinct roots

Page 8: Chap. 5 Bracketing Methods

Graphical MethodGraphical Method Graphical method is useful for

getting an idea of what’s going on in a problem, but depends on eyeball.

Use bracketing methods to improve the accuracy

Bisection and false-position methods

Page 9: Chap. 5 Bracketing Methods

Bracketing MethodsBracketing Methods Both bisection and false-position

methods require the root to be bracketed by the endpoints.

How to find the endpoints? * plotting the function * incremental search * trial and error

Page 10: Chap. 5 Bracketing Methods

Incremental SearchIncremental Search

Page 11: Chap. 5 Bracketing Methods

Incremental SearchIncremental Search

Page 12: Chap. 5 Bracketing Methods

>> xb=incsearch(inline('sin(10*x)+cos(3*x)'),3,6)number of brackets: 5 xb = 3.24489795918367 3.30612244897959 3.30612244897959 3.36734693877551 3.73469387755102 3.79591836734694 4.65306122448980 4.71428571428571 5.63265306122449 5.69387755102041>> yb = xb.*0yb = 0 0 0 0 0 0 0 0 0 0>> x=3:0.01:6; y=sin(10*x)+cos(3*x);>> plot(x,y,xb,yb,'r-o')

Incremental SearchIncremental Search

Find 5 roots

Page 13: Chap. 5 Bracketing Methods

missed

missed

Use 50 intervals between Use 50 intervals between [3, 6][3, 6]

1 2 3 4 5

Page 14: Chap. 5 Bracketing Methods

Increase Subintervals to 200Increase Subintervals to 200>> xb=incsearch(inline('sin(10*x)+cos(3*x)'),3,6,200)number of brackets: 9xb = 3.25628140703518 3.27135678391960 3.36180904522613 3.37688442211055 3.73869346733668 3.75376884422111 4.22110552763819 4.23618090452261 4.25125628140704 4.26633165829146 4.70351758793970 4.71859296482412 5.15577889447236 5.17085427135678 5.18592964824121 5.20100502512563 5.66834170854271 5.68341708542714>> yb = xb.*0;>> H = plot(x,y,xb(:,1),yb(:,1),'r-v',xb(:,2),yb(:,2),'k^');>> set(H,'LineWidth',2,'MarkerSize',8)

Find all 9 roots!

Page 15: Chap. 5 Bracketing Methods

Find all 9 roots

Incremental SearchIncremental Search

Page 16: Chap. 5 Bracketing Methods

Bracketing Methods

Graphic Methods (Rough Estimation) Single Root e.g.(X-1) (X-2) = 0 (X = 1, X =2) Double Root e.g. (X-1)^2 = 0 (X = 1)

Effective Only to Single Root Cases f(x) = 0 xr is a single root then f(xl)*f(xu) always < 0 if xl < xr and xu > xr.

Page 17: Chap. 5 Bracketing Methods

Bisection Method

Step 1: Choose xl and xu such that xl and xu bracket the root, i.e. f(xl)*f(xu) < 0. Step 2 : Estimate the root (bisection). xr = 0.5*(xl + xu) Step 3: Determine the new bracket. If f(xr)*f(xl) < 0 xu = xr else xl = xr end Step 4: Examine if xr is the root.

Page 18: Chap. 5 Bracketing Methods

Bisection MethodBisection Method5. If not Repeat steps 2 and 3 until convergence

reached been has iterations of number maximumthe (c)

iterations successive in 100x

xx (b)

f(x ei 0f(x (a)

snewr

oldr

newr

a

rr

%)(

).,.,)

x*x2

x1

x3

Non-monotonic convergence: x1 is closer to the root than x2 or x3

Page 19: Chap. 5 Bracketing Methods

o x

y

xu

f(xl)f(xu) <0

xl

xm=0.5(xl+xu)

Bisection Method

Page 20: Chap. 5 Bracketing Methods

If f(xm )f(x l) < 0th e n xu = xme lse xl = xm

e nd

S o lu tiono b ta indx r = xm

If f(xm ) ? = 0

xm = 0 .5(x l+ xu)

In pu t x l & xuf(x l)* f(xu ) < 0

S ta rt

no yes

BisectionFlowchart

Page 21: Chap. 5 Bracketing Methods

Determine the mass m of a bungee jumper with a drag coefficient of 0.25 kg/m to have a velocity of 36 m/s after 4 s of free fall.

Rearrange the equation – solve for m

t

mgc

cmgtv d

d

tanh)(

Mass of a Bungee JumperMass of a Bungee Jumper

0tvtm

gccmgmf d

d

)(tanh)(

Page 22: Chap. 5 Bracketing Methods

Graphical Depiction of Bisection Method Graphical Depiction of Bisection Method

0tvtm

gccmgmf d

d

)(tanh)( (50 kg < m < 200 kg)

Page 23: Chap. 5 Bracketing Methods

Hand Calculation ExampleHand Calculation Example

23 02x,x estimeates initial03x2xxfExample

ul

2

.,.)(:

0375002496099375201253975260750050200125305397525150099409752053924302025005323923603909223622214416223021xxfxxxiter rrul

..............................

)(

Bisection Method

f(2) = 3, f(3.2) = 0.84

Page 24: Chap. 5 Bracketing Methods

Use “inline” command to specify the

function “func”

M-file in textbook

Page 25: Chap. 5 Bracketing Methods

break: terminate a “for” or “while” loop

a x b

ya

yb

y

Use “feval” to evaluate the

function “func”

An interactive M-file

Page 26: Chap. 5 Bracketing Methods

1. Find root of Manning's equation

2. Two other functions

0S2hb

bhn1Qf(h) 1/2

2/3

5/3

Examples: BisectionExamples: Bisection

0xe)x(f 2x

01x3x)x(f 3

Page 27: Chap. 5 Bracketing Methods

Bisection Method for Manning EquationBisection Method for Manning Equation»bisect2('manning')enter lower bound xl = 0enter upper bound xu = 10allowable tolerance es = 0.00001maximum number of iterations maxit = 50Bisection method has converged step xl xu xr f(xr) 1.0000 0 10.0000 5.0000 264.0114 2.0000 5.0000 10.0000 7.5000 -337.3800 3.0000 5.0000 7.5000 6.2500 -25.2627 4.0000 5.0000 6.2500 5.6250 122.5629 5.0000 5.6250 6.2500 5.9375 49.4013 6.0000 5.9375 6.2500 6.0938 12.2517 7.0000 6.0938 6.2500 6.1719 -6.4605 8.0000 6.0938 6.1719 6.1328 2.9069 9.0000 6.1328 6.1719 6.1523 -1.7740 10.0000 6.1328 6.1523 6.1426 0.5672 11.0000 6.1426 6.1523 6.1475 -0.6032 12.0000 6.1426 6.1475 6.1450 -0.0180 13.0000 6.1426 6.1450 6.1438 0.2746 14.0000 6.1438 6.1450 6.1444 0.1283 15.0000 6.1444 6.1450 6.1447 0.0552 16.0000 6.1447 6.1450 6.1449 0.0186 17.0000 6.1449 6.1450 6.1449 0.0003 18.0000 6.1449 6.1450 6.1450 -0.0088 19.0000 6.1449 6.1450 6.1450 -0.0042 20.0000 6.1449 6.1450 6.1450 -0.0020

Page 28: Chap. 5 Bracketing Methods

CVEN 302-501CVEN 302-501

Homework No. 4Homework No. 4 Chapter 4 Problems 4.10 (15), 4.12 (15) Hand computation Chapter 5 Problem 5.8 (20) (hand calculations for parts b) and c) Problem 5.1 (20) (hand calculations) Problem 5.3 (20) (hand calculations) Problem 5.4 (30) (MATLAB Program)

Due 09/22/08 Monday at the beginning of the periodDue 09/22/08 Monday at the beginning of the period

Page 29: Chap. 5 Bracketing Methods

False-Position (point) MethodFalse-Position (point) Method

Why bother with another method? The bisection method is simple and guaranteed to

converge (single root) But the convergence is slow and non-monotonic! The bisection method is a brute force method and

makes no use of information about the function Bisection only the sign, not the value f(xk ) itself False-position method takes advantage of function

curve shape False position method may converge more quickly

Page 30: Chap. 5 Bracketing Methods

Algorithm for Algorithm for False-Position MethodFalse-Position Method

1. Start with [xl , xu] with f(xl) . f(xu) < 0 (still need to bracket the root)2. Draw a straight line to approximate the root

3. Check signs of f(xl) . f(xr) and f(xr) . f(xu) so that [xl , xu ] always bracket the root

Maybe less efficient than the bisection method for highly nonlinear functions

)()())(()(

ul

uluurr xfxf

xxxfxx 0xf

Page 31: Chap. 5 Bracketing Methods

False-Position MethodFalse-Position Methody(x)

x

secant line

x*xl xu

xr

Straight line (linear) approximation to exact curve

y(xu)

y(xl)

Page 32: Chap. 5 Bracketing Methods

False Point Method Step 1: Choose xl and xu. f(xl)*f(xu) < 0. Step 2 : Estimate the root: Find the intersection of the line connecting the two points (xl,

f(xl)), and (xu,f(xu)) and the x-axis. xr = xu - f(xu)(xu - xl)/(f(xu) - f(xl)) Step 3: Determine the new bracket. If f(xr)*f(xl) < 0 , then xu = xr else xl = xr Step 4: whether or not repeat the iteration (see

slide 18).

Page 33: Chap. 5 Bracketing Methods

o x

y

xu,f(xu)

xl,f(xl)

xr=xu - f(xu)(xu-xl)/(f(xu) -f(xl))

False-point method

xr,0

Page 34: Chap. 5 Bracketing Methods

From geometry, similar triangles have similar ratios of sides

The new approximate for the root : y(xr ) = 0 This can be rearranged to yield False Position

formula

ru

ru

lu

lu

xxxyxy

xxxyxyslope

)()()()(

)()()( u

ul

ulur xf

xfxfxxxx

False-Position MethodFalse-Position Method

Page 35: Chap. 5 Bracketing Methods

I f f(xm )f(x l) < 0th e n xu = xre lse x l = xr

e nd

S o lu tiono b ta ind

x r

If f(x r) ? = 0o r

a b s (x r - p x r) < to le ra n ce

x r = xu - f(xu )(x l -xu )/(f(x l) - f(xu ))

In pu t x l & xuf(x l)* f(xu ) < 0

S ta rt

no yes

False-point MethodFlowchart

Page 36: Chap. 5 Bracketing Methods

000027409999931522399985624000576099985622399698230120709968223937522246109375223021xfxxxiter rrul

................

)(

Hand Calculation ExampleHand Calculation Example

23 02x,x estimeates initial03x2xxfExample

ul

2

.,.)(:

False-

Position

Page 37: Chap. 5 Bracketing Methods

1. Find root of Manning's equation

2. Some other functions

0S2hb

bhn1Qf(h) 1/2

2/3

5/3

Examples: False-PositionExamples: False-Position

0xe)x(f 2x2

01x3x)x(f 3

Page 38: Chap. 5 Bracketing Methods

Linear interpolation

False-positionFalse-position

(Regula-Falsi)(Regula-Falsi)

Linear Interpolation

Method

Page 39: Chap. 5 Bracketing Methods

False-Position (Linear Interpolation) Method False-Position (Linear Interpolation) Method Manning EquationManning Equation

>> false_position('manning')enter lower bound xl = 0enter upper bound xu = 10allowable tolerance es = 0.00001maximum number of iterations maxit = 50False position method has converged step xl xu xr f(xr) 1.0000 0 10.0000 4.9661 271.4771 2.0000 4.9661 10.0000 6.0295 27.5652 3.0000 6.0295 10.0000 6.1346 2.4677 4.0000 6.1346 10.0000 6.1440 0.2184 5.0000 6.1440 10.0000 6.1449 0.0193 6.0000 6.1449 10.0000 6.1449 0.0017 7.0000 6.1449 10.0000 6.1449 0.0002

Much faster convergence than the bisection method

May be slower than bisection method for some cases

Page 40: Chap. 5 Bracketing Methods

Why don't we always use false position method?

There are times it may converge very, very slowly.

Example:

What other methods can we use?

04x3x)x(f 4

Convergence RateConvergence Rate

Page 41: Chap. 5 Bracketing Methods

Convergence slower than bisection method

midpointroot

30xx04x3xxf

ul

4

,,)(

1 2 3 12

Page 42: Chap. 5 Bracketing Methods

04x3x)x(f 4

Bisection Method False-Position MethodBisection Method False-Position Method» xl = 0; xu = 3; es = 0.00001; maxit = 100;» [xr,fr]=bisect2(inline(‘x^4+3*x-4’))Bisection method has converged step xl xu xr f(x) 1.0000 0 3.0000 1.5000 5.5625 2.0000 0 1.5000 0.7500 -1.4336 3.0000 0.7500 1.5000 1.1250 0.9768 4.0000 0.7500 1.1250 0.9375 -0.4150 5.0000 0.9375 1.1250 1.0312 0.2247 6.0000 0.9375 1.0312 0.9844 -0.1079 7.0000 0.9844 1.0312 1.0078 0.0551 8.0000 0.9844 1.0078 0.9961 -0.0273 9.0000 0.9961 1.0078 1.0020 0.0137 10.0000 0.9961 1.0020 0.9990 -0.0068 11.0000 0.9990 1.0020 1.0005 0.0034 12.0000 0.9990 1.0005 0.9998 -0.0017 13.0000 0.9998 1.0005 1.0001 0.0009 14.0000 0.9998 1.0001 0.9999 -0.0004 15.0000 0.9999 1.0001 1.0000 0.0002 16.0000 0.9999 1.0000 1.0000 -0.0001 17.0000 1.0000 1.0000 1.0000 0.0001 18.0000 1.0000 1.0000 1.0000 0.0000 19.0000 1.0000 1.0000 1.0000 0.0000

» xl = 0; xu = 3; es = 0.00001; maxit = 100;» [xr,fr]=false_position(inline(‘x^4+3*x-4’))False position method has converged step xl xu xr f(xr) 1.0000 0 3.0000 0.1333 -3.5997 2.0000 0.1333 3.0000 0.2485 -3.2507 3.0000 0.2485 3.0000 0.3487 -2.9391 4.0000 0.3487 3.0000 0.4363 -2.6548 5.0000 0.4363 3.0000 0.5131 -2.3914 6.0000 0.5131 3.0000 0.5804 -2.1454 7.0000 0.5804 3.0000 0.6393 -1.9152 8.0000 0.6393 3.0000 0.6907 -1.7003 9.0000 0.6907 3.0000 0.7355 -1.5010 10.0000 0.7355 3.0000 0.7743 -1.3176 11.0000 0.7743 3.0000 0.8079 -1.1503 12.0000 0.8079 3.0000 0.8368 -0.9991 13.0000 0.8368 3.0000 0.8617 -0.8637 14.0000 0.8617 3.0000 0.8829 -0.7434 15.0000 0.8829 3.0000 0.9011 -0.6375 16.0000 0.9011 3.0000 0.9165 -0.5448 17.0000 0.9165 3.0000 0.9296 -0.4642 18.0000 0.9296 3.0000 0.9408 -0.3945 19.0000 0.9408 3.0000 0.9502 -0.3345 20.0000 0.9502 3.0000 0.9581 -0.2831 …. 40.0000 0.9985 3.0000 0.9988 -0.0086 …. 58.0000 0.9999 3.0000 0.9999 -0.0004

Page 43: Chap. 5 Bracketing Methods

» x = -2:0.1:2; y = x.^3-3*x+1; z = x*0;» H = plot(x,y,'r',x,z,'b'); grid on; set(H,'LineWidth',3.0);» xlabel('x'); ylabel('y'); title('f(x) = x^3 - 3x + 1 = 0');

Example: Rate of ConvergenceExample: Rate of Convergence

Page 44: Chap. 5 Bracketing Methods

>> bisect2(inline('x^3-3*x+1'))enter lower bound xl = 0enter upper bound xu = 1allowable tolerance es = 1.e-20maximum number of iterations maxit = 100exact zero found step xl xu xr f(xr) 1.0000 0 1.0000 0.5000 -0.3750 2.0000 0 0.5000 0.2500 0.2656 3.0000 0.2500 0.5000 0.3750 -0.0723 4.0000 0.2500 0.3750 0.3125 0.0930 5.0000 0.3125 0.3750 0.3438 0.0094 6.0000 0.3438 0.3750 0.3594 -0.0317 7.0000 0.3438 0.3594 0.3516 -0.0112 8.0000 0.3438 0.3516 0.3477 -0.0009 9.0000 0.3438 0.3477 0.3457 0.0042 10.0000 0.3457 0.3477 0.3467 0.0016 11.0000 0.3467 0.3477 0.3472 0.0003 12.0000 0.3472 0.3477 0.3474 -0.0003 13.0000 0.3472 0.3474 0.3473 0.0000 14.0000 0.3473 0.3474 0.3474 -0.0001 .. . .. . 50.0000 0.3473 0.3473 0.3473 -0.0000 51.0000 0.3473 0.3473 0.3473 0.0000 52.0000 0.3473 0.3473 0.3473 -0.0000 53.0000 0.3473 0.3473 0.3473 -0.0000 54.0000 0.3473 0.3473 0.3473 0

Comparison of rate of convergence for bisection and false-position method

Continued on next page

Page 45: Chap. 5 Bracketing Methods

>> false_position(inline('x^3-3*x+1'))enter lower bound xl = 0enter upper bound xu = 1allowable tolerance es = 1.e-20maximum number of iterations maxit = 100exact zero found step xl xu xr f(xr) 1.0000 0 1.0000 0.5000 -0.3750 2.0000 0 0.5000 0.3636 -0.0428 3.0000 0 0.3636 0.3487 -0.0037 4.0000 0 0.3487 0.3474 -0.0003 5.0000 0 0.3474 0.3473 -0.0000 6.0000 0 0.3473 0.3473 -0.0000 7.0000 0 0.3473 0.3473 -0.0000 8.0000 0 0.3473 0.3473 -0.0000 9.0000 0 0.3473 0.3473 -0.0000 10.0000 0 0.3473 0.3473 -0.0000 11.0000 0 0.3473 0.3473 -0.0000 12.0000 0 0.3473 0.3473 -0.0000 13.0000 0 0.3473 0.3473 -0.0000 14.0000 0 0.3473 0.3473 -0.0000 15.0000 0 0.3473 0.3473 -0.0000 16.0000 0 0.3473 0.3473 -0.0000 17.0000 0 0.3473 0.3473 0

iter1=length(x1); iter2=length(x2); k1=1:iter1; k2=1:iter2;>> root1=x1(iter1); root2=x2(iter2);>> error1=abs((x1-root1)/root1); error2=abs((x2-root2)/root2);>> H=semilogy(k1,error1,'ro-',k2,error2,'bs-'); set(H,'LineWidth',2.0);>> xlabel('Number of Iterations'); ylabel('Relative Error');

Compute relative errors

f(x) = x3 – 3x +1 = 0

Page 46: Chap. 5 Bracketing Methods

Rate of ConvergenceRate of Convergencef(x)= x3 3x + 1

Bisection Method

False position