Chapter 1 Introduction
Chapter 1 Introduction
CHAPTER-ONE
3/22/2024 1
CONTENTS
• Introduction to Numerical Methods
• Measuring Errors
• Sources of Errors
• Taylor Theorem Revision
• Reading Assignment
✓ Binary Representation of
Numbers
✓ Floating Point Representation
✓ Propagation of Errors
3/22/2024 2
Part I
Introduction to Numerical Methods
• A numerical method is a complete and unambiguous set of
procedures for the solution of a problem, together with computable
error estimates.
• It uses algorithms for approximating solutions to problems.
• Mathematical models are an integral part in solving engineering
problems.
• It results in need of using mathematical procedures
➢ Differentiation and integration
➢Non-linear equations,
➢simultaneous linear equations,
➢curve fitting by interpolation or regression,
3/22/2024 3
Why use Numerical Methods?
x u2
1 −
2
e 2 du
−
3/22/2024 4
Steps in Solving Engineering Problem
.
Problem Description
Mathematical Model
3/22/2024 5
MEASURING ERRORS
Why do we measure errors?
1) To determine the accuracy of numerical results.
2) To develop stopping criteria for iterative algorithms.
3/22/2024 6
True Error
3/22/2024 7
Example—True Error
3/22/2024 8
Example (cont.)
Solution:
a) For x = 2 and h = 0.3
f (2 + 0.3) − f (2)
f ' (2)
0.3
f (2.3) − f (2)
=
0.3
7e0.5(2.3) − 7e0.5(2)
=
0.3
22.107 − 19.028
= = 10.263
0.3
3/22/2024 9
Example (cont.)
Solution:
b) The exact value of f ' (2) can be found by using
our knowledge of differential calculus.
f (x) = 7e0.5 x
f ' (x) = 7 0.5 e0.5x
= 3.5e0.5x
So the true value of f ' (2) is
f ' (2) = 3.5e0.5(2)
= 9.5140
True error is calculated as
Et = True Value – Approximate Value
= 9.5140 −10.263 = −0.722
3/22/2024 10
Relative True Error
• Defined as the ratio between the true error, and the true value.
True Error
Relative True Error ( t ) =
True Value
3/22/2024 11
Example—Relative True 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.
3/22/2024 13
Example—Approximate Error
3/22/2024 14
Example (cont.)
Solution: (cont.)
f (2.3) − f (2)
=
0.3
7e0.5(2.3) − 7e0.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)
f ' (2)
0.15
f (2.15) − f (2)
=
0.15
3/22/2024 15
Example (cont.)
Solution: (cont.)
7e 0.5(2.15) − 7e 0.5(2)
=
0.15
20.50 − 19.028
= = 9.8800
0.15
3/22/2024 16
Relative Approximate Error
Approximate Error
Relative Approximate Error ( a) =
Present Approximation
3/22/2024 17
Example—Relative Approximate Error
3/22/2024 18
Example (cont.)
Solution: (cont.)
Approximate Error
a =
Present Approximation
− 0.38300
= = −0.038765
9.8800
as a percentage,
a = − 0 . 0 3 8 7 6 5 1 0 0 % = − 3. 8765%
3/22/2024 19
How is Absolute Relative Error used as a
stopping criterion?
3/22/2024 20
Table of Values
h f (2) a m
0.3 10.263 N/A 0
3/22/2024 21
SOURCES OF ERROR
There are two sources of numerical error
1) Round off error
2) Truncation error
Round-off Error
▪ Caused by representing a number approximately
• Most decimal numbers cannot be represented with complete
accuracy in a computer.
• The numbers are stored in fixed-point format or floating-point
format.
1
0.333333
3
2 1.4142...
3/22/2024 22
Truncation error
• Error caused by truncating or approximating a mathematical
procedure.
Example of Truncation Error
• Taking only a few terms of a Maclaurin series to approximate
ex 2 3
x x
e x = 1+ x + + +..................
• If only 3 terms are used, 2! 3!
x − 1+ x + x 2
Truncation Error = e
2!
3/22/2024 23
Another Example of Truncation Error
secant line
P
tangent line
3/22/2024 24
Another Example of Truncation Error
90
y = x2
60
30
0 x
0 1.5 3 4.5 6 7.5 9 10.5 12
3/22/2024 25
Example 1 —Maclaurin series
f (x + x) − f (x)
Find f (3)
for f (x) = x 2 using f (x)
x
and x = 0.2 =
3.2 2 − 32
f (3 + 0.2) − f (3)
f (3) =
'
0.2 0.2
f (3.2) − f (3) 10.24 − 9 1.24
= = = = 6.2
0.2 0.2 0.2
y = x2
60
30
0 x
0 3 6 9 12
3/22/2024 28
Integration example (cont.)
Choosing a width of 3, we have
9
x 2
dx = (x 2
)
x=3
(6 − 3) + (x 2 )
x=6
(9 − 6)
3
Can you find the truncation error
= (3 )3 + (6 )3
2 2
with 4 rectangles?
= 27 +108 = 135 y
9 3 − 33
9 3 9
x dx = 3 = 3 = 234
x y = x2
2
60
3 3
234 − 135 = 99 0 x
0 1.5 3 4.5 6 7.5 9 10.5 12
3/22/2024 29
TAYLOR SERIES REVISITED
What is a Taylor series?
Some examples of Taylor series which you must have seen
x 2 x 4 x6
cos(x) = 1− + − +
2! 4! 6!
x3 x5 x 7
sin(x) = x − + − +
3! 5! 7!
x2 x3
e = 1+ x +
x
+ +
2! 3!
3/22/2024 30
General Taylor Series
3/22/2024 31
Example—Taylor Series
Find the value of f (6) given that f (4) = 125, f (4) = 74,
f (4) = 30, f (4) = 6 and all other higher order derivatives
of f (x) at x = 4 are zero.
Solution:
h2 h3
f (x + h ) = f (x )+ f (x)h+ f (x ) + f (x) +
2! 3!
x=4
h = 6−4 = 2
3/22/2024 32
Example (cont.)
Solution: (cont.)
Since the higher order derivatives are zero,
22 23
f (4+ 2 ) = f (4 )+ f (4)2+ f (4) + f (4)
2! 3!
2 2 23
f (6) = 125 + 74 (2)+ 30 + 6
2! 3!
= 125 + 148 + 60 + 8
= 341
Note that to find f (6) exactly, we only need the value
of the function and all its derivatives at some other
point, in this case x = 4
3/22/2024 33
Derivation for Maclaurin Series for ex
3/22/2024 34
Derivation (cont.)
3/22/2024 35
Error in Taylor Series
3/22/2024 36
Example—error in Taylor series
3/22/2024 37
Example—(cont.)
Solution:
Using (n + 1) terms of Taylor series gives error bound of
Rn (x) =
(x − h)n+1 (n+1)
f (c) x = 0, h = 1, f (x) = e x
(n + 1)!
Rn (0) =
(0 −1)n+1 (n+1)
(n + 1)! f (c)
=
( −1)n+1 c
(n +1)! e
Since
x c x+h
0 c 0 +1 Rn (0)
1 e
0 c 1 (n +1)! (n +1)!
3/22/2024 38
Example—(cont.)
Solution: (cont.)
So if we want to find out how many terms it would
require to get an approximation of e1 within a
magnitude of true error of less than 10−6 ,
e
10−6
(n +1)!
(n +1)! 106 e
(n +1)! 106 3
n9
So 9 terms or more are needed to get a true error
less than 10−6
3/22/2024 39
Part 2 Reading Assignment
BINARY REPRESENTATION
• In a binary system, we have the base made of only two digits 0
and 1. So it is a base 2 system.
• For example, look at the number 257.56. Each digit in 257.56
has a value of 0 through 9 and has a place value. It can be
written as
11/2 5 1 = a0
5/2 2 1 = a1
2/2 1 0 = a2
1/2 0 1 = a3
Hence
(11)10 = (a3 a2 a1a0 ) 2
= (1011)2
3/22/2024 41
Start
Integer N to be
Input (N) 10
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
3/22/2024 42
Fractional Decimal Number
to Binary
Table 2. Converting a base-10 fraction to binary representation.
Hence
(0.1875)10 = (a−1a−2a−3a− 4 )2
= (0.0011)2
3/22/2024 43
Start
Fraction F to be
Input (F) 10
converted to binary
format
i = −1
Multiply F by 2 to get
number before decimal,
S and after decimal, T
i = i −1, F = T
ai = R
No
Is T =0?
Yes
n=i
(F)10 = (a-1. . .a-n)2
STOP
3/22/2024 44
Decimal Number to Binary
(11.1875)10= ( ?.? )2
Since
(11)10 = (1011) 2
and
(0.1875)10 = (0.0011)2
we have
(11.1875)10 = (1011.0011)2
3/22/2024 45
All Fractional Decimal Numbers Cannot
be Represented Exactly
Table 3. Converting a base-10 fraction to approximate binary representation.
Number Number
Number after before
decimal Decimal
0.3 2 0.6 0.6 0 = a−1
0.6 2 1.2 0.2 1 = a−2
0.2 2 0.4 0.4 0 = a−3
0.4 2 0.8 0.8 0 = a−4
0.8 2 1.6 0.6 1 = a−5
3/22/2024 46
Another Way to Look at Conversion
3/22/2024 47
(0.1875)10 = 2−3 + 0.0625
=2 +2
−3 −4
(11.1875)10= (1011.0011)2
3/22/2024 48
FLOATING POINT REPRESENTATION
256.78 is written as + 2 . 5 6 7 8 1 0 2
0.003678 is written as + 3 . 6 7 8 1 0 − 3
− 256.78 is written as − 2 . 5 6 7 8 1 0 2
3/22/2024 49
Example
Example: For
− 2.5678102
= −1
m = 2.5678
e=2
3/22/2024 50
Floating Point Format for Binary Numbers
y = m 2e
= sign of number (0for + ve,1for - ve)
m = mantissa (1)2 m (10)2
3/22/2024 51
Example
9 bit-hypothetical word
▪ the first bit is used for the sign of the number,
▪ the second bit for the sign of the exponent,
▪ the next four bits for the mantissa, and
▪ the next three bits for the exponent
(54.75)10 = (110110.11)2 = (1.1011011)2 2 5
(1.1011)2 (101)2
We have the representation as
0 0 1 0 1 1 1 0 1
mantissa exponent
Sign of the Sign of the
number exponent
3/22/2024 52
Machine Epsilon
Defined as the measure of accuracy and found
by difference between 1 and the next number
that can be represented
3/22/2024 53
Example
3/22/2024 54
Relative Error and Machine Epsilon
0 1 0 1 1 0 1 1 0 0
Sign of the exponent mantissa
Sign of the
number
exponent
(1.1100)2 2 −(0110 ) 2
= 0.0274375
0.02832 − 0.0274375
a =
0.02832
3/22/2024 = 0.034472 2 −4 = 0.0625 55
IEEE-754 Floating Point Standard
• Standardizes representation of floating point
numbers on different computers in single and double
precision.
3/22/2024 56
IEEE-754 Format Single Precision
Value = (−1) (1 s
m)2
2.e'−127
3/22/2024 57
Example#1
1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
= (−1) (1.10100000)2 2
1 (10100010 ) 2 −127
3/22/2024 58
Example#2
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
− 5.5834 10 10
= ( − 1) 1
( 1. ? ) 2 ?
3/22/2024 59
Exponent for 32 Bit IEEE-754
8 bits would represent
0 e 255
Bias is 127; so subtract 127 from representation
−127 e 128
3/22/2024 60
Exponent for Special Cases
Actual range of e
1 e 254
e = 0 and e = 255 are reserved for special numbers
Actual range of e
− 126 e 127
3/22/2024 61
Special Exponents and Numbers
e = 0 all zeros
e = 255 all ones
s e m Represents
0 all zeros all zeros 0
1 all zeros all zeros -0
0 all ones all zeros
1 all ones all zeros −
0 or 1 all ones non-zero NaN
3/22/2024 62
IEEE-754 Format
Machine epsilon
mach = 2−23 = 1.19 10 −7
3/22/2024 63
PROPAGATION OF ERRORS
3/22/2024 64
Example 1:
Find the bounds for the propagation in adding two numbers. For example if one is
calculating X +Y where
X = 1.5 ± 0.05
Y = 3.4 ± 0.04
Solution
Maximum possible value of X = 1.55 and Y = 3.44
3/22/2024 65
Propagation of Errors In Formulas
f f f f
f X 1 + X 2 + .......+ X n−1 + X n
X 1 X 2 X n−1 X n
3/22/2024 66
Example 2:
Thus
1 2F F
E = 2 F + 3
h + 2 2
E
h E h E h E
1 2 72
= 0.9 + 0.0001
(4 10 ) (70 10 )
−3 2 9
(410 ) (70 10 )
−3 3 9
72
+ 1.5109
Solution
Let
z = x− y
Then
z z
z = x + y
x y
= (1)x + (−1)y
= x + y
So the relative change is
z x + y
=
z x− y
3/22/2024 70
Example 3:
For example if
x = 2 0.001
y = 2.003 0.001
z 0.001 + 0.001
=
z | 2 − 2.003 |
= 0.6667
= 66.67%
3/22/2024 71