section 5 root finding and optimizationweb.engr.oregonstate.edu › ~webbky ›...

78
MAE 4020/5020 – Numerical Methods with MATLAB SECTION 5: ROOT FINDING AND OPTIMIZATION

Upload: others

Post on 05-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

MAE 4020/5020 – Numerical Methods with MATLAB

SECTION 5: ROOT FINDING AND OPTIMIZATION

Page 2: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

2

Root Finding & Optimization

K. Webb  MAE 4020/5020

Two closely related topics covered in this section Root finding – determination of independent variable values at which the value of a function is zero 

Optimization – determination of independent variable values at which the value of a function is at its maximum or minimum (optima)

Chapra

Page 3: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

Root Finding3

K. Webb  MAE 4020/5020

Page 4: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

4

Root Finding ‐ Example

K. Webb  MAE 4020/5020

Determine the length, L, of a single‐fin heat sink to remove 500mW from an electronic package, given the following:

Width:  w = 1 cm Thickness:  t = 2 mm Heat transfer coeff.:          

h = 100 W/(m2K) Aluminum:  k = 210 W/(m∙K) Ambient temperature:  

40° Base temperature:  

100°

Page 5: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

5

Root Finding ‐ Example

K. Webb  MAE 4020/5020

Fin heat transfer rate is given by:

∙sinh cosh

cosh sinh

where

,    

,     

Page 6: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

6

Root Finding ‐ Example

K. Webb  MAE 4020/5020

Would like to set  and solve for  , given all other parameters But, we can’t isolate  – a transcendental equation – can’t be solved algebraically 

Instead, subtract  from both sides

500

∙sinh cosh

cosh sinh500 0

Now, find the value of  for which  A root‐finding problem

Page 7: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

7

Root Finding ‐ Example

K. Webb  MAE 4020/5020

Looking for  such that 

Page 8: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

8

Root Finding ‐ Example

K. Webb  MAE 4020/5020

Find the root of   , i.e.  such that 

Page 9: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

9

Root‐Finding Techniques – Bracketing vs. Open

K. Webb  MAE 4020/5020

Two categories of root‐finding methods: Bracketing methods

Require two initial values – must bracket (one on either side of) the root

Always converge Can be slow

Open methods Initial value(s) need not bracket the root Often fasterMay not converge

Page 10: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

Root Finding: Basic Concepts10

K. Webb  MAE 4020/5020

Page 11: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

11

Presence of a Root – Sign Change

K. Webb  MAE 4020/5020

A root is a value of  at which crosses the x‐axis changes sign

If  is a root of  , and , then

Not always true  e.g. multiple roots Won’t consider multiple roots here

Page 12: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

12

Error Evaluation and Tracking

K. Webb  MAE 4020/5020

Approximate error,  Don’t know where the true root is, so must approximate error

, ,

,∙ %

Tells us when a root has been determined to adequate precision – stop when 

True error,  Useful for evaluating the performance of root‐finding algorithms – when we know the location of the root

Page 13: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

Root Finding: Bracketing Methods13

K. Webb  MAE 4020/5020

Page 14: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

14

Root Finding – Bracketing Methods

K. Webb  MAE 4020/5020

We’ll look at three bracketing methods Each require two initial values, which must bracket the root

Incremental search Bisection False position

Page 15: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

15

Incremental Search

K. Webb  MAE 4020/5020

Say we want to find a root,  , which we know exists between  and 

Initialize the search with bracketing values

Starting at  , move incrementally toward  , searching for a sign change in 

Accuracy determined by increment length Too large – inaccurate – could miss closely spaced roots

Too small ‐ slow

Page 16: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

16

Incremental Search

K. Webb  MAE 4020/5020

has three roots on  , Incremental search with increment length, Δ

Closely‐spaced roots are missed entirely

A root is detected

Location only known to within Δ

Δ

Page 17: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

17

Bisection

K. Webb  MAE 4020/5020

Search initialized with bracketing values Current root estimate,  , , is the midpoint of the current interval

,, ,

2 At each iteration root estimate replaces upper or lower bracketing value

,, , ∙ , 0

, , ∙ , 0

,, , ∙ , 0

, , ∙ , 0

Page 18: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

18

Bisection

K. Webb  MAE 4020/5020

At each iteration: Root estimate 

midpoint of bracketing  interval

New bracketing interval sub‐interval containing the sign change

Chapra

Page 19: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

19

Bisection – Absolute Error

K. Webb  MAE 4020/5020

Absolute error is bounded by the bracketing interval

,Δ2

, ,

2

Bracketing interval halved at each iteration Max absolute error halved each iteration. After  iterations:

,Δ2

Can calculate required iterations for a specified maximum absolute error:

logΔ

1

Page 20: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

20

False Position – Linear Inerpolation

K. Webb  MAE 4020/5020

Similar to bisection, but root estimate calculated differently Not the midpoint of the bracketing interval , is the root of the line connecting  , and  ,

Page 21: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

21

False Position – Calculating 

K. Webb  MAE 4020/5020

Slope of the line:

ΔΔ

, ,

, ,

From  , to zero:

Δ ,

From , to  , :

ΔΔΔ ∙ ,

The root estimate is:

, , Δ →

Δ ,

Δ , ,

, , ,, ,

, ,

Page 22: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

22

Bracketing Methods ‐ Summary

K. Webb  MAE 4020/5020

All methods require two initial values that bracket the root

Always convergent

Incremental search Mostly for illustrative purposes – not recommended 

Bisection Predictable Can calculate required iterations for desired absolute error ‐predictable

False position – linear interpolation Often outperforms bisection May be slow for certain types of functions

Page 23: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

23Exercise

Write a MATLAB function to find a root of a mathematical function using the false position method Function syntax:

[xr,fxr,epsa,iter] = fproot(func,xl,xu,reltol,maxiter)

Use your function to determine the required length of the fin from the example at the beginning of this section – i.e., find L, such that

∙sinh cosh

cosh sinh500

where

,    

∙ ,      2 2

Shell MATLAB files are available for download on the course website.

Exercise – False Position Function

K. Webb  MAE 4020/5020

Page 24: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

Root Finding: Open Methods24

K. Webb  MAE 4020/5020

Page 25: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

25

Root Finding – Open Methods

K. Webb  MAE 4020/5020

May require only a single initial value If two initial values are required, they need not bracket the root

Often significantly faster than bracketing methods Convergence is not guaranteed

Dependent on function and initial values

Fixed‐point iteration Newton‐Raphson Secant methods Inverse quadratic interpolation

Page 26: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

26

Fixed Point Iteration

K. Webb  MAE 4020/5020

A fixed point of a function is a value of the independent variable that the function maps to itself

Root‐finding problem is determining  , such that 

Can add  to both sides – equation is unchanged

Value of  that satisfies the equation is still the root

Page 27: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

27

Fixed Point Iteration

K. Webb  MAE 4020/5020

Root is the solution to

A fixed point of  Also the solution to system of two equations

Root is the intersection of and  i.e., the intersection of 

and Chapra

Page 28: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

28

Fixed Point Iteration

K. Webb  MAE 4020/5020

Provides an iterative formula for  :

Iterate until approximate error falls below a specified stopping criterion

Chapra

Page 29: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

29

Fixed Point Iteration – Convergence 

K. Webb  MAE 4020/5020

Current error is proportional to the previous error times the slope of  :

, ∙ ,

If  1, error will grow Estimate will diverge

If  1, error will decrease Estimate will converge

If  0, sign of error will oscillate Oscillatory, or spiral convergence 

or divergence Chapra

Page 30: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

30

Fixed Point Iteration – Rate of Convergence 

K. Webb  MAE 4020/5020

Current error is proportional to the previous error times the slope of  :

, ,

Once a convergent estimate becomes relatively close to the root, the slope of  is relatively constant varies little from iteration to iteration

Error of the current iteration is roughly proportional to the error from the previous iteration Linear convergence

Page 31: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

31

Newton‐Raphson Method

K. Webb  MAE 4020/5020

New estimate is the root of a line tangent to  at  ,

Slope of  at  , is the derivative at  , :

,ΔΔ

,

, ,

Solving for the new root estimate:

, ,,

,

An iterative formula for 

Page 32: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

32

Newton‐Raphson Method

K. Webb  MAE 4020/5020

Iterate, using the Newton‐Raphson formula:

, ,,

,

Iterate until approximate error falls below a specified stopping criterion

Page 33: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

33

Newton‐Raphson – Convergence

K. Webb  MAE 4020/5020

Often fast, but convergence is not guaranteed

Inflection point (constant slope) near a root causes divergence

Areas of near‐zero slope are problematic Oscillation around local maximum/minimum

Tangent line sends estimate very far away – or to infinity for zero slope

Chapra

Page 34: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

34

Newton‐Raphson – Rate of Convergence

K. Webb  MAE 4020/5020

Current error is proportional to the square of the previous error

, 2 ,

Quadratic convergence Number of significant figures of 

accuracy approximately doubles each iteration

Page 35: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

35

Newton‐Raphson – Derivative Function

K. Webb  MAE 4020/5020

Newton‐Raphson algorithm requires two functions

, ,,

,

Function whose roots are to be found,  Derivative function, 

That means  must be found analytically Inconvenient – may be tedious for some functions

Already performing numerical approximationsWhy not calculate  numerically?  Secant methods

Page 36: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

36

Secant Methods

K. Webb  MAE 4020/5020

Same iterative formula as Newton‐Raphson:

, ,,

,

Now, approximate  using a finite difference

Secant method iterative formula:

, ,,

Would require two initial values

Instead, generate the second value as a fractional perturbation of the first (the current estimate)

, ,

where  is a very small number Finite difference approx. of  :

≅ , , ,

,

The modified secant iterative formula:

, ,, ∙ ,

, , ,

Page 37: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

37

Root‐Finding Methods – Interpolation 

K. Webb  MAE 4020/5020

False position and the Newton‐Raphson/secant methods all use linear interpolation Non‐linear function approximated as a linear function Root of the linear approximation becomes the approximation of the root

We’ll get to curve‐fitting and interpolation later, but we should already suspect that a higher‐order approximation for a non‐linear function may be more accurate than a linear (first‐order) approximation

Increase accuracy of the root estimate by approximating our non‐linear function as a quadratic

Page 38: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

38

Inverse Quadratic Interpolation

K. Webb  MAE 4020/5020

Instead of using two points to approximate  as a line, use three points to approximate it as a parabola

Root estimate is where the parabola crosses the x‐axis

But, not all parabolas cross the x‐axis – complex roots

All parabolas do cross the y‐axis To guarantee an x‐axis crossing, turn the parabola on its side

An inverse quadratic function

Page 39: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

39

Inverse Quadratic Interpolation – Example

K. Webb  MAE 4020/5020

Three points required for quadratic approx. How are they chosen?

Inverse quadratic function will cross the x‐axis For same three points a quadratic may not

May be very efficient May not converge

Page 40: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

40

Inverse Quadratic Interpolation

K. Webb  MAE 4020/5020

Three known  and corresponding  values: ,  ,  , and  ,  , 

Fit an inverse parabola to these three points  Lagrange polynomial – more on these later

Don’t actually need to calculate this parabola Only need its root – evaluate at  0 for new root estimate:

,

Page 41: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

41

Inverse Quadratic Interpolation

K. Webb  MAE 4020/5020

Determining  , from the three points is only part of the algorithm Algorithm initialized with one or two  values Need to determine the other one or two initial  values

Must update  ,  , and  on each iteration We won’t get into these details here

Will fail if any two  are equal Revert to another open method (e.g. secant)

May diverge Revert to a bracketing method (e.g. bisection)

Page 42: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

42

Brent’s Method – fzero(…)

K. Webb  MAE 4020/5020

MATLAB’s fzero(…) is based on Brent’s method Bracketing points either specified or determined from a single point If one point is specified, search in both directions for a sign change, using increasingly larger increments

Use a point beyond the sign change as the second bracketing value

Use inverse quadratic interpolation to generate root estimates when possible

In case of convergence issues revert to bisection Always try faster method first, then use bisection only if necessary

Page 43: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

43

fzero(…)

K. Webb  MAE 4020/5020

xr = fzero(fhandle,x0,options)

xr is the root value returned fhandle is a function handle x0 is an initial value for x or a vector of two values that must bracketing the root

options is a structure created with MATLAB’s optimset(…) function

options = optimset(‘param’,‘value’,…)

See help for more information on optimset(…)

Page 44: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

44

Example – fzero(…)

K. Webb  MAE 4020/5020

Returning to our heat sink fin design problem Want to know the length of the fin required for a heat transfer rate of  500 , given the other specified parameters:

Width:  w = 1 cm Thickness:  t = 2 mm Heat transfer coeff.:          

h = 100 W/(m2K) Aluminum:  k = 210 W/(m∙K) Ambient temperature:  

40° Base temperature:  

100°

Page 45: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

45

Example – fzero(…)

K. Webb  MAE 4020/5020

We’ll now use fzero(…) to find the root of 

∙sinh cosh

cosh sinh500 0

where

,     ⋅

∙ ,      2 2

Page 46: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

46

Example – fzero(…)

K. Webb  MAE 4020/5020

Define the function whose root we want to find

Use optimset(…) to turn on display of results at each iteration

Initialize x to a value in the vicinity of the root – 4cm, here Pass the function handle, initial guess, and options structure to fzero

Page 47: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

47

Example – fzero(…)

K. Webb  MAE 4020/5020

First 14 iterations are searching for a sign change

Remaining iterations refine the root estimate

Only interpolation used in this case –bisection not necessary

Root is at 0.0311 A  fin

Page 48: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

48

Roots of Polynomials

K. Webb  MAE 4020/5020

Polynomials are linear (first order) or nonlinear (second and higher order) functions of the form

An nth‐order polynomial has n roots Often, we’d like to find all n roots at onceMethods described thus far find only one root at a time

For 2nd‐order,the quadratic formula yields both roots at once:

Page 49: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

49

Roots of Polynomials – roots(…)

K. Webb  MAE 4020/5020

To find all n roots of a polynomial:

x = roots(c)

x is an n x 1  column vector of roots c is an (n+1)‐vector of polynomial coefficients, i.e. the 

’s from the previous slide:

roots(…) works by treating the root‐finding problem as an eigenvalue problem More on eigenvalues later in the course

Page 50: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

50

Roots of Polynomials– poly(…)

K. Webb  MAE 4020/5020

Polynomials are a very important class of functions Curve‐fitting and interpolation Linear system theory and controls

Often we may want to generate the nth‐order polynomial corresponding to a given set of n roots

c = poly(x)

c is a 1 x (n+1) row vector of polynomial coefficients x is an n‐vector of roots

Page 51: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

Optimization51

K. Webb  MAE 4020/5020

Page 52: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

52

Optimization

K. Webb  MAE 4020/5020

Optimization is very important to engineers Adjusting parameters to maximize some measure of performance of a system

Process of finding maxima and minima (optima) of functions

Chapra

Page 53: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

53

Maxima and Minima

K. Webb  MAE 4020/5020

An optimum point of a function occurs where the first derivative (slope) of the function is zero

An optimum point is a maximum if the second derivative (curvature) of the function is negative

An optimum point is a minimum if the second derivative (curvature) of the function is positive

Page 54: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

54

Optimization as a Root‐Finding Problem

K. Webb  MAE 4020/5020

Optima occur where  Could find optima of  by finding roots of 

Requires calculation of the derivative, either analytically or numerically

Direct (non‐derivative) methods are often faster and more reliable

Page 55: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

55

Optimization

K. Webb  MAE 4020/5020

Optimization methods exist for one‐dimensionaland multi‐dimensional functions

As with root‐finding, both bracketing and openmethods exist

Here, we’ll look at: One dimensional optimization Golden‐section search Parabolic interpolation Use of MATLAB’s fminbnd(…)

Multi‐dimensional optimization Use of MATLAB’s fminsearch(…)

Page 56: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

One‐Dimensional Optimization56

K. Webb  MAE 4020/5020

Page 57: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

57

The Golden Ratio –

K. Webb  MAE 4020/5020

Divide a value into two parts,  and   ,

such that the ratio of the larger part to the smaller part is equal to the ratio of the whole to the larger part

The ratio  is the golden ratio

Page 58: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

58

The Golden Ratio –

K. Webb  MAE 4020/5020

Given an interval  , , subdivide it from both ends according to the golden ratio

and

If we discard the upper portion of the interval

we‘re left with a smaller interval, itself divided according to 

The same is true if we discard the lower subinterval 

Page 59: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

59

The Golden Ratio –

K. Webb  MAE 4020/5020

Starting from one of the subintervals (the lower one, here)

we can further subdivide it according to the golden ratio, starting from the upper bound on the interval

If we reassign the variable names

→ ,→ ,→ ,→ ,

we‘re back where we started

But now, the overall interval size has been reduced by a factor of 

This process is the basis for the golden‐section search algorithm

Page 60: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

60

Golden‐Section Search 

K. Webb  MAE 4020/5020

A bracketing optimization method Two initial values must bracket an optimum point

Looks for a minimum To find a maximum use 

Only one minimum point (local or global) in the bracketing interval Unimodal

Very similar to bisection Now looking for a minimum, instead of a zero‐crossing Need two intermediate points

Page 61: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

61

Golden‐Section Search 

K. Webb  MAE 4020/5020

Start with two initial values, and , that bracket a minimum point 

of the function, 

Subdivide the interval according to the golden ratio with two intermediate points  and 

Evaluate the function at each of the intermediate points

and 

Compare values of  and  Two possibilities

or

Page 62: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

62

Golden‐Section Search –

K. Webb  MAE 4020/5020

If 

is the current estimate for the minimum point of  , 

True minimum cannot lie in the range of  ,

Discard the lower subinterval Reassign variable names

→→→

Using new  ,  , and  values, calculate a new 

Page 63: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

63

Golden‐Section Search –

K. Webb  MAE 4020/5020

If 

is the current estimate for the minimum point of  , 

True minimum cannot lie in the range of  ,

Discard the upper subinterval Reassign variable names

→→→

Using new  ,  , and  values, calculate a new 

Page 64: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

64

Golden‐Section Search

K. Webb  MAE 4020/5020

Continue iterating and updating the  , the estimate of the minimizing value for  Only one new point needs to be calculated at each iteration This is the beauty of using the golden ratio Very efficient

Size of the bracketing interval decreases by a factor of  with each iteration

Continue to iterate until error estimate satisfies a stopping criterion

Page 65: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

65

Golden‐Section Search – Error 

K. Webb  MAE 4020/5020

Consider the case where  Lower subinterval,  , , is 

discarded Optimum point estimate is 

This scenario represent the worst‐case error

11

and 1

1

Page 66: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

66

Golden‐Section Search – Error 

K. Webb  MAE 4020/5020

The worst‐case error is

2

Normalize to the current estimate  Convert from absolute to relative 

error

Use worst‐case value as our approximate error

2 ∙ 100%

Calculate  each iteration Continue until stopping criterion is 

satisfied

Page 67: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

67

Parabolic Interpolation

K. Webb  MAE 4020/5020

Near an optimum point, many functions can be satisfactorily approximated with a quadratic

Three points define a unique parabola Two points define the bracketing interval A third intermediate point somewhere within the bracket

Optimum point of the parabolic approximation becomes current estimate of the optimum point

Evaluate  at 

Retain the subinterval containing the optimum point, discard one of the bracketing points, and iterate

must be unimodal Looking for a minimum, but algorithm can easily be modified to look for a 

maximum

Page 68: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

68

Parabolic Interpolation

K. Webb  MAE 4020/5020

Start with three points, which bracket the optimum

Evaluate the  at these points

Fit a parabola to the three points Can use a Lagrange polynomial

Not necessary to actually calculate the parabola – can jump to finding its optimum point

Page 69: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

69

Parabolic Interpolation

K. Webb  MAE 4020/5020

Expression for  derived by solving  0

becomes the current estimate for the optimum point, 

Evaluate  ) Use values of  and  )

to appropriately reduce the bracketing interval

Calculate the optimum point of the parabolic approximation12 ∙

Page 70: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

70

Parabolic Interpolation – Reducing the Bracket

K. Webb  MAE 4020/5020

If  If  (shown here)

is in the lower subinterval Discard the upper subinterval

, ,

, ,

, ,

If  is in the upper subinterval Discard the lower subinterval

, ,

, ,

, ,

Page 71: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

71

Parabolic Interpolation – Reducing the Bracket

K. Webb  MAE 4020/5020

If  If  (shown here)

is in the upper subinterval Discard the lower subinterval

, ,

, ,

, ,

If  is in the lower subinterval Discard the upper subinterval

, ,

, ,

, ,

Page 72: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

72

Parabolic Interpolation – Finding a Maximum

K. Webb  MAE 4020/5020

Can  also use parabolic interpolation to locate a maximum point Parabola fit to the three points may open up or down

Need to adjust bracket reduction algorithm depending on whether a maximum or minimum point is sought

Page 73: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

73

One‐Dimensional Optimization – fminbnd(…)

K. Webb  MAE 4020/5020

Parabolic interpolation is efficient, but may not converge fminbnd uses a parabolic interpolation when possible and golden‐section search when necessary

Finds the minimum of a function over an interval

[xmin,fmin] = fminbnd(f,x1,x2,options)

f is a function handle x1 and x2 are initial values that must bracket a single minimum point

options is a structure generated with optimset(…) xmin is the optimum point of  fmin is the minimum value of  ,

Page 74: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

74

One‐Dimensional Optimization – Example

K. Webb  MAE 4020/5020

Determine the load resistance of an electrical circuit that maximizes power delivered to the load Normalize to source resistance and open‐circuit voltage 1Ω,  1

Power delivered to the load is

Determine  to maximize 

Page 75: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

75

One‐Dimensional Optimization – Example

K. Webb  MAE 4020/5020

Max Power occurs at

1 →

Negate function to find maximum

Page 76: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

76

Multi‐Dimensional Optimization – fminsearch(…)

K. Webb  MAE 4020/5020

Find the minimum of a function of two or more variables fminsearch uses a direct, non‐gradient, method –derivatives are not calculated

[xmin,fmin] = fminsearch(f,x0,options)

f is a function handle x0 is a vector of initial values 

Best initial guess for the optimum point options is a structure generated with optimset(…)

xmin is the optimum point of  ‐ a vector fmin is the minimum value of  ,

Page 77: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

77

Multi‐Dimensional Optimization – Example

K. Webb  MAE 4020/5020

Find the minimum of a function of two variables

Page 78: Section 5 Root Finding and Optimizationweb.engr.oregonstate.edu › ~webbky › MAE4020_5020_files...Chapra. 3 Root Finding K. Webb MAE 4020/5020. 4 Root Finding ‐Example K. Webb

78

Multi‐Dimensional Optimization – Example

K. Webb  MAE 4020/5020

Convergence for this example depends on choice of