You are on page 1of 48

MBE2036 Engineering Computing part 6, Dr.

Yajing Shen 1

Review
Root of equation: f(x)=0
Graphical Method
Given a series of x to calculate f(x)
f(x)

Rough estimation This is the x making f(x)=0

provides useful information for


understanding the properties of
the functions
x
Gives us the initial guess

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 2

Review
Root of equation: f(x)=0
Automatic Methods f(x) Figure 5.4

Bracketing methods

Two initial guesses that


bracket the root
Always work but converge
slowly
x
xL xU
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 3

Review
Root of equation: f(x)=0
Automatic Methods
Bracketing methods xL xr xU
Bisection method

f(x)
1. Check the sign: f (xL)f(xU)
2. Calculate the root: xr
3. Check the root: f(xr) f(xL)
x

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 4

Review
Error Analysis for Bisection Method
The approximate percent relative error:
xU xL
2 x xL
a = 100% = U 100% Eq 5.10
xU + xL xU + xL
2

After n iterations, the absolute error:


x 0
E An = n
Eq 5.11
2
To reach the desired error EA,d, the number of iteration required:

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 6, Dr. Yajing Shen 5

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 6, version1

MBE 2036
Roots of Equations Open Methods

Part 6

Dr. Yajing Shen


Office: AC-1 G6617
Email: yajishen@cityu.edu.hk

Department of Mechanical and


6
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 7

Roots of Equations Open Methods


Unlike bracketing methods, the open methods
normally require only a single starting value or
two starting values that do not necessarily
bracket the root
As such, they do not always converge to the true
value of the root, i.e. sometimes they move
away from the true root as the computation
progresses
However, when the open methods converge,
they usually do so much more quickly than the
bracketing methods
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 8

Newton-Raphson Method
It is one of the most widely root-locating
method.
If the initial guess is xi, a tangent can be
extended from the point [xi , f(xi)] (see
figure 6.1).
The point where this tangent crosses the x
axis usually represents an improved
estimate of the root (see figure 6.1).

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 9

Newton-Raphson Method (cont)


Slope = f(xi)
f(x)

f(xi)

f(xi)-0
x
xi+1 xi

xi xi+1

Figure 6.1

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 10

Newton-Raphson Method (cont)


Based on Figure 6.1, the slope of the tangent
of the point [xi , f(xi)] is:

f(xi ) 0
f ' (xi ) =
f(xi ) 0
Eq 6.1 xi xi +1 = '
xi xi +1 f (xi )
The above equation can be re-arranged to
yield
f(xi )
xi +1 = xi ' Eq 6.2
f (xi )

Eq 6.2 is called the Newton-Raphson formula


Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 11

Example 1
Problem statement: Use the Newton-Raphson
method to estimate the root of f(x)=e-x - x,
employing an initial guess of x0=0. The true value
of the root is 0.56714329.
Solution: Evaluate the first derivative:
f ' (x) = e x 1 Eq 6.3
Eq 6.3 can be substituted along with the original
function in Eq 6.2 to give
e xi xi
xi +1 = xi
e 1 xi
( Eq 6.4
)
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 12

Example 1(cont)
Starting with an initial guess of x0=0
When i=0: True value Estimate
x0=0
the initial error =0.56714329 -0 = 0.56714329
The true percent relative error =100%
0
True value - Approxima tion
t = 100% Eq 3.1
True value
When i = 1: e x0 x0 0
x = x0 = 0.5
From Eq 6.4, 1 ( e 1)
x0

The true error = 0.56714329 -0.5 = 0.06714329


The true percent relative error =11.8389% t = 0.56714329 - 0.5 100%
0.56714329
The approximate percent relative error:
x rnew x rold 0.5 - 0
a = 100% = 100% = 100%
x rnew 0.5
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 13

Example 1(cont)
0.5
e x1 x1
When i=2: x2 = x1 = 0.566311003
( e 1)
x1

The true error = 0.56714329 0.566311003 = 8.3229x10-4


The true percent relative error =0.1468%
0.56714329 - 0.566311003
t = 100% = 0.1468%
0.56714329
The approximate percent relative error:

x rnew x rold 0.56631100 3 - 0.5


a = new
100% = 100% = 11.7093%
xr 0.56631100 3

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 14

Example 1 (cont)
i xi True error t (%) a (%)
0 0 0.56714329 100

1 0.5 0.06714329 11.8389 100

2 0.566311003 8.3229x10-4 0.1468 11.7093

3 0.567143165 1.25 x 10-7 0.000022 0.14767

4 0.567143290 0 0 0.000022

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 15

Example 2
Remember the previous problem of finding the
drag coefficient c for a parachutist in Lecture
Handout 5: m=68.1kg, g=9.8m/s2, v=40m/s
c
m t
f(c) = g(1 - e m ) - v = 0 Eq. 5.3
c
667.38
f(c) = (1 e 0.146843c ) 40 Eq 5.5
c
This is to find c which will make Eq 5.5 to zero
In Handout 5, we use the Bisection Method to
solve the problem. Now, we will try the Newton-
Raphson Method
The true root of the Eq 5.5 is 14.7802085
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 16

Example 2
If we let k1=667.38 and k2=0.146843, Eq 5.5
can be rewritten as: n
dx
k1 k1 k2c = nx n1
f(c) = e 40 Eq6.5 dx
c c d ( k1c 1 ) k1dc 1
d (uv )
=u
dv
+v
du = = k1c 2
dx dx dx dc dc
d40
=0
d k1 k1 k2c d dc
f ' (c) = e 40 = (k1c 1 k1c 1e k2c 40)
dc c c dc

( )
k2 c 1
d 1 k 2 c 1 de k 2 c dc
k1c e = k1c + k1e
dc dc dc
f ' (c) = k1c 2 (k1c 1( k 2 e k2c )) (e k2c ( k1c 2 ))
f ' (c) = k1c 2 + (k1c 1k 2 e k2c ) + (e k2c k1c 2 ) Eq 6.6
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 17

Example 2 (cont)
Substitute Eq 6.5 and 6.6 into Eq 6.2
-1 k2ci
k1ci k1ci e 40
-1
ci +1 = ci 2 1 k2ci k2ci 2
k1ci + (k1ci k 2 e ) + (e k1ci )

Assuming the initial guess is 12 and s=0.5%


-1 k2c0
k1c0 k1c0 e 40
-1
c1 = c0 2 1 k2c0 k2c0 2
k1c0 + (k1c0 k 2 e ) + (e k1c0 )
6.06694996
c1 = 12 = 14.4896708 7
(-2.436848 18)
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 18

Example 2 (cont)
The true error = 14.7802085-14.48967087
=0.29053763
14.7802085 - 14.4896708 7
t = 100% = 1.96572072 %
14.7802085

x rnew x rold 14.4896708 7 12


a = new
100% = 100 = 17.1823839 1%
xr 14.4896708 7

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 19

Example 2 (cont)
i xi True error t (%) a (%)

0 12

1 14.48967087 0.290537 1.965720 17.182383

2 14.77691694 0.003291 0.022270 1.943883

3 14.78020816 3.305693x10-7 2.236567x10-6 0.022267

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 20

Example 2 (cont)
Bisection Method
Iterati
on
xL xU xr t (%) a (%)
1 12 16 14 5.2787

2 14 16 15 1.4871 6.667

3 14 15 14.5 1.8958 3.448

4 14.5 15 14.75 0.2044 1.695

5 14.75 15 14.875 0.64134 0.840

6 14.75 14.875 14.8125 0.2185 0.422

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 21

Error Estimates
The error Et,i+1=xr xi+1, where xr is the true root
and xi+1 is the estimated root
Using Taylor series method, the true error can be
written as:
f '' (xr ) 2
E t,i+1 '
E t,i Eq 6.7
2f (xr )
Thus the error is roughly proportional to the
square of the previous error.
This means that the number of correct decimal
places approximately doubles with each iteration
This is referred to as quadratic convergence

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 22

Pitfalls of Newton-Raphson Method


There are situations where the Newton-Raphson method
performs poorly.
Figure 6.2 shows the case where an inflection point (ie,
f(x)=0) occurs in the vicinity of a root.
Figure 6.3 illustrates the tendency of the Newton-
Raphson technique to oscillate around a local maximum
or minimum. Such oscillations may persist.
Figure 6.4 shows how an initial guess that is close to one
root can jump to a location several roots away. This
tendency to move away from the area of interest is
because near-zero slopes are encountered.
Figure 6.5 shows the solution shooting off horizontally
and never hits the x axis. This is caused by a zero slope
(i.e. f(x) = 0); it causes a division-by-zero in the Newton-
Raphson formula (Eq 6.2)
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 23

Pitfalls of Newton-Raphson Method (cont)


f(x)

x1 x0

x2
x

Figure 6.2

An inflection point (ie, f(x)=0) occurs in the vicinity of a root

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 24

Pitfalls of Newton-Raphson Method (cont)


f(x)

x2 x0 x3 x1
x

Figure 6.3

Oscillate around a local maximum or minimum.

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 25

Pitfalls of Newton-Raphson Method (cont)


f(x)

x1

x0 x

Figure 6.4
An initial guess that is close to one root can jump to a location several roots
away. This tendency to move away from the area of interest is because near-
zero slopes are encountered.
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 26

Pitfalls of Newton-Raphson Method (cont)


f(x)

x0 x1 x
Figure 6.5
the solution shooting off horizontally and never hits the x axis. This is caused
by a zero slope (i.e. f(x) = 0)
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 27

Program for Newton-Raphson


Method
Because of the aforementioned potential pitfalls, the
software program would be improved by incorporating
several additional features:
A plotting routine should be included in the program.
At the end of the computation, the final root estimate should
always be substituted into the original function to compute
whether the result is close to zero. This check partially guards
against those cases where slow or oscillating convergence may
lead to a small value of a while the solution is still far from a root
The program should always include an upper limit on the number
of iterations to guard against oscillating, slow convergent or
divergent solutions that could persist for a long period of time.
The program should guards against and also alert the user when
the case of f(x)=0 occurs, i.e., avoiding the divide-by-zero error

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 28

Open Method Secant Method


A potential problem in implementing the
Newton-Raphson method is the evaluation of
the derivative.
Certain functions whose derivatives may be
extremely difficult or inconvenient to evaluate.
The derivative can be approximated by a
backward finite divided difference:

f(xi 1 ) f(xi )
f (xi )
'
Eq 7.1
xi 1 xi

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 29

Secant Method (cont)


f(x)
f(xi 1 ) f(xi )
f(xi) f ' (xi ) Eq 7.1
xi 1 xi

f(xi-1)
x
xi+1 xi-1 xi

Figure 7.1

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 30

Secant Method (cont)


Equation 7.1 can be substituted into Eq 6.2
f(x ) f(xi )
xi +1 = xi ' i Eq 6.2 xi +1 = xi Eq 7.2
f (xi ) f(xi 1 ) f(xi )

xi 1 xi
f(xi )(xi 1 xi )
xi +1 = xi Eq 7.3
f(xi 1 ) f(xi )

Equation 7.3 is the formula for the secant method


The approach requires two initial estimates of x
However, f(x) is not required to change signs between
the estimates
Therefore it is not a bracketing method

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 31

Example 1
Problem statement: Use the Secant method to
estimate the root of f(x)=e-x - x, employing two
initial guesses of x-1=0 and x0=1. The true value
of the root is 0.56714329.
Solution:
First iteration:
x-1 = 0 f(x-1) = e0 -0 = 1.0

x0 = 1 f(x0) = e-1 -1 = 0.367879 1 = -0.63212

f(x0 )(x1 x0 ) - 0.63212(0 - 1)


Based on Eq 7.3, x1 = x0 = 1- = 0.61270
f(x1 ) f(x0 ) 1 - (-0.63212)

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 32

Example 1 (cont)
0.56714329 - 0.61270
t = 100% = 8.032%
0.56714329

Second Iteration:

x0 = 1 f(x0) = e-1 -1 = 0.367879 1 = -0.63212

x1 = 0.61270 f(x1) = e-0.6127 - 0.6127= 0.54188 0.6127 = -0.07081

f(x1 )(x0 x1 ) - 0.07081(1 - 0.6127)


x2 = x1 = 0.6127 - = 0.56384
f(x0 ) f(x1 ) - 0.63212 - (-0.07081)

0.56714329 - 0.56384
t = 100% = 0.5823%
0.56714329

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 33

Example 1 (cont)
Third iteration:

x1 = 0.61270 f(x1) = e-0.6127 - 0.6127= 0.54188 0.6127 = -0.07081

x2 = 0.56384 f(x2) = 0.00518

0.00518(0.6127 - 0.56384)
x3 == 0.56384 - = 0.56717
- 0.07081 - (-0.00518)
0.56714329 - 0.56717
t = 100% = 0.00471%
0.56714329

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 34

Modified Secant Method


Rather than using two arbitrary values to estimate the
derivate, an alternative approach involves a fractional
perturbation of the independent variable to estimate
f(x),
f(xi + xi ) f(xi )
f (xi )
'
Eq 7.4
xi
Substitute Eq 7.4 into Eq 6.2:
xi f(xi )
xi +1 = xi Eq 7.5
f(xi + xi ) f(xi )

Eq 7.5 is the formula for the Modified Secant Method

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 35

Modified Secant Method (cont)


The choice of a proper value for is not
automatic.
If is too small, the method can be affected by
round-off error caused by subtractive
cancellation in the denominator of Eq 7.5
If is too big, the method can become inefficient
and even divergent
However, if chosen correctly, it provides a nice
alternative for cases where evaluating the
derivative is difficult and developing two initial
guesses is inconvenient.

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 36

Example 2
Problem statement: Use the Modified Secant
method to estimate the root of f(x)=e-x - x,
employing =0.01 and an initial guess of x0=1.
The true value of the root is 0.56714329.
Solution:
First iteration:
x0 = 1 0.01 x 1 f(x0) = e-1 -1 = 0.367879 1 = -0.63212

x0 + x0 = 1.01 f(x0+ x0 ) = e-1.01 -1.01 = 0.364219 1.01 = -0.64578


x0 f(x0 ) 0.01(-0.63212)
Based on Eq 7.5, x1 = x0 = 1- = 0.537263
f(x0 + x0 ) f(x0 ) - 0.64578 - (-0.63212)
t = 5.3%

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 37

Example 2
Second iteration:
x1 = 0.537263 f(x1) = e-0.537263 -0.537263 = 0.047083

x1 + x1 = 0.542635 f(x1+ x1 ) = e-0.542635 -0.542635 =0.038579


x1 f(x1 ) 0.005373(0.047083)
x2 = x1 = 0.537263 - = 0.56701
f(x1 + x1 ) f(x1 ) 0.038579 - 0.047083
t = 0.0236%

Third iteration:
x2 = 0.56701 f(x2) = 0.000209
x2 + x2 = 0.567143 f(x2+ x2 ) = -0.00867
0.0056701(0.000209)
x3 = 0.56701 - = 0.567143
- 0.00867 - 0.000209

t = 2.365 10 -5%
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 38

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

The difference between bracketing and open methods


for root location.
Newton-Raphson Mehtod
The basic principle
The concept of quadratic convergence
The limitations
Secant method and Modified Secant method
The basic principle
The limitations
The applications
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 39

Programming and Software


Sequence, selection, iteration and how to use function

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 40

Sequence

Matlab example:

x=3
y=x+5;
z=x*y;

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 41

Selection
Matlab example:

if x<0
y=-10;
else if x>0
y=10;
else
y=0;
end

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 42

Iteration
Matlab example:

for i=0:2:20
x=x+i;
end

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 43

Another iteration
Matlab example:

while (1)

i=i+1;

if i>=10, break, end

y=x^2;

end

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 44

function mainprog

m=68.1;
g=9.8; function myvelocity(m,g,c,t)
c=0.25;
t=0:2:12;

v=velocity(m,g,c,t) function velocity_profile(m,g,c,t)

v1=velocity_profile(m,g,c,t)

plot (t, v, t, v1) function

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 45

Appendix

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 46

Error estimates of
Newton-Raphson Method
f (n ) (x0 )
Taylor series: f ( x0 + x) = f ( x) = f ( x0 ) + f ( x0 )( x x0 ) + ... + ( x x 0 )n + R n ( x )
n!
f ( n +1) ( )
Rn ( x ) = (x x0 )n+1 ,
(n + 1)

So, we have: = + + 2
2!
Noted that: = 0
Therefore:

+ + 2
=0
2!

In Newton-Raphson method, we use:

+ +1 = 0

Department of Mechanical and


Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 47

Error estimates of
Newton-Raphson Method
Thus, subtracting the following two equations:


+ + 2 = 0
2!

+ +1 = 0

We have:

+1 + 2
=0
2!

Noted that error between xr and xi+1 is:


,+1 = +1

It can be expressed as: 2


2 So, ,+1 =
,+1 + , = 0 2 ,
2!
Department of Mechanical and
Biomedical Engineering City University of Hong Kong
MBE2036 Engineering Computing part 6, Dr. Yajing Shen 48

f(x)=e-x - x

Department of Mechanical and


Biomedical Engineering City University of Hong Kong

You might also like