Anda di halaman 1dari 43

MBE2036 Engineering Computing, part 5, version 2

MBE 2036
Finding Roots
Part 5

Dr. Yajing Shen


Office: AC-1 G6617
Email: yajishen@cityu.edu.hk
Department of Mechanical and
1
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 2

Review
Round-off error
Computer has range limit
Floating point numbers approximated by binary number

Truncation error

dv v v (t i 1) v (t i )
Eq. 1.31
dt t t i 1 t i

The difference here

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 3

Truncation error
truncate the series after the first derivative term:
Taylor series
f '' ( x i ) 2 f ( 3 ) ( x i ) 3
f ( xi 1 ) f ( xi ) f ( xi )h
'
h h ...
2! 3!
f ( n ) ( xi ) n
h Rn (Eq 4.1)
n!
where the step size h xi 1 xi

v (ti1) v (t i ) v ' (t i )(t i 1 t i ) R1


Remainder:

is a number lies between xi and xi+1

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 4

Truncation Errors
v (ti1) v (t i ) v ' (t i )(t i 1 t i ) R1 Eq 4.7

The finite difference approximation of first derivative

v (ti1 ) v (t i ) R1
v ' (t i ) Eq
ti+1 4.8is the step size
-ti=h,
(t i 1 t i ) (t i 1 t i )

The truncation error R1 v ' ' ( )


( t i 1 t i ) Eq 4.9
of the approximation: ( t i 1 t i ) 2!
R1
Or written as: ( t t ) O ( t i 1 t i ) Eq 4.10
i 1 i

Halving the step size would in principle halve the error of the derivative

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 5

Example of Taylor series


Proof Maclaurin series expansion is a special case of
the Taylor series expansion with xi = 0 and h = x:

Let: f(x)=ex f '' ( x i ) 2 f ( 3 ) ( x i ) 3


f ( xi 1 ) f ( xi ) f ( xi )h
'
h h ...
2! 3!
So: f(x)=ex f(0)=1 f ( n ) ( xi ) n
h Rn (Eq 4.1)
f(x)=ex f(0)=1 n!
where the step size h xi 1 xi
f(n)(x)=ex f(n)(0)=1

When xi = 0 and h=x

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 6

Total Numerical Error


Small step size will increase round-
Log error
off error due to subtractive Large step size will decrease
cancellation or due to an increase round-off error
in the number of computations

Optimal
point

Truncation error can be Large step size will increase


reduced by reducing truncation error
step size
Log step size

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 7

-Application case-

Finding Roots

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 8

Roots of Equations
Consider our parachutists example:
c
m t
v g(1 - e m ) Eq. 1.29
c
Determine: The time t for a parachutist to attain a
prescribed velocity v, with a given mass m and
given drag coefficient c

Determine: The drag coefficient c for a parachutist


of a given mass m to attain a prescribed velocity v
in a set time period t
c is an implicit parameter, it is impossible to
isolate c on one side of the equal sign

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 9

Roots of Equations
For a quadratic equation:

f(x) ax bx c 2
Eq 5.1

The following formula can calculate the roots for Eq 5.1,


i.e. the values of x that make f(x)=0:

- b b 2 4ac
x Eq 5.2
2a

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 10

Roots of Equations
c
m t
v g(1 - e m ) Eq. 1.29
c

To solve this problem, we can re-formulate Eq


1.29 to the following equation and find the root
of the equation:

c
m t
f(c) g(1 - e m
)- v 0 Eq. 5.3
c

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 11

Root of equations

f(x)=0
How to solve?

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 12

Graphical Method
Suppose mass m=68.1kg, g = 9.8 m/s2 and we want to
find c which it will make v=40m/s after falling for time
t=10s
c
m t
f(c) g(1 - e m ) - v 0 Eq. 5.3
c
f(c)
This is the c making f(c)=0

The roots are actually the


values of c which make
f(c)=0, i.e. where the curve
c
crosses the c axis

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 13

Graphical Method
c
m t
f(c) g(1 - e m ) - v 0 Eq. 5.3
c

So, to find the root, we can substitute the known parameters


(m, g, t) to Eq 5.3 and plot the graph of the equation in order
to observe where it crosses the c axis:

9.8 68.1 f(c)


f(c) (1 e (c/68.1) 10 ) 40 Eq 5.4
c
c
667.38
f(c) (1 e 0.146843c ) 40 Eq 5.5
c
Give a series of c to calculate f(c) =>
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 14

Graphical Method
c f(c)
1 51.14440
2 44.92059
3 39.26343
4 34.11489 f(c)
5 29.42327 c=14

6 25.14248
7 21.23144
c=15
8 17.65345
9 14.37577
10 11.36912
11 8.60733 root
12 6.06695
13 3.72701
14 1.56871
15 -0.42483
16 -2.26875
17 -3.97666
18 -5.56079 c
19 -7.03214 ~14.75
20 -8.40062 Figure 5.1

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 15

Graphical Method
The graphical method gives a rough estimation
of the root and the value of c is 14.75
The result can be substituted back to Eq 5.5 to
confirm the estimate: Very close to zero
667.38
f(14.75) (1 e 0.146843 14.75 ) 40 0.059
14.75
The result can also be checked by substituting
the estimated c to Eq 1.29 Very close to 40 m/s

9.8 68.1
v (1 e (14.75/68.1)10 ) 40.059
14.75

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 16

Graphical Method
Although graphical method is not very precise, it
can still give rough estimates of the roots
The graphical method provides useful
information for understanding the properties of
the functions
These estimates can be used as the initial
guesses for other numerical methods for finding
the roots and anticipating the pitfalls of the
numerical methods

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 17

Some Common Cases of Roots


f(x) Figure 5.2 f(x) Figure 5.3

xL xU x xL xU x

If f(xL) and f(xU) in both Figure 5.2 and 5.3 have the same sign, either there
will be no roots or there will be an even number of roots within the interval
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 18

Some Common Cases of Roots


f(x) Figure 5.4 f(x) Figure 5.5

x x
xL xU xL xU

If f(xL) and f(xU) in both Figure 5.4 and 5.5 have different signs,
there will be an odd number of roots within the interval
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 19

Some Exceptional Cases


f(x) Figure 5.6 f(x) Figure 5.7

Discontinuous
function

x x
The function is
xL tangential to xU xL xU
the x axis
Both cases have even number of roots even f(xL)
and f(xU) in both Figure 5.6 and 5.7 have
different signs
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 20

Automatic Methods for Finding Roots


Two major classes of methods:
Bracketing methods
These methods are based on two initial guesses that bracket
the root, i.e. on either side of the root.
These methods can always work but converge slowly, i.e.
they typically take more iterations to home in on the answer
Open methods
these methods can involve one or more initial guesses, but
there is no need for them to bracket the root
These methods do not always work but when they do they
usually converge quicker

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 21

Bracketing methods
Two initial guesses that bracket the root:

f(x) Figure 5.4

x
xL xU

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 22

Bisection Method
Bisection method is a common Bracketing method
It uses an incremental search technique to find root. The
basic principle is:
In general, if f(x) is real and continuous in the interval from xL and
xU and f(xL) and f(xU) have opposite signs, i.e.
f(xL) f(xU) < 0
then there is at least one real root between f(xL) and f(xU)

Locating an interval where the function changes sign.


Dividing the interval into a number of subintervals. Each of these
subintervals is searched to locate the sign change.
The process is repeated and the root estimate refined by dividing
the subintervals into finer increments

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 23

Bisection Method (cont)


The bisection method (alternatively called binary
chopping, interval halving or Bolzanos method) is one
type of incremental search method in which the interval
is always divided in half.
If the function changes sign over an interval, that
particular interval will be sub-divided in half.
Each sub-interval will then be checked for sign change.
The sub-interval with the sign change will be sub-divided
again into smaller subintervals
The whole process is repeated until the root is found or
until the estimate error is less than a stopping criterion

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 24

Bisection Method (cont)


The initial guesses could be obtained from a plot of the function
or from an incremental search method
A simple algorithm for the bisection calculation:
Step 1: choose lower xL and upper xU guesses for the root
that the function changes sign over the interval, i.e.
f(xL)f(xU)<0
Step 2: An estimate of the root xr is determined by
x x
xr L U Eq 5.6 Assuming the root is in the middle of the interval
2
Step 3: Make the following evaluation to determine in which
Check
whether
subinterval the root lies:
the a. If f(xL)f(xr)<0, the root lies in the lower subinterval. Therefore,
assumption set xU=xr and return to step 2
in step 2 is b. If f(xL)f(xr)>0, the root lies in the upper subinterval. Therefore,
true set xL=xr and return to step 2
c. If f(xL)f(xr)=0, the root equals xr; terminate the computation

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 25

Bisection Method (cont)


The termination criterion stated in Step 3 c is not
practical for many real applications;
It may take a long time to converge to the root;
Sometimes, it may never converge to the root due to
round-off error
Two possible alternatives for terminate the process:
Define the stopping criterion based on the true percent relative
error
True value - Approxima tion
t 100% Eq 3.1 and t s
True value

Problem ! We are actually trying to find the root so the true value is not known.
Hence, it is not too practical to use the above as the termination criterion
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 26

Bisection Method (cont)


Define the stopping criterion based on the
approximate percent relative error (recall Eq 3.3)
present approximat ion - previous approximat ion
a 100% Eq 3.3
present approximat ion
xrnew xrold
a 100% Eq 5.7 and a s
xrnew
a is the approximat e percent relative error
x rnew is the estimated root for the current iteration
x rold is the estimated root for the previous iteration
and s is the stopping criterion
This method is more suitable for practical applications

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 27

Example 1 - Bisection Method


Problem Statement : use bisection method to find the
root for the same problem shown in Figure 5.1.
Assuming we are given the search range between 12
and 16 and the stopping criterion of s=0.5%.

c
m t
f(c) g(1 - e m
)- v 0 Eq.
f(c)
5.3
c

c
Note: If we are not given the initial search range, we could use Figure 5.1 to help us to select the initial guesses. In this case, we
may choose the search range between 14 and 16 instead.

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 28

Example 1 - Bisection Method


Step 1: Check the sign: xU
xL

xL=12, xU=16 f(c)


f(12)= 6.0669 > 0
f (16)= -2.2688 < 0
c

So, we have: f (xL)f(xU)<0

It mean the root is between xL (12) and xu (16)

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 29

Example 1 - Bisection Method


Step 2: Calculate xr :
xL xr xU

x L xU
xr Eq 5.6
=14 f(c)
2
Step 3: Check the root:
We check whether the root is actually in the
middle, upper subinterval or lower subinterval:
c
1st Iteration f(xr)=f(14)= 1.5687 > 0 f(12)f(14) > 0

It indicates that there is no sign change between xL and the midpoint xr


Therefore, the root must lies in the upper subinterval, i.e between 14 (xr) and 16 (xu)

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 30

Example 1 - Bisection Method (cont)


Repeat Step 2 to calculate new xr, but with xL=old
xr (14): xL= 14 and xU=16
xL x
Old U
2nd Iteration
xr =(14+16)/2=15
New
15 - 14 f(c)
a 100 6.6667%
15

a > s therefore we continue Step 3:


f(14)f(15) = 1.5687*(-0.4248)< 0
Therefore, the root lies between 14 and 15 c
Repeat Step 2 with xU=xr xL= 14 and
xU=15
14.5 - 15
xr =(14+15)/2=14.5 a 100 3.4483%
3rd Iteration 14.5
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 31

Example 1 - Bisection Method (cont)

a > s therefore we continue Step 3:


f(14)f(14.5) =1.5687*0.5523=0.8664 >0
Repeat Step 2 with xL=xr xL= 14.5 and xU=15
4th Iteration xr =(14.5+15)/2=14.75

14.75 - 14.5
a 100 1.6949%
14.75

a > s therefore we continue Step 3.

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 32

Example 1 - Bisection Method (cont)


xL=12, xU=16
1st Iteration
12 14 16 xr=14

f (xL)f(xr)>0

xL=14, xU=16
2nd Iteration
12 14 16 xr=15
f (xL)f(xr)<0

xL=14, xU=15
3rd Iteration
12 14 16 xr=14.5
f (xL)f(xr)>0

xL=14.5, xU=15
4th Iteration
12 14 16 xr=14.75

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 33

Example 1 - Bisection Method (cont)

The result is as Iteration xL xU xr a (%)


shown in the 1 12 16 14
table on the
right 2 14 16 15 6.667

3 14 15 14.5 3.448

4 14.5 15 14.75 1.695

5 14.75 15 14.875 0.840

a < s so it stops 6 14.75 14.875 14.8125 0.422

The true root is 14.7802085

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 34

Error Analysis for Bisection Method


Estimated root xr
xL xU x L xU
xr
True root
2
(the correct estimate of the true root)

xL xr +x/2
xU
x = xU - xL

True root If the true root lies between xU and xL, the
(the upper limit of the true root) true root must lies within:

xL
-x/2 xr xU x Nice! It tells us the
xr maximum error of our
2
estimate of the true root
True root
(the lower limit of the true root)

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 35

Error Analysis for Bisection Method (cont)


xrnew xrold
a 100% Eq 5.7 and a s
xrnew This tells us that a provides the
xrnew - xrold exact upper bound on the true
error. i.e
a t
xLold xrold xUold
Previous iteration

xL new xrnew xUnew


Current iteration

x Lnew xUnew
xrnew Eq 5.8
2
x = xUnew - xLnew x
2
xUnew xLnew
xrnew xrold Eq 5.9
2
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 36

Error Analysis for Bisection Method (cont)


xrnew xrold
a 100% Eq 5.7 and a s
xrnew
x Lnew xUnew
xrnew Eq 5.8
2
xUnew xLnew
xrnew xrold Eq 5.9
2

Substitute Eq 5.8 and Eq 5.9 into Eq 5.7, we have


an alternative formulation for the approximate
percent relative error:
xU xL
2 x xL
a 100% U 100% Eq 5.10
xU xL xU xL
2

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 37

Error Analysis for Bisection Method (cont)


xrnew xrold
a 100% Eq 5.7 and a s
xrnew

xU xL
2 x xL
a 100% U 100% Eq 5.10
xU xL xU xL
2

Equation 5.10 produce the same result as Eq


5.7 for bisection method
Unlike Eq 5.7, Eq 5.10 can be used to calculate
a for the initial guesses

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 38

Error Analysis for Bisection Method (cont)

Besides the ability to analysis the exact upper bound of the true error
The bisection method also allows us to calculate the number of iterations
required to obtain an absolute error before starting the iterations
Before starting the technique, the absolute error is:
EA0=xU0 xL0 = x0
After the first iteration, the error becomes:
EA1 = x0/2
Because each succeeding iteration halves the error, a general formula
relating the error and the number of iterations, n, is:
x 0 This is the maximum error
E An Eq 5.11
2n
If EA,d is the desired error, the above equation can be written as:
x 0
log

x 0 x 0
E A,d
2n n log 2n log n
EA E A,d log 2
x 0
n log 2
Eq 5.12
E A,d
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 39

Example 2 - Error Analysis for Bisection


Method
Assuming the initial guesses for xU=16 and xL=12.
x0=16-12 = 4.
After 6 iterations, based on Eq5.11 the absolute error
is: 4 4
6
EA 6
0.0625
2 64
In reverse, if we are satisfied with the absolute error
EA6=0.0625, based on Eq 5.12, we can calculate the
number of iteration required to obtain the required
error: 4
log
n 0.0625
6
log 2

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 40

Bisection Algorithm
This program is not
Function(xL, xU, es, imax, xr, iter,ea) a real program. It
% es: stopping criterion just shows the main
sequences and
% ea: approximate percent relative error logics of the
% imax: the maximum number of iteration program.
iter = 0
fL = f(xL) % f( ) is the function for evaluation
DO
xr_old = xr
xr = (xL + xU)/2
fr=f(xr)
iter = iter +1
IF xr 0 THEN
ea =ABS((xr xr_old)/xr)*100
END IF
Department of Mechanical and Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing, part 5, version 2.0 41

Bisection Algorithm (cont)


test = fL * fr
IF test < 0 THEN
xU = xr
ELSE IF test > 0 THEN
xL = xr
fL = fr
ELSE
ea = 0
END IF
IF ea < es OR iter >= imax EXIT
END DO
Bisect = xr
END Bisect

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 42

Flow chart

Department of Mechanical and Biomedical Engineering City University of Hong Kong


MBE2036 Engineering Computing, part 5, version 2.0 43

Learning Outcomes
After this lecture, the student would be able to understand the
following:

What roots problems are and where they occur


in engineering applications
How to use a graphical method to determine the
rough estimate of root
The basic principle and the use of bisection
method for finding roots
How to estimate the error of bisection method

Department of Mechanical and Biomedical Engineering City University of Hong Kong

Anda mungkin juga menyukai