section 5 root finding and optimizationweb.engr.oregonstate.edu › ~webbky ›...
TRANSCRIPT
MAE 4020/5020 – Numerical Methods with MATLAB
SECTION 5: ROOT FINDING AND OPTIMIZATION
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
Root Finding3
K. Webb MAE 4020/5020
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°
5
Root Finding ‐ Example
K. Webb MAE 4020/5020
Fin heat transfer rate is given by:
∙sinh cosh
cosh sinh
where
,
,
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
7
Root Finding ‐ Example
K. Webb MAE 4020/5020
Looking for such that
8
Root Finding ‐ Example
K. Webb MAE 4020/5020
Find the root of , i.e. such that
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
Root Finding: Basic Concepts10
K. Webb MAE 4020/5020
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
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
Root Finding: Bracketing Methods13
K. Webb MAE 4020/5020
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
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
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 Δ
Δ
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
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
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
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 ,
21
False Position – Calculating
K. Webb MAE 4020/5020
Slope of the line:
ΔΔ
, ,
, ,
From , to zero:
Δ ,
From , to , :
ΔΔΔ ∙ ,
The root estimate is:
, , Δ →
Δ ,
Δ , ,
, , ,, ,
, ,
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
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
Root Finding: Open Methods24
K. Webb MAE 4020/5020
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
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
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
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
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
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
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
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
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
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
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
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:
, ,, ∙ ,
, , ,
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
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
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
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:
,
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)
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
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(…)
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°
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
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
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
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:
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
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
Optimization51
K. Webb MAE 4020/5020
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
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
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
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(…)
One‐Dimensional Optimization56
K. Webb MAE 4020/5020
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
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
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
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
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
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
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
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
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
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
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
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
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 ∙
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
, ,
, ,
, ,
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
, ,
, ,
, ,
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
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 ,
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
75
One‐Dimensional Optimization – Example
K. Webb MAE 4020/5020
Max Power occurs at
1 →
Negate function to find maximum
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 ,
77
Multi‐Dimensional Optimization – Example
K. Webb MAE 4020/5020
Find the minimum of a function of two variables
78
Multi‐Dimensional Optimization – Example
K. Webb MAE 4020/5020
Convergence for this example depends on choice of