CH 06 Slides 4 Up
CH 06 Slides 4 Up
CH 06 Slides 4 Up
Gerald W. Recktenwald
Department of Mechanical Engineering These slides are a supplement to the book Numerical Methods with Matlab:
Portland State University Implementations and Applications, by Gerald W. Recktenwald,
c 2000–2006, Prentice-Hall,
c 2000–2006 Gerald W. Recktenwald.
Upper Saddle River, NJ. These slides are copyright
[email protected] The PDF version of these slides may be downloaded or stored or printed for noncommercial,
educational use. The repackaging or sale of these slides in any form, without written
consent of the author, is prohibited.
The latest version of this PDF file, along with other supplemental material for the book,
can be found at www.prenhall.com/recktenwald or web.cecs.pdx.edu/~gerry/nmm/.
Topics covered in this chapter Computing the dimensions of a picnic table leg involves a root-finding problem.
Leg assembly Detail of one leg
• Preliminary considerations and bracketing.
• Fixed Point Iteration d1
b c a
• Bisection
b2
• Newton’s Method
h
• The Secant Method d2 α
b θ α
θ
• Hybrid Methods: the built in fzero function 2α
d1
• Roots of Polynomials d2 d2
NMM: Finding the Roots of f (x) = 0 page 2 NMM: Finding the Roots of f (x) = 0 page 3
Example: Picnic Table Leg Roots of f (x) = 0
Dimensions of a the picnic table leg satisfy Any function of one variable can be put in the form f (x) = 0.
Given overall dimensions w and h, and the material dimension, b, what is the value of θ ? 1
cos(x) = x,
An analytical solution for θ = f (w, h, b) exists, but is not obvious. 0.5
-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4
NMM: Finding the Roots of f (x) = 0 page 4 NMM: Finding the Roots of f (x) = 0 page 5
NMM: Finding the Roots of f (x) = 0 page 6 NMM: Finding the Roots of f (x) = 0 page 7
Bracketing Bracketing Algorithm (1)
NMM: Finding the Roots of f (x) = 0 page 8 NMM: Finding the Roots of f (x) = 0 page 9
A simple test for sign change: f (a) × f (b) < 0 ? A better test uses the built-in sign function
or in Matlab fa = ...
fb = ...
if if sign(fa)~=sign(fb)
fa = ... save bracket
fb = ... end
if fa*fb < 0
save bracket
end
See implementation in the brackPlot function
but this test is susceptible to underflow.
NMM: Finding the Roots of f (x) = 0 page 10 NMM: Finding the Roots of f (x) = 0 page 11
The brackPlot Function Apply brackPlot Function to sin(x) (1)
-1
where
-10 -5 0 5 10
x
myFun is the name of an m-file that evaluates f (x)
xmin, xmax define range of x axis to search
nx is the number of subintervals on [xmin,xmax] used to
check for sign changes of f (x). Default: nx= 20
NMM: Finding the Roots of f (x) = 0 page 12 NMM: Finding the Roots of f (x) = 0 page 13
Apply brackPlot to a user-defined Function (1) Apply brackPlot to a user-defined Function (2)
To solve 1.5
1/3
f (x) = x − x − 2 = 0 >> Xb = brackPlot(’fx3’,0,5)
Xb = 1
we need an m-file function to evaluate f (x) for any scalar or vector of x values. 3.4211 3.6842
0.5
-1
function f = fx3(x) -1.5
% fx3 Evaluates f(x) = x - x^(1/3) - 2
f = x - x.^(1/3) - 2; -2
-2.5
0 1 2 3 4 5
x
Run brackPlot with fx3 as the input function
>> brackPlot(’fx3’,0,5)
ans =
3.4000 3.6000
NMM: Finding the Roots of f (x) = 0 page 14 NMM: Finding the Roots of f (x) = 0 page 15
Apply brackPlot to a user-defined Function (3) Root-Finding Algorithms
Instead of creating a separate m-file, we can use an in-line function object. We now proceed to develop the following root-finding algorithms:
These algorithms are applied after initial guesses at the root(s) are identified with
Note: When an inline function object is supplied to brackPlot, the name of the bracketing (or guesswork).
object is not surrounded in quotes:
NMM: Finding the Roots of f (x) = 0 page 16 NMM: Finding the Roots of f (x) = 0 page 17
Fixed point iteration is a simple method. It only works when the iteration function is An automatic root-finding procedure needs to monitor progress toward the root and stop
convergent. when current guess is close enough to the desired root.
NMM: Finding the Roots of f (x) = 0 page 18 NMM: Finding the Roots of f (x) = 0 page 19
Fixed Point Iteration Example (1) Fixed Point Iteration Example (2)
NMM: Finding the Roots of f (x) = 0 page 20 NMM: Finding the Roots of f (x) = 0 page 21
Given a bracketed root, halve the interval while continuing to bracket the root For the bracket interval [a, b] the midpoint is
1
xm = (a + b)
f (x1) 2
f (b1) A better formula, one that is less susceptible to round-off is
b−a
xm = a +
a x2 x1 b 2
f (a1)
NMM: Finding the Roots of f (x) = 0 page 22 NMM: Finding the Roots of f (x) = 0 page 23
Bisection Algorithm Bisection Example
NMM: Finding the Roots of f (x) = 0 page 24 NMM: Finding the Roots of f (x) = 0 page 25
„ «n
Let δn be the size of the bracketing interval at the nth stage of bisection. Then δn 1 δn
„ «
−n
= =2 or n = log2
δ0 2 δ0
NMM: Finding the Roots of f (x) = 0 page 26 NMM: Finding the Roots of f (x) = 0 page 27
Convergence Criteria Convergence Criteria on x
An automatic root-finding procedure needs to monitor progress toward the root and stop f (x)
when current guess is close enough to the desired root. true root
˛ ˛
tolerance Absolute tolerance: ˛xk − xk−1˛ < δx
• Convergence checking will avoid searching to unnecessary accuracy. on f (x) x
˛ ˛
• Check whether successive approximations are close enough to be considered the same: ˛x − x
˛ k
˛
k−1 ˛
tolerance Relative tolerance: ˛ ˛ < δ̂x
on x ˛ b−a ˛
|xk − xk−1| < δx
• Check whether f (x) is close enough zero. xk = current guess at the root
xk−1 = previous guess at the root
|f (xk )| < δf
NMM: Finding the Roots of f (x) = 0 page 28 NMM: Finding the Roots of f (x) = 0 page 29
f (x) If f ′(x) is small near the root, it is easy If f ′(x) is large near the root, it is
˛ ˛
true root Absolute tolerance: ˛f (xk )˛ < δf to satisfy a tolerance on f (x) for a large possible to satisfy a tolerance on ∆x
range of ∆x. A tolerance on ∆x is more when |f (x)| is still large. A tolerance
tolerance
on f (x) Relative tolerance: conservative. on f (x) is more conservative.
x
n o
tolerance
|f (xk )| < δ̂f max |f (a0)|, |f (b0)| f (x) f (x)
on x
NMM: Finding the Roots of f (x) = 0 page 30 NMM: Finding the Roots of f (x) = 0 page 31
Newton’s Method (1) Newton’s Method (2)
f(x1)
Substitute ∆x = xk+1 − xk and neglect second order terms to get
′
f (xk+1) ≈ f (xk ) + (xk+1 − xk ) f (xk )
x2 x3 x1 where ˛
f(x2) ′ df ˛˛
f (xk ) =
dx ˛xk
NMM: Finding the Roots of f (x) = 0 page 32 NMM: Finding the Roots of f (x) = 0 page 33
NMM: Finding the Roots of f (x) = 0 page 34 NMM: Finding the Roots of f (x) = 0 page 35
Newton’s Method Example (1) Newton’s Method Example (2)
Solve: Conclusion
1/3 1/3
x−x −2=0 xk − xk −2
xk+1 = xk − • Newton’s method converges
−2/3
First derivative is 1 − 13 xk much more quickly than
′ 1 −2/3
f (x) = 1 − x bisection
3 k xk f ′ (xk ) f (x)
The iteration formula is • Newton’s method requires an
1/3
xk − xk − 2 0 3 0.83975005 -0.44224957 analytical formula for f ′(x)
xk+1 = xk − −2/3 1 3.52664429 0.85612976 0.00450679
1 − 13 xk • The algorithm is simple as long
2 3.52138015 0.85598641 3.771 × 10−7 as f ′(x) is available.
3 3.52137971 0.85598640 2.664 × 10−15
• Iterations are not guaranteed to
4 3.52137971 0.85598640 0.0 stay inside an ordinal bracket.
NMM: Finding the Roots of f (x) = 0 page 36 NMM: Finding the Roots of f (x) = 0 page 37
Since Given two guesses xk−1 and xk , the next guess at the root is where the line through
f '(x1) ≈ 0 f (xk−1) and f (xk ) crosses the x axis.
f (xk )
f(x1) xk+1 = xk −
f ′(xk )
f(b)
the new guess, xk+1, will be far from
the old guess whenever f ′(xk ) ≈ 0
x1
f(x1)
x2 a x1 b
f(a)
NMM: Finding the Roots of f (x) = 0 page 38 NMM: Finding the Roots of f (x) = 0 page 39
Secant Method (2) Secant Method (3)
NMM: Finding the Roots of f (x) = 0 page 40 NMM: Finding the Roots of f (x) = 0 page 41
NMM: Finding the Roots of f (x) = 0 page 42 NMM: Finding the Roots of f (x) = 0 page 43
Divergence of Secant Method Summary of Basic Root-finding Methods
NMM: Finding the Roots of f (x) = 0 page 44 NMM: Finding the Roots of f (x) = 0 page 45
20
fzero is a hybrid method that combines bisection, secant and reverse quadratic
interpolation Find the point where the x
axis intersects the sideways 15
Syntax: parabola passing through
three pairs of (x, f (x))
r = fzero(’fun’,x0)
values. 10
r = fzero(’fun’,x0,options)
r = fzero(’fun’,x0,options,arg1,arg2,...)
5
x0 can be a scalar or a two element vector
0
• If x0 is a scalar, fzero tries to create its own bracket.
• If x0 is a two element vector, fzero uses the vector as a bracket.
−5
0 0.5 1 1.5 2
NMM: Finding the Roots of f (x) = 0 page 46 NMM: Finding the Roots of f (x) = 0 page 47
fzero Function (2) fzero Function (3)
Optional parameters to control fzero are specified with the optimset function.
fzero chooses next root as
Examples:
• Result of reverse quadratic interpolation (RQI) if that result is inside the current Tell fzero to display the results of each step:
bracket. >> options = optimset(’Display’,’iter’);
• Result of secant step if RQI fails, and if the result of secant method is in inside the >> x = fzero(’myFun’,x0,options)
current bracket.
• Result of bisection step if both RQI and secant method fail to produce guesses inside Tell fzero to use a relative tolerance of 5 × 10−9:
the current bracket. >> options = optimset(’TolX’,5e-9);
>> x = fzero(’myFun’,x0,options)
Tell fzero to suppress all printed output, and use a relative tolerance of 5 × 10−4:
>> options = optimset(’Display’,’off’,’TolX’,5e-4);
>> x = fzero(’myFun’,x0,options)
NMM: Finding the Roots of f (x) = 0 page 48 NMM: Finding the Roots of f (x) = 0 page 49
y = f(x)
’off’ Suppress all print out
perturbations in the 1
’TolX’ tol Iterate until polynomial coefficients f1(x) f2(x) f3(x)
|∆x| < max [tol, tol ∗ a, tol ∗ b] (conditioning).
0
where ∆x = (b−a)/2, and [a, b] is the current bracket.
distinct repeated complex
real roots real roots roots
-1
The default values of ’Display’ and ’TolX’ are equivalent to 0 2 4 6 8 10
x (arbitrary units)
options = optimset(’Display’,’iter’,’TolX’,eps)
NMM: Finding the Roots of f (x) = 0 page 50 NMM: Finding the Roots of f (x) = 0 page 51
Algorithms for Finding Polynomial Roots roots Function (1)
NMM: Finding the Roots of f (x) = 0 page 52 NMM: Finding the Roots of f (x) = 0 page 53
NMM: Finding the Roots of f (x) = 0 page 54 NMM: Finding the Roots of f (x) = 0 page 55
roots Examples