You are on page 1of 148

MEEG 503

Numerical Methods
Fall 2015

Binary Representation

James Bond 00111


2

Floating Point

15900000000000000
14
could be represented as

Mantissa
Exponent
159
* 1014
15.9 * 1015
1.59 * 1016
A calculator might display 159 E14

257.76 2 10 2 5 101 7 100 7 10 1 6 10 2

Base 2
(1 23 0 2 2 1 21 1 2 0 )

(1011.0011) 2
1
2
3
4
(0 2 0 2 1 2 1 2 )
11.1875

10

Basic Conversion
Converting a decimal number to a
floating point number.
1. Take the integer part of the number and generate
the binary equivalent.
2. Take the fractional part and generate a binary
fraction
3. Then place the two parts together and normalise.

Convert Base 10 Integer to binary


representation
Table 1 Converting a base-10 integer to binary
representation.
Ex: 11
Quotient
Remainder
11/2

5/2

2/2

1/2

Hence

1 a0
1 a1
0 a2
1 a3

(11)10 (a3 a 2 a1 a0 ) 2
(1011) 2
6

Start

Input (N)10

Integer N to be
converted to binary
format

i=0

Divide N by 2 to get
quotient Q & remainder R

i=i+1,N=Q

ai = R

No
Is Q = 0?
Yes
n=i
(N)10 = (an. . .a0)2

STOP

Convert Factional Decimal Number to


Binary Representation
Table 2. Converting a base-10 fraction to binary
representation.
Ex: 0.1875
Number after Number before
Number

decimal

decimal

0.1875 2
0.375 2

0.375

0.375

0.75

0.75

0.75 2

1.5

0.5

0 a1
0 a 2
1 a 3

0.5 2

1.0

0.0

1 a 4

Hence (0.1875) ( a a a a )
10
1 2 3 4 2

(0.0011) 2
8

Start
Fraction
F
to
be
converted
to
binary
format

Input (F)10

Multiply F by 2 to get
number before decimal, S
and after decimal, T

ai = R

No
Is T =0?
Yes
n=i
(F)10 = (a-1. . .a-n)2

STOP

All Fractional Decimal Numbers Cannot be


Represented Exactly
Table 3. Converting a base-10 fraction to approximate binary
representation.

Number

0.3 2
0.6 2
0.2 2
0.4 2
0.8 2

0.6
1.2
0.4
0.8
1.6

Number
after
decimal
0.6
0.2
0.4
0.8
0.6

Number
before
Decimal

0 a1
1 a 2
0 a3
0 a 4
1 a 5

(0.3)10 (a1a 2 a3 a 4 a5 ) 2 (0.01001) 2 0.28125


10

Exercise-1:
Convert 235.8723 to a binary representation

How are numbers stored in a


computer

IEEE Single Precision


The number will occupy 32 bits

The first bit represents the sign of the number;


1= negative 0= positive.
The next 8 bits will specify the exponent stored
in biased 127 form.
The remaining 23 bits will carry the mantissa
normalised to be between 1 and 2.
i.e. 1<= mantissa < 2

Convert 6.75 to 32 bit IEEE format.


1. The Mantissa. The Integer first.
6/2 =3r0
3/2 =1r1
= 1102
1/2 =0r1
2. Fraction next.
.75 * 2 = 1.5
= 0.112
.5 * 2 = 1.0
3. put the two parts together
Now normalise

110.11
1.1011 * 22

IEEE Example 1
Convert 6.75 to 32 bit IEEE format.
1. The Mantissa. The Integer first.
6/2 =3r0
3/2 =1r1
= 1102
1/2 =0r1
2. Fraction next.
.75 * 2 = 1.5
= 0.112
.5 * 2 = 1.0
3. put the two parts together
Now normalise

110.11
1.1011 * 22

IEEE Biased 127


Exponent
To generate a biased 127 exponent
Take the value of the signed exponent and add 127.
Example.

216 then 2127+16 = 2143 and my value for the


exponent would be 143 = 100011112
So it is simply now an unsigned value ....

Why Biased ?
The smallest exponent
00000000
Only one exponent zero 01111111
The highest exponent is 11111111
To increase the exponent by one simply add 1 to
the present pattern.

Back to the example


Our original example revisited. 1.1011 * 22
Exponent is 2+127 =129 or 10000001 in binary.
NOTE: Mantissa always ends up with a value of 1
before the Dot. This is a waste of storage therefore it
is implied but not actually stored. 1.1000 is stored .
1000
6.75 in 32 bit floating point IEEE representation:0 10000001 10110000000000000000000
sign(1) exponent(8)
mantissa(23)

Special cases
0 + Infinity and - infinity.
Zero is a pattern that only contains 0s
00000000000000000000000000000000
Positive Infinity is the pattern
011111111.
Negative Infinity is the pattern
111111111.

From Floating Point Binary to


Decimal Example
1 01111011 11100000100000000000000
Sign = 1 therefore this number is a negative number.
Exponent 01111011 = 64+32+16+8+2+1
= 123
subtract the 127 = - 4
Mantissa
= 1.111000001
1.111000001 * 2- 4
-ve
0.0001111000001
1/16 + 1/32 +1/64+1/128+1/8192
or - 0.1173095703125

Convert
1 10000001

01101000000000000000000

To a decimal representation

Convert x = 329.390625 into floating point binary rep.

Errors

23

Taylor Series Revisited


s

10/19/15

24

What is a Taylor series?


Some examples of Taylor series which you
must have seen
x2 x4 x6
cos( x) 1


2! 4! 6!

x3 x5 x7
sin( x) x
3! 5! 7!
x2 x3
e 1 x

2! 3!
x

10/19/15

25

General Taylor Series


The general form of the Taylor series is given
by f x h f x f x h f x h 2 f x h3
2!

3!

provided that all derivatives of f(x) are


continuous and exist in the interval [x,x+h]

10/19/15

26

ExampleTaylor Series
f 4 125, f 4 74,
Find the value f 6 given
f 4 30, f 4 6 and all
of
that
other higher order
derivatives
of f x a x 4 are
zero.
t
Solution:

h2
h3
f x h f x f x h f x f x
2!
3!
x4
h 64 2

10/19/15

27

Example (cont.)
Solution:
(cont.)the higher order derivatives are zero,
Since
22
23
f 4 2 f 4 f 4 2 f 4 f 4
2!

3!

22
23
6

f 6 125 74 2 30
2!
3!
125 148 60 8

341

f 6 exactly, we only need the


Note that to
find
of
the function andvalue
all its derivatives at some
x case
4
other point, in this
10/19/15

28

Derivation for Maclaurin Series for


ex
Derive the Maclaurin
series
x2 x3
x
e 1 x

2!

3!

The Maclaurin series is simply the Taylor series


about the point x=0
h2
h3
h4
h5
f x h f x f x h f x f x f x f x
2!
3!
4
5
h2
h3
h4
h5
f 0 h f 0 f 0 h f 0 f 0 f 0 f 0
2!
3!
4
5

10/19/15

29

Derivation (cont.)
Sinc f ( x) e x , f ( x) e x ,
fen (0) e 0 1

f ( x) e x , ... , f n ( x) e x

and

the Maclaurin series is then


( e 0 ) 2 (e 0 ) 3
f ( h) (e ) (e ) h
h
h ...
2!
3!
1
1
1 h h 2 h 3 ...
2!
3!
0

So,

10/19/15

x 2 x3
f ( x) 1 x ...
2! 3!
30

Error in Taylor Series


The Taylor polynomial of order n of a function
f(x) with (n+1) continuous derivatives in the
domain [x,x+h] is given by
h2
hn
n
f x h f x f x h f ' ' x f x Rn x
2!
n!

where the remainder is given


n 1
by R x x h f n 1 c
n

(n 1)!

where
x c xh

that is, c is some point in the domain [x,x+h]


10/19/15

31

Exampleerror in Taylor series


x

The Taylor series e at


for e x 1 x x 2 x 3 x 4 point
x5

2!

3!

4!

x 0 is given by

5!

It can be seen that as the number of terms


used
increases, the error bound decreases and
hence a
better
estimate of the function can be
found.
How many terms would it require to
get an approximation of e1 within a
magnitude of true error of less than
10-6.
10/19/15

32

Example(cont.)
Solution:
Usin n 1 terms of Taylor series gives
n 1
g

x herror
x 0, h 1, f ( x ) e x
Rn x
f n 1 c
n 1!
n 1

0 1
Rn 0
f n 1 c
n 1!
n 1

ec
n 1!
Since

x c xh
0 c 0 1
0 c 1

10/19/15

1
e
Rn 0
(n 1)!
(n 1)!
33

bound of

Example(cont.)
Solution: (cont.)
So if we want to find out how many terms it
e1 within a
would
require to get an
approximation
of error of less 10 6 ,
magnitude
of true
than e 106
(n 1)!

(n 1)! 10 6 e
(n 1)! 10 6 3

n9

So 9 terms or more are needed to get a true


error
less than10 6
10/19/15

34

Measuring Errors

35

Why measure errors?


1) To determine the accuracy of
numerical results.
2) To develop stopping criteria for
iterative algorithms.

36

True Error
Defined as the difference between the true
value in a calculation and the approximate
value found using a numerical method.
True Error = True Value Approximate Value

37

ExampleTrue Error
f (x) of a
f (x) can be
The
derivative,
function
approximated
by the
equation,
f ' ( x)

f ( x h) f ( x)
h

0.5 x
If f ( x) 7e an h 0.3
a) Find dthe approximate
value
b) Trueofvalue f ' ( 2)
of
c) True error for part
(a)

f ' ( 2)

38

Example (cont.)
Solution:
a) For x 2 and h 0.3
f ( 2 0.3) f ( 2)
0 .3
f ( 2.3) f ( 2)

0 .3

f ' ( 2)

7e 0.5( 2.3) 7e 0.5( 2 )

0 .3
22.107 19.028
10.263

0.3

39

Example (cont.)
Solution
: b) The exact value f ' ( 2) can be found by
of knowledge of differential
usingcalculus.
our
f ( x ) 7 e 0. 5 x

f ' ( x ) 7 0.5 e 0.5 x


3.5e 0.5 x

So the true value


0 .5 ( 2 )
f
'
(
2
)

3
.
5
e
of

f ' ( 2)

is

9.5140

True error is calculated as


Et True Value Approximate
9.Value
5140 10.263 0.722
40

Relative True Error


Defined as the ratio between the
true error, and the true value.
Relative True Error (t ) =

True Error
True Value

41

ExampleRelative True
Error
Following from the previous example for true
error,
find the relative true error f ( x) 7e 0.5 x at f ' (2)
for h 0.3
wit
h From the previous example,
Et 0.722

Relative True Error is defined


as t True Error
True Value
0.722

0.075888
9.5140

as a percentage,
t 0.075888 100% 7.5888%
42

Approximate Error
What can be done if true values are
not known or are very difficult to
obtain?
Approximate error is defined as the
difference between the present
approximation and the previous
approximation.
Approximate
ErrorE ) = Present Approximation Previous
a

Approximation

43

ExampleApproximate
Error
For f ( x) 7e 0.5 x at x 2 find the
a) f (2) usin h 0.3 following,
b) f (2) gusin h 0.15
g
c) approximate
error for the
value of
Solution:
x 2 and h 0.3
a)
For f ' ( x ) f ( x h ) f ( x )

f (2)

h
f ( 2 0.3) f ( 2)
f ' ( 2)
0.3

44

for part
b)

Example (cont.)
Solution: (cont.)
f ( 2.3) f ( 2)
0 .3
7e 0.5( 2.3) 7e 0.5( 2 )

0 .3

22.107 19.028
10.263
0.3

b) For x 2 and h 0.15


f (2 0.15) f (2)
0.15
f (2.15) f (2)

0.15

f ' ( 2)

45

Example (cont.)
Solution: (cont.)
7e 0.5( 2.15) 7e 0.5( 2)

0.15

20.50 19.028
9.8800
0.15

E a is
c) So the approximate
error,
Ea Present Approximation Previous
9Approximation
.8800 10.263
0.38300

46

Relative Approximate Error


Defined as the ratio between the
approximate error and the present
approximation.
Relative Approximate
Error (

a) =

Approximate Error
Present Approximation

47

ExampleRelative Approximate
Error
f ( x) 7e 0.5 x

For
at x 2 , find the relative
h 0.3 and h 0.15
error using values approximate
from
Solution:
From Example 3, the approximate
0.3 and f (2) 9.8800 usin h 0.15
value hof
usin
g Ea Present Approximationg Previous

f (2) 10.263

9Approximation
.8800 10.263
0.38300

48

Example (cont.)
Solution:
(cont.) Approximate Error
a

Present Approximation
0.38300
0.038765
9.8800

as a
a 0.038765 100% 3.8765%
percentage,
Absolute relative approximate errors may also
need to be calculated,
a | 0.038765 | 0.038765 or 3.8765 %
49

How is Absolute Relative Error used as


a stopping criterion?
If |a | s wher s is a pre-specified tolerance,
e iterations
then are necessary and the
no further
process is stopped.
If at least m significant digits are required
to be correct in the final answer, then
|a | 0.5 10 2m %

50

Table of Values
0.5 x
For f ( x) 7e at x 2 with varying step
size,

f (2)

0.3

10.263

N/A

0.15

9.8800

3.877%

0.10

9.7558

1.273%

0.01

9.5378

2.285%

0.001

9.5164

0.2249%

51

Sources of Error

10/19/15

52

Two sources of numerical error


1) Round off error
2) Truncation error

53

Round off Error


Caused by representing a number
approximately
1
0.333333
3

2 1.4142...

54

Problems created by round off


error
28 Americans were killed on February
25, 1991 by an Iraqi Scud missile in
Dhahran, Saudi Arabia.
The patriot defense system failed to
track and intercept the Scud. Why?

55

Problem with Patriot missile


Clock cycle of 1/10 seconds
was represented in 24-bit
fixed point register created
an error of 9.5 x 10-8 seconds.
The battery was on for 100
consecutive hours, thus
causing an inaccuracy of
s
3600s
9.5 10
100hr
0.1s
1hr
0.342 s
8

56

Problem (cont.)
The shift calculated in the ranging
system of the missile was 687
meters.
The target was considered to be out
of range at a distance greater than
137 meters.

57

Example of Truncation Error


Taking only a few terms of a Maclaurin
x
series
to
e
approximat
2
3
e
x
x
x

e 1 x

2!

3!

....................

If only 3 terms are used,

x
x
Truncation Error e 1 x
2!

58

Another Example of Truncation


Error
x to
Using a
) f ( x)
finite f ( x) f ( x xapproximate
x

secant line

P
tangent line

Figure 1. Approximate derivative using finite


x
59

f (x)

Another Example of Truncation


Error
Using finite rectangles to approximate
an integral.

60

Example 2 Differentiation
f (3)

f ( x) x 2

Find
for
an x 0.2
f (3 0.2) f (3)
'
d
f (3)

f ( x x) f ( x)
using f ( x)
x

0.2
3.2 2 32
f (3.2) f (3)

0 .2
0.2

1.24
10.24 9
6. 2

0. 2
0.2

The actual value


is f ' ( x) 2 x, f ' (3) 2 3 6
6 6.2 0.2
Truncation error is
then,
Can you find
61 the truncation error x 0.1

Example 3 Integration
Use two rectangles of equal width to
approximate the area under the curve
[3,9]
f ( x) x 2 over the
for
interval
9

x
dx

62

Integration example (cont.)


Choosing a width of 3, we
9
havex 2 dx ( x 2 ) (6 3) ( x 2 ) (9 6)

x 3

x 6

(3 2 )3 (6 2 )3
27 108 135

Actual value is given by


9

x3
2
3 x dx 3

93 33

234
3

Truncation error is then


234 135 99

Can you find the


63 truncation error with 4

Root Finding
find x, f is a scalar real-valued
function of a single real-valued
variable

f ( x) 0

64

Root Finding
Bisection Method

Basis of Bisection Method


Theorem An equation f(x)=0, where f(x) is a real continuous
function, has at least one root between xl and xu if f(xl)
f(xu) < f(x)
0.

x
xu

Figure 1 At least one root exists between the two points if the
function is real, continuous, and changes sign.

Basis of Bisection Method


f(x)

xu

f x
Figure 2 If function
does not change sign between two
f x of
0the equation
points, roots
may still exist between
the two points.

Basis of Bisection Method


f(x)

f(x)

xu

xu
x

f x
Figure 3 If the function
does not change sign between two
0 for the equation
f xroots
points, there may not be any
between the two points.

Basis of Bisection Method


f(x)

xu

f x
Figure 4 If the function
changes sign between two points,
more than one root for the equation
may exist between
f x 0
the two points.

Algorithm for Bisection


Method

Step 1
Choose xl and xu as two guesses for the root such
that f(xl) f(xu) < 0, or in other words, f(x) changes
sign between xl and xu. This was demonstrated in
Figure 1. f(x)

x
xu

Figure 1

Step 2
Estimate the root, xm of the equation f (x) = 0 as
the mid point between xl and xu as
f(x)

x xu
xm =
2
x

xm
xu

Figure 5

Estimate of xm

Step 3
Now check the following
a) If f xl f xm 0 ,
then the root lies between xl
and xm; then xl = xl ; xu = xm.

f
x
f
x
0 ,
l
m
b) If
then the root lies between
xm and xu; then xl = xm; xu = xu;
f xl f xm 0

c) If
then the root is xm. Stop
the algorithm if this is true.

Step 4
Find the new estimate of the root
x xu
xm =
2

Find the absolute relative approximate error


a

old
x new

x
m
m

new
m

100

where
xmold previous estimate of root
xmnew current estimate of root

Step 5
Compare the absolute relative approximate error
a
with the pre-specified errortolerance
.
s
Yes

Go to Step 2 using
new upper and lower
guesses.

No

Stop the algorithm

Is a s ?

Note one should also check whether the number


of iterations is more than the maximum number of
iterations allowed. If so, one needs to terminate
the algorithm and notify the user about it.

Example 1
You are working for a company that makes floats
for ABC commodes. The floating ball has a
specific gravity of 0.6 and has a radius of 5.5 cm.
You are asked to find the depth to which the ball
is submerged when floating in water.

Figure 6 Diagram of the floating ball

Vw w g Vc g
Vc 1 / 3h 2 (3R h )
xh
R 5.5cm

0.6
w
4
R 3g 1 / 3x 2 (3R x ) w g
3
4

x 0.165 x 3.993 10 0
3

a) Use the bisection method of finding roots of


equations to find the depth x to which the ball is
submerged under water. Conduct three iterations
to estimate the root of the above equation.
b) Find the absolute relative approximate error at
the end of each iteration, and the number of
significant digits at least correct at the end of
each iteration.

Example 1 Cont.
From the physics of the problem, the ball would be
submerged between x = 0 and x = 2R,
where R = radius of the ball,
that is
0 x 2R

0 x 2 0.055
0 x 0.11

Figure 6 Diagram of the floating ball

Example 1 Cont.
Solution

To aid in the
understanding of how
this method works to find
the root of an equation,
the graph of f(x) is shown
to the right,

x x 3 0.165 x 2 3.993 10- 4


fwhere
Figure 7 Graph of the function f(x)

Example 1 Cont.
Let us assume

x 0.00
xu 0.11
Check if the function changes sign between xl
and xu .
3
2
4

f xl f 0 0 0.165 0 3.993 10 3.993 10 4


f xu f 0.11 0.11 0.165 0.11 3.993 10 4 2.662 10 4
3

Hence

f xl f xu f 0 f 0.11 3.993 10 4 2.662 10 4 0

So there is at least on root between xl and xu, that is between 0


and 0.11

Example 1 Cont.

Figure 8 Graph demonstrating sign change between


initial limits

Example 1 Cont.
Iteration 1
The estimate of the root isxm

x xu 0 0.11

0.055
2
2

f xm f 0.055 0.055 0.165 0.055 3.993 10 4 6.655 10 5


3

f xl f xm f 0 f 0.055 3.993 10 4 6.655 10 5 0

Hence the root is bracketed between xm and xu, that is,


between 0.055 and 0.11. So, the lower and upper limits of the
new bracket
are , x 0.11
x 0.055
l

At this point, the absolute relative approximate error


cannot be calculated as we do not have a previous
approximation.

Figure 9 Estimate of the root for Iteration 1

Example 1 Cont.
Iteration 2
xm
The estimate of the root is

x xu 0.055 0.11

0.0825
2
2

f x m f 0.0825 0.0825 0.165 0.0825 3.993 10 4 1.622 10 4


3

f xl f x m f 0.055 f (0.0825) 1.622 10 4 6.655 10 5 0

Hence the root is bracketed between xl and xm, that is,


between 0.055 and 0.0825. So, the lower and upper limits of
the new
x bracket
0.055, xare 0.0825
l

Example 1 Cont.

Figure 10 Estimate of the root for Iteration 2

Example 1 Cont.
The absolute relative approximate error
a
Iteration 2 is

at the end of

xmnew xmold
a
100
new
xm

0.0825 0.055
100
0.0825
33.333%

None of the significant digits are at least correct in the estimate


root of xm = 0.0825 because the absolute relative approximate
error is greater than 5%.

Example 1 Cont.
x xu 0.055 0.0825

0.06875
2
2

Iteration 3
The estimate of the root xism

f xm f 0.06875 0.06875 0.165 0.06875 3.993 10 4 5.563 10 5


3

f xl f xm f 0.055 f 0.06875 6.655 10 5 5.563 10 5 0

Hence the root is bracketed between xl and xm, that is,


between 0.055 and 0.06875. So, the lower and upper limits of
the new
x bracket
0.055, xare
0.06875
l

Example 1 Cont.

Figure 11 Estimate of the root for Iteration 3

Example 1 Cont.
The absolute relative approximate error
a
Iteration 3 is

at the end of

xmnew xmold
a
100
new
xm

0.06875 0.0825
100
0.06875
20%

Still none of the significant digits are at least correct in the


estimated root of the equation as the absolute relative
approximate error is greater than 5%.
Seven more iterations were conducted and these iterations are
shown in Table 1.

Table 1 Cont.
Table 1 Root of f(x)=0 as function of number of iterations for
bisection method.

Table 1 Cont.
Hence the number of significant digits at least correct is given
by the largest value or m for which

a 0.5 10 2 m
0.1721 0.5 10 2 m
0.3442 10 2 m

log 0.3442 2 m

m 2 log 0.3442 2.463

So

m2

The number of significant digits at least correct in the


estimated root of 0.06241 at the end of the 10th iteration is 2.

function bisect(a,b,tol,n)
fprintf('%2.0f %10.4f %10.4f %12.6f
% Bisection method for solving the nonlinear
%10.6f %10.6f\n',iter,a,b,c,w,err)
%equation f(x)=0.
if (w*u<0)
function ft= f(x)
b=c;v=w;
ft= x^3-0.165*x^2+0.0003993;
end
end
if (w*u>0)
a0=a;
a=c;
b0=b;
u=w;
iter=0;
end
u= f(a);
iter=iter+1;
v=f(b);
c=(a+b)*0.5;
c=(a+b)*0.5;
err=abs(b-a)*0.5;
err=abs(b-a)*0.5;
end
disp('______________________________________') if (iter>n)
disp(' iter a
b
c
f(c)
|b- disp(' Method failed to converge')
a|/2 ')
end
disp('_____________________________________')else
fprintf('\n')
disp(' The method cannot be applied
if (u*v<=0)
f(a)f(b)>0')
while (err>tol)&(iter<=n)
end
w= f(c);
end

xl = 0. xu = 5 tol = 0.01 n=100


The method cannot be applied f(a)f(b)>0
xl= 0 xu =0.11 tol = 0.01 n=100
iter a
b
c
f(c)
0
0.0000
0.1100
0.055000 0.000067
1
0.0550
0.1100
0.082500 -0.000162
2
0.0550
0.0825
0.068750 -0.000056

xl= 0
iter
0
1
2
3
4
5
6
7
8
9

|b-a|/2
0.055000
0.027500
0.013750

xu =0.11 tol = 0.01 n=100


a
0.0000
0.0550
0.0550
0.0550
0.0619
0.0619
0.0619
0.0619
0.0623
0.0623

b
0.1100
0.1100
0.0825
0.0688
0.0688
0.0653
0.0636
0.0627
0.0627
0.0625

c
0.055000
0.082500
0.068750
0.061875
0.065312
0.063594
0.062734
0.062305
0.062520
0.062412

f(c)
0.000067
-0.000162
-0.000056
0.000004
-0.000026
-0.000011
-0.000003
0.000001
-0.000001
-0.000000

|b-a|/2
0.055000
0.027500
0.013750
0.006875
0.003438
0.001719
0.000859
0.000430
0.000215
0.000107
94

Advantages
Always convergent
The root bracket gets halved with
each iteration - guaranteed.

Drawbacks

Slow convergence
If one of the initial guesses is close
to the root, the convergence is
slower

Drawbacks (continued)
If a function f(x) is such that it just
touches the x-axis it will be unable
to find the lower and upper guesses.
f(x)

f x x
x

Drawbacks (continued)

Function changes sign but root does


not exist
f(x)

1
f x
x
x

Newton-Raphson
Method

99

Newton-Raphson Method
f(x)

x f x

f(xi)

i,

f(xi )
xi 1 = xi f (xi )

f(xi-1)

xi+2

100

xi+1

xi

Figure 1 Geometrical illustration of the Newton-Raphson


method.
http://numericalmethods.eng.usf.edu

Derivation
f(x)

f(xi)

tan(

AB
AC

f ( xi )
f ' ( xi )
xi xi 1
C

xi+1

xi

f ( xi )
xi 1 xi
f ( xi )

Figure 2 Derivation of the Newton-Raphson method.

101
http://numericalmethods.eng.usf.edu

Algorithm for Newton-Raphson


Method

102
http://numericalmethods.eng.usf.edu

Step 1

Evaluat
e

103

f (x)

symbolically.

http://numericalmethods.eng.usf.edu

Step 2
xi
Use an initial guess of the root,
new value of thexi root,
, as
1

, to estimate the

f xi
xi 1 = xi f xi

104

http://numericalmethods.eng.usf.edu

Step 3
a
Find the absolute relative approximate error

as

xi 1- xi
a =
100
xi 1

105

http://numericalmethods.eng.usf.edu

Step 4
Compare the absolute relative approximate error
with the pre-specified relative error tolerances .
Yes
Is a s ?
No

Go to Step 2 using
new estimate of the
root.
Stop the algorithm

Also, check if the number of iterations has


exceeded the maximum number of iterations
allowed. If so, one needs to terminate the
algorithm and notify the user.
106

http://numericalmethods.eng.usf.edu

Example 1
You are working for that makes floats for ABC
commodes. The floating ball has a specific
gravity of 0.6 and has a radius of 5.5 cm. You are
asked to find the depth to which the ball is
submerged when floating in water.

107

Figure 3 Floating ball


http://numericalmethods.eng.usf.edu
problem.

Example 1 Cont.
The equation that gives the depth x in meters to
which the ball is submerged under water is given
by

f x x 3-0.165 x 2+3.993 10 - 4

108

Figure 3 Floating ball


problem.
Use the Newtons method of finding roots of equations to find
a) the depth x to which the ball is submerged under water.
Conduct three iterations to estimate the root of the above
equation.
b) The absolute relative approximate error at the end of each
iteration, and
c) The number of significant digits at least correct at the end of

http://numericalmethods.eng.usf.edu

Example 1 Cont.
Solution
To aid in the
understanding of how
this method works to find
the root of an equation,
the graph of f(x) is shown
to the right,
where

f x x 3-0.165 x 2+3.993 10 - 4
Figure 4 Graph of the function
f(x)
109

Example 1 Cont.
Solve for f ' x

f x x 3-0.165 x 2+3.993 10- 4


f ' x 3x 2 -0.33x

x 0
Let us assume the initial guess of the rootf of
x0is 0.05m
. This is a reasonable guess
x0
x 0.11m
(discuss
why
and
are not good choices) as the
extreme values of the depth x would be 0 and
the diameter (0.11 m) of the ball.
110

Example 1 Cont.
Iteration 1
The estimate of the root is

f x0
x1 x0
f ' x0
3
2

0.05 0.165 0.05 3.993 10 4


0.05
2
3 0.05 0.33 0.05

1.118 10 4
0.05
9 10 3
0.05 0.01242
0.06242

111

Example 1 Cont.

Figure 5 Estimate of the root for the first


112
iteration.

Example 1 Cont.
The absolute relative approximate error
a
Iteration 1 is

at the end of

x1 x0
100
x1

0.06242 0.05
100
0.06242
19.90%

The number of significant digits at least correct is 0, as you


need an absolute relative approximate error of 5% or less for
at least one significant digits to be correct in your result.

113

Example 1 Cont.
Iteration 2
The estimate of the root is

f x1
x2 x1
f ' x1

3
2

0.06242 0.165 0.06242 3.993 10 4


0.06242
2
3 0.06242 0.33 0.06242

3.97781 10 7
0.06242
8.90973 10 3
0.06242 4.4646 10 5

0.06238
114

Example 1 Cont.

Figure 6 Estimate of the root for the


Iteration 2.

115

Example 1 Cont.
The absolute relative approximate error
a
Iteration 2 is

at the end of

x2 x1
100
x2

0.06238 0.06242
100
0.06238
0.0716%

a 0.5 10 2 m
The maximum value of m for which
is
2.844. Hence, the number of significant digits at least
correct in the answer is 2.

116

Example 1 Cont.
Iteration 3
The estimate of the root is

x3 x2

f x2
f ' x2

3
2

0.06238 0.165 0.06238 3.993 10 4


0.06238
2
3 0.06238 0.33 0.06238

4.44 10 11
0.06238
8.91171 10 3
0.06238 4.9822 10 9
0.06238

117

Example 1 Cont.

Figure 7 Estimate of the root for the


Iteration 3.

118

Example 1 Cont.
The absolute relative approximate error
a
Iteration 3 is

at the end of

x2 x1
100
x2

0.06238 0.06238
100
0.06238
0%

The number of significant digits at least correct is 4, as


only 4 significant digits are carried through all the
calculations.

119

Advantages and
Drawbacks of Newton
Raphson Method

120

Advantages
Converges fast (quadratic
convergence), if it converges.
Requires only one guess

121

Drawbacks
1. Divergence at inflection points
Selection of the initial guess or an iteration value of the
x function
root that is close to the inflection point off the
may start diverging away from the root in ther NewtonRaphson method.
3
f x x 1 0.512 0
For example, to find the root of the equation
3
3
x 1 0.512
.
x x i
i 1

3 xi 1

The Newton-Raphson method reduces to


.

x 1

Table 1 shows the iterated values of the root of the equation.


The root starts
at Iteration 6 because the previous
x 0to.2diverge
.
estimate of 0.92589 is close to the inflection122
point

Drawbacks Inflection
Points
Table 1 Divergence near inflection
point.
Iteration
xi
Number
0

5.0000

3.6560

2.7465

2.1084

1.6000

0.92589

30.119

19.746

18

0.2000

Figure 8 Divergence at inflection


point for f x x 1 3 0.512 0
123

Drawbacks Division by
Zero
2. Division by zero
For the equation
f x x 3 0.03 x 2 2.4 10 6 0
the Newton-Raphson
method reduces to
xi3 0.03 xi2 2.4 10 6
xi 1 xi
3 xi2 0.06 xi

For x0 0 or x0 0.02 , the


denominator will equal
zero.

Figure 9 Pitfall of division by


zero
or near a zero
number
124

Drawbacks Oscillations near


local maximum and minimum
3. Oscillations near local maximum and minimum
Results obtained from the Newton-Raphson method
may oscillate about the local maximum or minimum
without converging on a root but converging on the
local maximum or minimum.
Eventually, it may lead to division by a number close to
zero and may diverge.
2
For example forf x x 2 0
real roots.

the equation has no

125

Drawbacks Oscillations near


local maximum and minimum
Table 3 Oscillations near local
maxima and mimima in NewtonRaphson method.
Iteration
xi
f xi a %
Number

0
1
2
3
4
5
6
7
8
9

1.0000
0.5
1.75
0.30357
3.1423
1.2529
0.17166
5.7395
2.6955
0.97678

3.00
2.25
5.063
2.092
11.874
3.570
2.029
34.942
9.266
2.954

300.00
128.571
476.47
109.66
150.80
829.88
102.99
112.93
175.96

Figure 10 Oscillations around


x x2 2
local
minimaf for
.
126

Drawbacks Root Jumping


4. Root Jumping
f x
In some cases where the function
is oscillating and has a
number of roots, one may choose an initial guess close to a root.
However, the guesses may jump and converge to some other
f(x)
root.


1.5

For
f example
x sin x

0.5

Choose
x0 2.4

7.539822
x0

It will converge to

-2

-0.06307

0.5499

4.461

7.539822

-0.5

-1

-1.5

x 2 6.2831853 Figure 11 Root jumping from

instead of

10

intended
location of root
f x 127
sin x 0
for
.

Secant Method

Secant Method
Derivation
f(x)

x f x

f(xi)

i,

Newtons Method
f(xi )
xi 1 = xi f (xi )

Approximate the
f ( xi ) f ( xi 1 )
derivative
f ( x )
i

f(xi-1)

xi+2

xi+1

xi

(1)

Figure 1 Geometrical illustration


of the Newton-Raphson method.

xi xi 1

(2)

Substituting Equation
(2) into Equation (1)
gives the Secant
method
f ( xi )( xi xi 1 )
xi 1 xi
f ( xi ) f ( xi 1 )

Secant Method
Derivation
The secant method can also be derived from geometry:
f(x)

f(xi)

The Geometric Similar Triangles


AB DC

AE DE
can be written as

f ( xi )
f ( xi 1 )

xi xi 1 xi 1 xi 1

f(xi-1)

xi+1

E D
xi-1

A
xi

Figure 2 Geometrical
representation of the Secant
method.

On rearranging, the secant


method is given as

xi 1 xi

f ( xi )( xi xi 1 )
f ( xi ) f ( xi 1 )

Algorithm for Secant


Method

Step 1
Calculate the next estimate of the root from two initial guesses

xi 1

f ( xi )( xi xi 1 )
xi
f ( xi ) f ( xi 1 )

Find the absolute relative approximate error

xi 1- xi
a =
100
xi 1

Step 2
Find if the absolute relative approximate error is
greater than the prespecified relative error
tolerance.
If so, go back to step 1, else stop the algorithm.
Also check if the number of iterations has
exceeded the maximum number of iterations.

Example 1
You are working for that makes floats for ABC
commodes. The floating ball has a specific
gravity of 0.6 and has a radius of 5.5 cm. You are
asked to find the depth to which the ball is
submerged when floating in water.

Figure 3 Floating Ball


Problem.

Example 1 Cont.
The equation that gives the depth x to which the ball is
submerged under water is given by

f x x 3-0.165 x 2+3.993 10 - 4
Use the Secant method of finding roots of
equations to find the depth x to which the ball is
submerged under water.
Conduct three iterations to estimate the root of
the above equation.
Find the absolute relative approximate error
and the number of significant digits at least
correct at the end of each iteration.

Example 1 Cont.
Solution
To aid in the
understanding of how
this method works to find
the root of an equation,
the graph of f(x) is shown
to the right,
where
f x x 3-0.165 x 2+3.993 10- 4
Figure 4 Graph of the function
f(x).

Example 1 Cont.
0
f xof
Let us assume the initial guesses of the root
as x1 0.02 and x0 0.05.
Iteration 1
The estimate of the root is

x1 x0

f x0 x0 x1
f x0 f x1

0.05 0.165 0.05

3.993 10 4 0.05 0.02


0.05
2
2
0.053 0.165 0.05 3.993 10 4 0.023 0.165 0.02 3.993 10 4
0.06461
3

Example 1 Cont.
a
The absolute relative approximate error
end of Iteration 1 is

at the

x1 x0
100
x1

0.06461 0.05

100
0.06461
22.62%
The number of significant digits at least correct is 0,
as you need an absolute relative approximate error
of 5% or less for one significant digits to be correct
in your result.

Example 1 Cont.

Figure 5 Graph of results of


Iteration 1.

Example 1 Cont.
Iteration 2
The estimate of the root is
x2 x1

f x1 x1 x0
f x1 f x0

0.06461 0.165 0.06461

3.993 10 4 0.06461 0.05


0.06461
2
2
0.064613 0.165 0.06461 3.993 10 4 0.053 0.165 0.05 3.993 10 4
0.06241
3

Example 1 Cont.
a
The absolute relative approximate error
end of Iteration 2 is

at the

x2 x1
100
x2

0.06241 0.06461

100
0.06241
3.525%
The number of significant digits at least correct is 1,
as you need an absolute relative approximate error
of 5% or less.

Example 1 Cont.

Figure 6 Graph of results of


Iteration 2.

Example 1 Cont.
Iteration 3
The estimate of the root is
x3 x2

f x2 x2 x1
f x2 f x1

0.06241 0.165 0.06241

3.993 10 4 0.06241 0.06461


0.06241
2
2
0.062413 0.165 0.06241 3.993 10 4 0.053 0.165 0.06461 3.993 10 4
0.06238
3

Example 1 Cont.
a
The absolute relative approximate error
end of Iteration 3 is

at the

x3 x2
100
x3

0.06238 0.06241

100
0.06238
0.0595%
The number of significant digits at least correct is 5,
as you need an absolute relative approximate error
of 0.5% or less.

Iteration #3

Figure 7 Graph of results of


Iteration 3.

Advantages

Converges fast, if it converges


Requires two guesses that do not need
to bracket the root

Drawbacks
2

1
f ( x)
f ( x)

f ( x)

10
10

f(x)
prev. guess
new guess

x x guess1 x guess2

Division by zero

10
10

f x Sin x 0

Drawbacks (continued)
2

1
f ( x)
f ( x)
f ( x)

secant ( x)
f ( x)
1

10
10

f(x)
x'1, (first guess)
x0, (previous guess)
Secant line
x1, (new guess)

10

x x 0 x 1' x x 1

Root Jumping

10

f x Sinx 0

You might also like