Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

1

MECE 390-A1
Numerical Methods of Mechanical Engineering

Chapter 2. Root finding


Lecture 3. 2018/09/11

Department of Mechanical Engineering, University of Alberta

MEC E 390 -A1


2

Objective

1. Roots of functions
2. Iterative root finding schemes
3. Bisection methods
4. Condition for root bracketing

MEC E 390 -A1


Roots of functions 3

Many engineering and design problems involve finding roots (or zeros) of equations.
• 𝑦 = 𝑓 𝑥 satisfies 𝑓 𝑥𝑟𝑜𝑜𝑡 = 0 then 𝑥𝑟𝑜𝑜𝑡 is the solution of the equation 𝑓 𝑥 = 0.

Example: a linear function connecting, say, (𝑥1 , 𝑦1 ) and (𝑥2 , 𝑦2 )

𝒚 𝑦 = 𝑓 𝑥 = 𝑚𝑥 + 𝑏. Since the two points are give, we obtain

𝑦2 − 𝑦1
(𝑥2 , 𝑦2 ) 𝑦= 𝑥 − 𝑥1 + 𝑦1
𝑥2 − 𝑥1
𝒙 𝑦 −𝑦
(𝑥1 , 𝑦1 ) where 𝑚 = 𝑥2−𝑥1 is the slope of 𝑦.
2 1

When 𝑦 = 0 one can rearrange the above to show that


𝑥2 − 𝑥1
𝑥 = 𝑥1 − 𝑦1 ,
𝑦2 − 𝑦1
which is the solution (root) .

MEC E 390 -A1


Roots of functions 4

Likewise, if 𝑦 = 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0, the roots are given by the quadratic formula


 Roots of a high order function

𝑦 = 2𝑥 2 + 3𝑥 − 7

> 0  2 real roots


Three cases: 𝑏2 − 4𝑎𝑐 = 0  2 equal roots
< 0  2 complex conjugate roots Run Matlab code
Therefore, the root are completely determined. Lecture3_ploy.m

• Unfortunately, the associated equation for cubic polynomials is a real mass and no such
equation exists for polynomials of order > 3.
• Similar ambiguities arise when trying to find the roots of e.g. 𝑦 = tan(ln(𝑥 2 )).
Q. What to do ?
 Use data cursor… OR more elegant solution  Use an iterative root finding schemes.
NOTE: In this course, we confine our analysis only to the cases with real roots.

MEC E 390 -A1


Iterative root finding schemes 5

 Classification
1. Bracketing method
2. Open method

 Bracketing methods
• In general, two initial points are used which bracket
𝑓 𝑥𝑈 root.
f(x) f(x)
• The function need to change the sign within this
interval.
xL root • Then the bracketing interval is reduced while
xu x continuing to bracket the root until the interval is
small enough.
𝑓 𝑥𝐿
𝑥𝐿 : lower bound, 𝑥𝑢 : upper bound

For Bracketing method, we will study two types of methods: 1) Bisection Method
2) False position method
MEC E 390 -A1
Bisection method 6

 When we define a bracket, three situations may occur


1. If the function changes sign between 𝑥𝐿 and 𝑥𝑈 :
interval [𝑥𝐿 , 𝑥𝑈 ] has 1,3, 5… roots
2. If function does not change sign between 𝑥𝐿 and 𝑥𝑈 : 𝑓𝑈
f(x)
interval [𝑥𝐿 , 𝑥𝑈 ] has 0,2, 4… roots
3. If either or both 𝑓(𝑥𝐿 ) = 0 or 𝑓(𝑥𝑢 ) = 0 : 𝑓𝑟

𝑥𝐿 and/or 𝑥𝑈 are roots (rarely happens) 𝑥𝐿 𝑥𝑈


𝑥𝑟 x

 Bisection method procedures 𝑓𝐿


a. Chose 𝑥𝐿 and 𝑥𝑈 so that the root is bracketed (i.e. 𝑓(𝑥𝐿 )𝑓(𝑥𝑈 ) < 0)
1
b. At the midpoint 𝑥𝑟 = 2 𝑥𝐿 + 𝑥𝑈 find 𝑓(𝑥𝑟 ).
c. Check whether 𝑓(𝑥𝑟 )𝑓(𝑥𝑈 ) < 0 if yes, set 𝑥𝑟 = 𝑥𝐿 ; if no set 𝑥𝑟 = 𝑥𝑈 . This means
 discarding the half of the bracket that dose not contain the root
1
NOTE: one may also use 𝑥𝑟 = 𝑥𝐿 + 2 𝑥𝑈 − 𝑥𝐿 : less round-off error!

MEC E 390 -A1


Bisection Method 7

 Bisection Pseudocode
Input: 𝑥𝐿 , 𝑥𝑈 , 𝑚𝑎𝑥𝑖𝑡, 𝜀 𝜀= tolerance, 𝑚𝑎𝑥𝑖𝑡: # of iteration
Check condition: [𝑥𝐿 , 𝑥𝑈 ] bracket a root
𝑓𝐿 = 𝑓(𝑥𝐿 ); 𝑓𝑈 = 𝑓(𝑥𝑈 )
f(x)
𝑓𝑈
Count = 0 ; always initialized counter before loop 𝑓𝑟
𝑎 = 𝑥𝐿
While (not converged) and count < 𝑚𝑎𝑥𝑖𝑡 𝑓𝑟
𝑓𝑟 𝑥𝑟 b = 𝑥𝐿
Count = count + 1
𝑓𝑟 x
𝑥𝑟 = 𝑥𝐿 + 𝑥𝑈 /2
𝑓𝑟 = 𝑓(𝑥𝑟 )
If 𝑓𝑟 𝑓𝑈 < 0 ,alternatively use 𝑠𝑖𝑔𝑛(𝑓𝑟 ) ≠ 𝑠𝑖𝑔𝑛(𝑓𝑈 )
𝑓𝐿 X
𝑥𝑟
𝑥𝐿 = 𝑥𝑟 ; 𝑓𝐿 = 𝑓𝑟 𝑥𝐿 X 𝑥𝑈
else
𝑥𝑈 = 𝑥𝑟 ; 𝑓𝑈 = 𝑓𝑟
𝑥𝐿
𝑥
X 𝑥𝑈
𝑟
end 𝑥𝐿 𝑥𝑈
estimated error 𝑥𝑟
End; print 𝑥𝑟 , 𝑓𝑟 , count; STOP
MEC E 390 -A1
Matlab code 8

Define function

Bisection method Algorithm

Convergence criteria:
Either equal to zero or smaller than the
tolerance
MEC E 390 -A1
Example 9

Example 1. find solution of the following equation using Bisection method.


𝑓 𝑥 = 𝑒𝑥 − 𝑥 − 2 = 0
Figure 1.
From Figure 1., we observe that there exist two roots.
𝑓 𝑥 = 𝑒𝑥 − 𝑥 − 2
We begin the iteration with initial guess
𝑥𝐿 = −3, 𝑥𝑈 = −1 (Root1 is bracketed )
 𝑥𝑟𝑜𝑜𝑡1 = −1.841370. (15 iterations)
𝑓(𝑥𝑟𝑜𝑜𝑡1 ) = -0.00030
Tolerance : 0.0001 Root1 Root2

In case that the initial guess is not enclose a root, for example, Note: Code is available (see,
𝑥𝑈 = −3, 𝑥𝐿 = −2 (Root1 is not bracketed. i.e. 𝑓 −3 and 𝑓(−2) are Lecture3_Bisection.m &
the same sign) Lecture3_Bisection_Graph.m).
Bisection method fail to find the solution. ‘a’ and ‘b’ are initial guesses

MEC E 390 -A1


Example continued… 10

In order to find the other root, we set the initial points as


𝑥𝐿 = 1 𝑥𝑈 = 2 (Root2 is bracketed )
This yields
 𝑥𝑟𝑜𝑜𝑡2 = 1.146179. (14 iterations)
𝑓(𝑥𝑟𝑜𝑜𝑡2 ) =-0.00030

If we enclose both roots say 𝑥𝐿 = −2 𝑥𝑈 = 2


Bisection method fail to find root. This is simply because
𝑓 −2 and 𝑓 2 are the same sign

Note
1. In order to find a root using Bisection method, a root must be enclosed by
initial guessing points.
2. Convergence is not guaranteed.
3. Finding different root are not always straight forward especially when we do
not know the shape of the graph. The method entirely rely on trial error.
MEC E 390 -A1
Imposition of iterative conditions 11

 Condition for Root bracketing


1. If 𝑓(𝑥𝐿 )𝑓(𝑥𝑈 ) < 0
Good method, but it may fail, if function values are extremely small or extremely large
(underflow or overflow ).
2. If 𝑠𝑖𝑔𝑛(𝑓𝐿 ) ≠ 𝑠𝑖𝑔𝑛(𝑓𝑈 )
Better method, because it is independent of magnitude of 𝑓(𝑥).
−1, when 𝑓 < 0
𝑠𝑖𝑔𝑛 𝑓 = 0, when 𝑓 = 0
1, when 𝑓 > 0
Advantage (Bisection method)
• conceptual simplicity
1
• The interval bracketing the root is halved in size at each iteration (i.e. 𝑥𝑟 − 𝑥𝐿 = 𝑥𝑈 − 𝑥𝐿 )
2
Disadvantage (Bisection method)
• Rate of convergence is relatively slow
• The initial guesses must bracket the root (i.e. 𝑓(𝑥𝑟 )𝑓(𝑥𝑈 ) < 0)
MEC E 390 -A1
Convergence 12

 Convergence criteria
f(x)
1. Convergence criteria in 𝑥 (required) 𝑥𝑎 𝑥𝑟 𝑥𝑏 𝛿𝑓

a)
𝑥𝑏 −𝑥𝑎
< 𝛿𝑥1 or 𝑥𝑏 − 𝑥𝑎 < 𝛿𝑥2 , x
𝑥𝑏𝑜 −𝑥𝑎𝑜

Relative error Ture error 𝛿𝑥

where 𝑥𝑏𝑜 − 𝑥𝑎𝑜 is the initial interval. This version of the relative error means an order of magnitude
𝑥 −𝑥
check: 𝑥 𝑏 −𝑥𝑎 < 10−5 (we require that the final interval be smaller than 5 orders of magnitude)
𝑏𝑜 𝑎𝑜
𝑥𝑏 −𝑥𝑎 𝑥𝑏 −𝑥𝑎 2 𝑥𝑏 −𝑥𝑎
b) Alternatively, we may also consider = = as a relative error
2𝑥𝑟 2 𝑥𝑏 +𝑥𝑎 𝑥𝑏 +𝑥𝑎
𝑥𝑏 −𝑥𝑎
< 𝛿𝑥1 or 𝑥𝑏 − 𝑥𝑎 < 𝛿𝑥2 ,
𝑥𝑏 +𝑥𝑎

Relative error Ture error


Usually, the first test should stop iterations ad practical values of are 𝛿𝑥1 are 10−4 ~10−6. The true
error test is a safeguard in case in case that roots are nearly zero.
𝛿𝑥2 is usually set to be 10−8 ~10−12 .

MEC E 390 -A1


Convergence 13

2. Convergence criterion on 𝑓 (optional)


The condition need to be imposed especially when we want to ensure our root estimate results in
a function value close to zero.
𝑓𝑟
< 𝛿𝑓 usually 𝛿𝑓 ≪ 𝛿𝑥1 , where 𝑓𝑟 = 𝑓(𝑥𝑟 ).
( 𝑓𝑎 + 𝑓𝑏 )/2

Note: The above test are “stopping criteria” used with For-loops. While-loops requires
“continuation criteria”:

Continuation While ( relative error > tol1 AND Ture error > tol2) OR (relative 𝑓 > tol3)

Stopping Stop if ( relative error < tol1 OR Ture error < tol2) AND (relative 𝑓 < tol3)

MEC E 390 -A1

You might also like