Chapter 2
Programming and Software
Packages and Programming
Structured Programming
Modular Programming
Excel
Matlab
Mathcad
Other Languages and Libraries
Chapter 3
Approximations and
Round-Off Errors
Example: modeling of a falling parachutist
• For many engineering problems, we cannot obtain analytical
solutions.
• Numerical methods yield approximate results, results that are
close to the exact analytical solution. We cannot exactly compute
the errors associated with numerical methods.
• Question: How much error is in our calculation?
Is it tolerable?
• how to define error, how to evaluate error?
• Accuracy. How close is a computed or measured value to the true
value
• Precision (or reproducibility). How close is a computed or
measured value to the previously computed or measured values.
• Inaccuracy (or bias). A systematic deviation from the actual value.
• Imprecision (or uncertainty). Magnitude of scatter.
Imprecision (or uncertainty
Inaccurate (or biased).
Error Definitions
Absolute error vs. Relative error
True Value = Approximation + Error
Et = True value – Approximation (+/-)
True error
Ex) Bridge length
True value=10,000m, Measured value=9,999m
Et=10,000-9,999=1m
Ex) Pencil length
True value=10cm, Measured value=9cm
Et=10-9=1cm
Error Definitions
true error
True fractional relative error =
true value
true error
True percent relative error, ε t = ×100%
true value
Ex) Bridge length
True value=10,000m, Measured value=9,999m
et=(10,000-9,999)/10,000*100=0.01%
Ex) Pencil length
True value=10cm, Measured value=9cm
et=(10-9)/10*100=10%
• True value???
• For numerical methods, the true value will be known only when
we deal with functions that can be solved analytically (simple
systems). In real world applications, we usually not know the
answer a priori. Then
Ea = Present Approximation − Previous Approximation
• Iterative approach, example Newton’s method
Present approximation - Previous approximation
εa = ×100%
Present approximation
Example: Error estimates for iterative methods
x x 2 x3 xn
e = 1+ x + + + ⋅⋅⋅⋅ +
2 3! n!
n =1 e 0.5 = 1 ε =?
1.5 − 1
n=2 e 0.5
= 1 + 0 .5 = 1 .5 ε= = 0.333
1.5
0.5 2 1.625 − 1.5
n=3 e 0 .5
= 1 + 0.5 + = 1.625 ε= = 0.0769
2 1.625
0.5 2 0.53 1.64583333 − 1.625
n=4 e 0.5
= 1 + 0.5 + + = 1.64583333 ε= = 0.0127
2 3! 1.64583333
• Use absolute value.
• Computations are repeated until stopping criterion is satisfied.
ε a 〈ε s Pre-specified % tolerance based on
the knowledge of your solution
0.5 2 0.53 1.64583333 − 1.625
n=4 e 0.5
= 1 + 0.5 + + = 1.64583333 ε= = 0.0127
2 3! 1.64583333
n=5 e 0.5 = 1.648438 ε = 0.001579779
n=6 e 0.5 = 1.648698 ε = 0.000157953
ε = 0.000013163
n=7 e 0.5 = 1.64872
n=8 e 0.5 = 1.648721 ε = 0.00000094018
ε s = 0.00001 or 0.001%
Source of Error
(1) Round-off error
Error created due to approximate representation of numbers
Ex) 1/3~0.333:
round-off error=1/3-0.333=0.00033333
(2) Truncation error
Error created by truncating (or approximating) a mathematical
procedure
Ex) x x 2 x3
e = 1+ x + + + ⋅⋅⋅⋅
2 3!
using 3-terms, 0.52
e 0.5 = 1 + 0.5 + = 1.625
2
truncation error=e0.5-1.625=0.0237
Significant Figures
• Number of significant figures indicates precision. Significant
digits of a number are those that can be used with confidence.
53,800 How many significant figures?
5.38 x 104 3
5.380 x 104 4
5.3800 x 104 5
Zeros are sometimes used to locate the decimal point not
significant figures.
0.00001753 4
0.0001753 4
0.001753 4
538,000,000
5.38 x 108
5.380 x 108
5.3800 x 108
5 3 8 0 0 0 0 0 0 ???
0 0 0 0 8 5 3 8
Exponent mantissa
Sign of
number
Sign of exponent
0 0 0 0 8 5 3 8
Exponent mantissa
Sign of
number
Sign of exponent
절대값이 가장 큰 수 00999999=9.99x10999
절대값이 가장 작은 수 01999100=1.00x10-999
IEEE-754 Standard for Single Precision
32bit for single precision
64bit for single precision
Introduction to Binary Numbers
• Base-10 (0-9), decimal point
256.79 = 2 ×10 2 + 5 ×101 + 6 ×100 + 7 ×10-1 + 9 ×10-2
Base-2 to Base-10
• Binary numbers: Base-2 (0-1), radix point
(1011.0011)2 = 1× 23 + 0 × 22 + 1× 21 + 1× 20 + 0 × 2-1 + 0 × 2-2 + 1× 2-3 + 1× 2-4
= (11.1887)10
Base-10 to Base-2
Ex) (11.1875)10=(?.?)2
(11.)10=(?.)2
Quotient Remainder
11/2 5 1=a0
5/2 2 1=a1
2/2 1 0=a2
1/2 0 1=a3
(11)10=(a3a2a1a0)2
=(1011)2
• (.1875)10=(.?)2
Number Number after . Number before .
0.1875×2 0.375 0.375 0=a-1
0.375×2 0.750 0.750 0=a-2
0.75×2 1.50 0.500 1=a-3
0.5×2 1.00 0.000 1=a-4
• (.1875)10=(.a-1a-2a-3a-4)2
=(.0011)2
Thus, (11.1875)10=(1011.0011)2
Floating point representation
Ex) 8 bit word
Exponent mantissa
Sign of
number
Sign of exponent
Ex) (-13.9)10 floating point representation?
(13)10=(1101)2
(0.9)10=(0.11100)2
(-13.9)10=(-1101.11100)2
=(-1.10111100)2×23
~(-1.101)2×2011
1 0 0 1 1 1 0 1
Sign of Exponent mantissa
number
Sign of exponent
Ex) (-13.9)10 floating point representation?
Check!
-(1.101)2×23= -(1×20+1×2-1+0×2-2+1×2-3)×23
=-(1.625)10×23
=-(13)10
Round-off error=(-13.9)~(-13)=0.9
1 0 0 1 1 1 0 1
Sign of Exponent mantissa
number
Sign of exponent
0/1 0 1 1 1 1 1 1
(1.11)2×2111= (1.75)10×27
=(224)10
Overflow: Any number larger than the largest number that
can be expressed on a computer will result in an overflow.
0/1 1 1 1 1 0 0 0
(1.00)2×2-111= (1.00)10×2-7
=(0.007813)10
Underflow: Any positive number smaller than the smallest
number that can be represented on a computer will result
an underflow.
Floating point representation
Ex) Biased Exponent: 8 bit word
Exponent mantissa
Sign of
number Min=0 (0000)
Max=15 (1111)
15/2=7.5
0 to 15 (biased)
-7 to 8 (unbiased)
Ex)
(−13.9)10 = −(1.101) 2 × 2(11) 2
1 1 0 1
Exponent ?
Unbiased to biased -> add 7
(11)2 = 3 → 3 + 7 = 10 = (1010) 2
1 1 0 1 0 1 0 1
IEEE-754 Standard for Single Precision
32bit for single precision
Biased Mantissa, m (23bits)
Exponent, e (8 bits)
(11111111)=255
Sign of 255/2=127.5
Number, s
value = (−1) s × (1.m )2 × 2e −127