Binary Error Root
Binary Error Root
Numerical Methods
Fall 2015
Binary Representation
Floating Point
15900000000000000
14
could be represented as
Mantissa
Exponent
159
* 1014
15.9 * 1015
1.59 * 1016
A calculator might display 159 E14
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.
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
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
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
Exercise-1:
Convert 235.8723 to a binary representation
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
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.
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.
Convert
1 10000001
01101000000000000000000
To a decimal representation
Errors
23
10/19/15
24
2! 4! 6!
x3 x5 x7
sin( x) x
3! 5! 7!
x2 x3
e 1 x
2! 3!
x
10/19/15
25
3!
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
28
2!
3!
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
So,
10/19/15
x 2 x3
f ( x) 1 x ...
2! 3!
30
(n 1)!
where
x c xh
31
2!
3!
4!
x 0 is given by
5!
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
34
Measuring Errors
35
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)
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
3
.
5
e
of
f ' ( 2)
is
9.5140
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
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
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
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
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
53
2 1.4142...
54
55
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
e 1 x
2!
3!
....................
x
x
Truncation Error e 1 x
2!
58
secant line
P
tangent line
f (x)
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
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
x 3
x 6
(3 2 )3 (6 2 )3
27 108 135
x3
2
3 x dx 3
93 33
234
3
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
x
xu
Figure 1 At least one root exists between the two points if the
function is real, continuous, and changes sign.
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.
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.
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.
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
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
Is a s ?
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.
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
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
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,
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
Hence
Example 1 Cont.
Example 1 Cont.
Iteration 1
The estimate of the root isxm
x xu 0 0.11
0.055
2
2
Example 1 Cont.
Iteration 2
xm
The estimate of the root is
x xu 0.055 0.11
0.0825
2
2
Example 1 Cont.
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%
Example 1 Cont.
x xu 0.055 0.0825
0.06875
2
2
Iteration 3
The estimate of the root xism
Example 1 Cont.
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%
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
So
m2
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
iter
0
1
2
3
4
5
6
7
8
9
|b-a|/2
0.055000
0.027500
0.013750
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)
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
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 )
101
http://numericalmethods.eng.usf.edu
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
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
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
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
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
1.118 10 4
0.05
9 10 3
0.05 0.01242
0.06242
111
Example 1 Cont.
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%
113
Example 1 Cont.
Iteration 2
The estimate of the root is
f x1
x2 x1
f ' x1
3
2
3.97781 10 7
0.06242
8.90973 10 3
0.06242 4.4646 10 5
0.06238
114
Example 1 Cont.
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
4.44 10 11
0.06238
8.91171 10 3
0.06238 4.9822 10 9
0.06238
117
Example 1 Cont.
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%
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
x 1
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
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
125
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
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
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)
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)
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.
xi 1 xi
f ( xi )( xi xi 1 )
f ( xi ) f ( xi 1 )
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 )
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.
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
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.
Example 1 Cont.
Iteration 2
The estimate of the root is
x2 x1
f x1 x1 x0
f x1 f x0
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.
Example 1 Cont.
Iteration 3
The estimate of the root is
x3 x2
f x2 x2 x1
f x2 f x1
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
Advantages
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