Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction...

24
10/3/2019 1 Computational Science: Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline Introduction Bracketing Methods The Bisection Method False‐Position Method Multiple Roots 2 1 2

Transcript of Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction...

Page 1: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

1

Computational Science:

Computational Methods in Engineering

Introduction to Root Finding and Bracketing Methods

Outline

• Introduction•Bracketing Methods

• The Bisection Method• False‐Position Method

•Multiple Roots

2

1

2

Page 2: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

2

Slide 3

Introduction

What is Root Finding?

4

What values of xmakes f(x) = 0?

2Let 0f x ax bx c

This is easily solved algebraically

2

r

4

2

b b acx

a

But what if…

0xf x e x This cannot be solved analytically.A numerical method must be used.

3

4

Page 3: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

3

Root Finding Methods

• Bracketing Methods• Finding a single root that falls within a known range.• Very robust• Must know something ahead of time.

• Open Methods• Trial‐and‐error iterative methods• Do not need bounds, only an initial guess.• More efficient than bracketing methods• Can be unstable and not find a solution

• Roots of Polynomials• Algorithm specific to polynomials• Physics of your problem must be fit to a polynomial• Able to find all roots.

5

Requires initial bounds

L r Ux x x

Requires an initial guess

r 1x x

Requires multiple points

1 1 2 2, , , , , ,N Nx y x y x y

Generalizing Root Finding Algorithms

6

Root finding algorithms find all values of x such that f(x) = 0.

What if it is desired to find all values of x such that f(x) = a?

Generalization

f x a

0f x a

Let g x f x a

Now perform standard root finding on g(x). 

5

6

Page 4: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

4

Preliminary Root Location

Slide 7

The basic root finding algorithms all require that a root be roughly located.  The root finding algorithm only refines the rough location.

This means approximate locations of the roots must be somehow determined.  This can be accomplished several ways.

1. Something is known about the physics of the problem being solved that provides information about where the roots should be.

2. If nothing else is known, plot the function to identify the rough location of the roots and feed each of those rough locations into a root‐finding algorithm.  Several plots may have to be generated while experimenting with axis scaling to find the roots.

Need for Better Algorithms

8

In many circumstances, a single computation of f(x) may take hours, days, or weeks!  In these cases it is highly desired to minimize the total number of computations of f(x).

It is often worth the investment of a few hours to write an awesome code that runs in an hour than waiting days or weeks for the answer to come from a simple code you wrote in a few minutes.

7

8

Page 5: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

5

Slide 9

The Bisection Method

Step 1

10

Pick a lower and upper bound, xL and xU that is known to contain a single root between them.

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Lx

Ux

f(x)

Root we wish to find.

9

10

Page 6: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

6

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 2

11

Calculate the midpoint between xL and xU as the first guess for the root.

Lx

Ux

f(x)

First guess

U Lr 2

x xx

Step 3

12

Calculate the function at the midpoint xr.

Lx

Ux

f(x) rf x

11

12

Page 7: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

7

Step 4

13

Adjust the bounds.

Lx

Ux

f(x)

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 5

14

Calculate the new midpoint.

Lx

Ux

f(x)U L

r 2

x xx

Second guess

13

14

Page 8: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

8

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 6

15

Calculate the function at the new midpoint xr.

Lx

Ux

f(x)

rf x

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 7

16

Adjust the bounds.

Lx

Ux

f(x)

15

16

Page 9: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

9

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 8

17

Calculate a new midpoint xr.

Lx

Ux

f(x)

U Lr 2

x xx

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 9

18

Calculate the function at the new midpoint xr.

Lx

Ux

f(x)

rf x

17

18

Page 10: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

10

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 10

19

Adjust the bounds.

Lx

Ux

f(x)

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 11

20

Calculate a new midpoint xr.

Lx

Ux

f(x)

U Lr 2

x xx

19

20

Page 11: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

11

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 12

21

Calculate the function as the new midpoint.

Lx

Ux

f(x)

rf x

Step 13 and Beyond…

22

And so on…

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 1

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 2

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 3

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 4

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 5

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 6

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 7

x

f(x)

2.5 3 3.5 4

-0.5

0

0.5

1

Iteration 8

x

f(x)

21

22

Page 12: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

12

Adjusting the Bounds (1 of 2)

23

Be careful about signs when adjusting the bounds.

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-1.2

-1.0

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

x

f(x)

Move upper boundMove upper bound

Here the function is positive and it is the upper bound that is moved.

Here the function is negative and it is still the upper bound that is moved.

Adjusting the Bounds (2 of 2)

24

elsebreak;

end

L rIf 0f x f x then there is a sign change between xL and xr.This means the root is closer to xL than xU.Move xU to xr.

% Adjust the Boundsif fL*fr<0 %root toward fL

xU = xr;fU = fr;

elseif fL*fr>0 %root toward fUxL = xr;fL = fr;

L rIf 0f x f x then the sign change must be between xU and xr.This means the root is closer to xU than xL.Move xL to xr.

L rIf 0f x f x then the root is found exactly.

23

24

Page 13: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

13

When is the Algorithm Finished?

25

i. Calculate the amount xr has moved from one iteration to the next.

ii. At the end of each iteration, check if xr is less than some threshold.

new oldr r

r oldr

x xx

x

r tolerancex Rule of thumb:  If you want some number of digits of precision, ensure 𝛿𝑥 is at least an order of magnitude less than the desired precision.

WARNING!  Do not set the convergence condition to |xU – xL| < tolerance because this will fail when the same boundary is being adjusted each iteration.

Block Diagram of Bisection Method

26

Choose xL and xU.Choose xL and xU.

Evaluate f(x) at endpoints.Evaluate f(x) at endpoints. L L U U f f x f f x

Calculate midpoint.Calculate midpoint.

U Lr 2

x xx

Evaluate f(x) at midpoint.Evaluate f(x) at midpoint.

r rf f x

L rf fMove upper bound.Move upper bound.

U r U r x x f f Move lower bound.Move lower bound.

L r L r x x f f

Calculate new xr.Calculate new xr.

new U Lr 2

x xx

Calculate change.Calculate change.new oldr r

r newr

x xx

x

r tolx

Done!Done!

0

0 0

noDone!Done!

yes

Start

25

26

Page 14: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

14

Algorithm for Bisection Method

27

1. Choose lower and upper bounds, xL and xU so that they surround a root.2. Evaluate the function at the endpoints, f(xL) and f(xU).3. Calculate midpoint xr.

4. Iterate until convergeda) Evaluate the function at the midpoint f(xr).b) Adjust the bounds.

c) Update the midpoint.

d) Determine if convergedi. Calculate step size

ii. Algorithm is converged if xr < tolerance.

U Lr 2

x xx

L r U r U r

L r L r L r

L r

If 0, then and

If 0, then and

If 0, then DONE!

f x f x x x f f

f x f x x x f f

f x f x

U Lr 2

x xx

new oldr r

r newr

x xx

x

Notes on Bisection Method

•Most robust root finder

• Least efficient root finder•Guaranteed to find a root as long as the bounds span a crossing

• Sometimes good to check sign change of bounds• No sign change – 0 or even number of roots.• Sign change – odd number of roots.

28

27

28

Page 15: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

15

Slide 29

False‐Position Method

More Intelligent “Midpoint”

30

Lx Ux

Uf x

Lf xnew U Lr

Bisection Method

2

x xx

U Lnew U L U Lr

U L

False-Position Method

2 2

f x f xx x x xx

f x f x

Since                            , it is a good assumption that the root is closer to xL than it is to xU. L Uf x f x

29

30

Page 16: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

16

Step 1

31

Pick a lower and upper bound, xL and xU that is known to contain a single root.

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Lx

Ux

f(x)

Root we wish to find.

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 2

32

Calculate a better estimate of the position of the root using a linear approximation.

Lx

Ux

f(x)

Better estimatenewrx

31

32

Page 17: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

17

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 3

33

Calculate the function at the new estimate for xr.

Lx

Ux

f(x)

rf x

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 4

34

Adjust the bounds.

Lx

Ux

f(x)

33

34

Page 18: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

18

2.50 2.75 3.00 3.25 3.50 3.75 4.00

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

x

f(x)

Step 5

35

Estimate the position of the root by linear approximation.

LxUx

f(x)

newrx

Second guess

Derivation of Estimate (1 of 3)

36

The equation of a line given a point (x0, y0) and slope m is

0 0y y m x x

Assuming the function connecting the bounds is close to linear, the slope is approximately

U L

U L

f x f xm

x x

35

36

Page 19: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

19

Derivation of Estimate (2 of 3)

37

Choose the lower bound to be the point in the line equation.

0 L

0 L

x x

y f x

U L

L LU L

m

f x f xy f x x x

x x

Now estimate the position of the root by setting y = 0 and solving for x.

U L newL r L

U L

Lnewr L U L

U L

0f x f x

f x x xx x

f xx x x x

f x f x

Derivation of Estimate (3 of 3)

38

Choose the upper bound to be the point in the line equation.

0 U

0 U

x x

y f x

U L

U UU L

m

f x f xy f x x x

x x

Now estimate the position of the root by setting y = 0 and solving for x.

U L newU r U

U L

Unewr U U L

U L

0f x f x

f x x xx x

f xx x x x

f x f x

37

38

Page 20: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

20

Interpretation of Estimate (1 of 2)

39

There are now two possible equations to estimate the position of the root.

Average these equations.

Unew

r U U LU L

f xx x x x

f x f x

Lnewr L U L

U L

f xx x x x

f x f x

L UL U L U U L

U L U Lnew

U LU L U L

U L

2

2 2

r

f x f xx x x x x x

f x f x f x f xx

f x f xx x x x

f x f x

new U Lr 2

x xx

Interpretation of Estimate (2 of 2)

40

Typical bisection method equation

A correction term to give a better estimate.

U L U L

U L 2

f x f x x x

f x f x

39

40

Page 21: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

21

Block Diagram of False‐Position Method

41

Choose xL and xU.Choose xL and xU.

Evaluate f(x) at endpoints.Evaluate f(x) at endpoints. L L U U f f x f f x

Calculate midpoint.Calculate midpoint.

U Lr 2

x xx

Evaluate f(x) at midpoint.Evaluate f(x) at midpoint.

r rf f x

L rf fMove upper bound.Move upper bound.

U r U r x x f f Move lower bound.Move lower bound.

L r L r x x f f

Calculate new xr.Calculate new xr.

new U U L U L

L

Lr

U2 2

f x f x x x

f x

xx

f x

x

Calculate change.Calculate change.new oldr r

r newr

x xx

x

r tolx

Done!Done!

0

0 0

noDone!Done!

yes

Start

This is the only difference from bisection method.

When False‐Position Fails

Slide 42

The false‐position method can fail or exhibit extremely slow convergence when the function is highly nonlinear between the bounds.

This happens because the estimated root is a very poor guess.

41

42

Page 22: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

22

Notes on False‐Position Method

•Very similar to bisection method

•Calculates a more intelligent “midpoint.”

•Converges much faster for near linear functions.

•Converges slower for functions with abrupt curves

43

Slide 44

Multiple Roots

43

44

Page 23: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

23

Recognizing Number of Roots

45

Single Root

• Sign changes on either side of root.

• Slope at root is not zero. r r 0f x f x

r 0f x

Double Root

• Sign is same on either side of root.

• Slope at root is zero.

•Curvature is same on either side of root.

r r 0f x f x

r 0f x

r r 0f x f x

Triple Root• Sign changes on either side of root.

• Slope at root is zero.

•Curvature changes on either side of root.

r r 0f x f x

r 0f x

r r 0f x f x

Quadruple Root• Sign is same on either side of root.

• Slope at root is zero.

• Curvature is same on either side of root.

• Curvature is broad and flat.

r r 0f x f x

r 0f x

r r 0f x f x

Problem with Multiple Roots

Slide 46

Bracketing methods require a sign change on either side of the root.  This means they only work for odd multiple roots.

double root triple root

WILL NOT WORK

45

46

Page 24: Lecture -- Introduction and bracketing methods...Computational Methods in Engineering Introduction to Root Finding and Bracketing Methods Outline •Introduction •Bracketing Methods

10/3/2019

24

A Useful Property

Slide 47

Define an auxiliary function u(x) that will have the same roots as f(x) but will always change sign on either side of any multiple root.

f x

u xf x

f x

triple rootdouble root

single root

The Fix

Slide 48

If f(x) has even multiple roots, perform root finding on the auxiliary function u(x) instead.

47

48