You are on page 1of 43

Computational Methods

(EPCE 2204)
Credit: 3 Hrs (Lecture: 2 Hr Lab: 3 Hr)
__________________________________________
Chapter-One
Number System and Numerical Error Analysis
Outline:
1. Introduction
2. Significant figures
3. Representation of Integers and Fractions
4. Number Representation in Computers
5. Error problems
◼ Types of error
6. Algorithm for Conversion from one base to another
7. Computational Problems and Algorithms
8. Computational Efficiency Tamiru Getahun (MSc)
9. Computational Methods for Error Estimation ASTU
Number System and Numerical Error Analysis

Introduction
Why we do Numerical analysis ?

◼ Numerical analysis is a part of mathematics, but it works on


questions that are strongly related to the use of computers and to
applications from Science and Engineering.

◼ Using numerical analysis we will be able to handle large systems


of equations, non-linearities, complicated geometries and solving
engineering problems which have no analytical solution.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 2


Number System and Numerical Error Analysis

x0 \ f (x0 ) = 0.
◼ Roots of equations :
◼ We will be interested in methods for

solving
◼ These methods are very useful in

engineering projects, because in many


occasions it is not possible to solve the
design equations analytically.

◼ Optimization:

◼ Determine the value x0 leading


to the optimal value of f(x).
◼ These problems can be subject
to constraints.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 3


Number System and Numerical Error Analysis

◼ Systems of linear equations:


a11x1 + a12x2 = b1
a21x1 + a22x2 = b 2
◼ We will study methods for computing
the set of values that simultaneously
satisfy a system of algebraic equations.

◼ Applications: calculus of structures,


electric circuits, supply networks, fit of
curves, etc.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 4


Number System and Numerical Error Analysis

◼ Fitting curves. Fitting techniques can be divided into two groups:


◼ Regression. It is used when one has errors in the experimental data.
One looks for the curve showing the trend of the data.

◼ Interpolation. It is used to fit tabulated data and predict


intermediate values or extrapolated data.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 5


Number System and Numerical Error Analysis

◼ Ordinary differential
equations :

◼ They are very important because


many problems can be stated in
terms of variations and not in
terms of magnitudes.

◼ There are two types of problems:


Initial value problems, and
boundary value problems.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 6


Number System and Numerical Error Analysis

◼ Integration:
b
I =  f ( x)dx
a
◼ Determine the area f(x)
below a given curve.

Integral
◼ It has many applications
in engineering. Calculus
of centers of gravity.
Calculus of areas,
volumes, etc.
a b x
◼ It can also be used to
solve differential
equations.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 7


Number System and Numerical Error Analysis

◼ Partial differential
equations:

 2u  2u
+ = f ( x, y )
2 2
x y
y
◼ Used for characterizing
engineering problems
where the behavior of
the physical magnitude
can be expressed in
terms of speed change
with respect to two or
more variables.

◼ Approximation by finite x
differences or the finite
element method.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 8


Number System and Numerical Error Analysis
◼ Review of number system
What is Number System in Maths?
◼ A number system is defined as a system of writing to express numbers.
◼ It is the mathematical notation for representing numbers of a given set
by using digits or other symbols in a consistent manner.
◼ It provides a unique representation of every number and represents the
arithmetic and algebraic structure of the figures.
◼ It also allows us to operate arithmetic operations like addition,
subtraction, multiplication and division.
◼ The value of any digit in a number can be determined by:
• The digit
• Its position in the number
• The base of the number system

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 9


Number System and Numerical Error Analysis

Types of Number System


◼ There are various types of number system in mathematics.
◼ The four most common number system types are:
1. Decimal number system (Base- 10)
2. Binary number system (Base- 2)
3. Octal number system (Base-8)
4. Hexadecimal number system (Base- 16)

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 10


Number System and Numerical Error Analysis

Decimal Number System (Base 10 Number System)


◼ Decimal number system has base 10 because it uses ten digits from 0 to 9.
◼In the decimal number system, the positions successive to the left of the
decimal point represent units, tens, hundreds, thousands and so on.
◼ This system is expressed in decimal numbers.
◼ Every position shows a particular power of the base (10). For example
(1×103) + (4×102) + (5×101) + (7×100)
(1×1000) + (4×100) + (5×10) + (7×1)
1000 + 400 + 50 + 7
1457

Binary Number System (Base 2 Number System)


◼The base 2 number system is also known as the Binary number system wherein,
only two binary digits exist, i.e., 0 and 1. Specifically, the usual base-2 is a radix of 2.
.For example, 110101 is a binary number.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 11


Number System and Numerical Error Analysis

Octal Number System (Base 8 Number System)


◼ In the octal number system, the base is 8 and it uses numbers from 0 to 7
to represent numbers. Octal numbers are commonly used in computer
applications.
Hexadecimal Number System (Base 16 Number System)
◼ In the hexadecimal system, numbers are written or represented with base
16. In the hex system, the numbers are first represented just like in
decimal system, i.e. from 0 to 9. Then, the numbers are represented
using the alphabets from A to F. The below-given table shows the
representation of numbers in the hexadecimal number system.

Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F

Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 12


Number System and Numerical Error Analysis

Background: How are numbers stored in a computer?

• The fundamental unit, a "word," consists of a string of "bits" (binary digits).


• Because computers are made up of gates or switches which are either closed or
open, we work in binary or base 2 system.
• A number in base q will be denoted by
(anan-1...a1a0.b1b2..bk..)q

The conversion to base 10 is, by definition

(anan-1...a1a0.b1b2..bk..)q =anqn+an-1qn-1+...+a1q+a0q0+b1q-1+b2q-2+...

Example:
(1011.01)2=1x23+0x22+1x2+1x20+0x2-1+1x2-2=11.25

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 13


Number System and Numerical Error Analysis

Conversion from base 10 to base q.

This is the recipe for conversion:

Integer part: we have to divide the integer part by 2 (successively) and


to retain the fractional part in each step.

Fractional part: we have to multiply by 2 and to retain the integer part


in each step.

Example:

(26.1)10=(11010.00011)2

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 14


Number System and Numerical Error Analysis

Example1: Binary conversion to Decimal


Binary Number: 101012
Calculating Decimal Equivalent −

Step Binary Number Decimal Number


4 3 2 1 0
Step 1 101012 ((1 x 2 ) + (0 x 2 ) + (1 x 2 ) + (0 x 2 ) + (1 x 2 ))10

Step 2 101012 (16 + 0 + 4 + 0 + 1)10

Step 3 101012 2110

Method: using the bin2dec function.

The 'bin2dec' built-in function converts a binary number string to a decimal number.
Example:
bin_nr ='110101';
dec = bin2dec(bin_nr)
The result is dec = 53.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 15


Number System and Numerical Error Analysis
using a for-loop
>> bin_nr ='110101';
dec = 0;
for i = 1 : length(bin_nr)
dec = dec + str2num(bin_nr(i)) * 2^(length(bin_nr) - i);
end
dec
Example2: Decimal to binary conversion
◼ Decimal value= 10 and Calculating Binary Equivalent-
>> dec_nr = 10;
>> dec_nr = 10; i = 1;
bin = dec2bin(dec_nr) q = floor(dec_nr/2);
r = rem(dec_nr, 2);
bin(i) = num2str(r(i));
Matlab's answer is while 2 <= q
dec_nr = q;
bin = 1010 i = i + 1;
q = floor(dec_nr/2);
r = rem(dec_nr, 2);
bin(i) = num2str(r);
end
bin(i + 1) = num2str(q);
bin = fliplr(bin)

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 16


Number System and Numerical Error Analysis
Example3: Binary to hexadecimal numbers
◼ Binary number 10001011110101 and Calculating hexadecimal Equivalent-
◼ In Matlab, we can go from binary to decimal, and then,
from decimal to hexadecimal. We can embed one instruction into the
other, like this:
hex_str = dec2hex(bin2dec(bin_str))
◼ It’s important to remember that both binary
numbers and hexadecimal ones are treated as strings in Matlab.
>> bin_str = '10001011110101';
hex_str = dec2hex(bin2dec(bin_str))
Matlab’s answer is:
hex_str = 22F5

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 17


Number System and Numerical Error Analysis
Example 4: Hex to binary
◼ Hex number AF5 translates into the binary equivalent.
◼ To convert a value from hex to binary, you merely translate each
hex symbol to the equivalent 4-bit binary group.
◼ In Matlab, we can go from hexadecimal to decimal, and then,
from decimal to binary. We can embed one instruction into the other,
like this:
bin_str = dec2bin(hex2dec(hex_str))
◼ So, we can use this concept, like this:
>> hex_str = 'AF5';
bin_str = dec2bin(hex2dec(hex_str))
Matlab’s answer is:

bin_str = 101011110101

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 18


Number System and Numerical Error Analysis
◼ Significant figures of a number:
◼ Significant figures are the digits of a number that are meaningful in
terms of accuracy or precision.
◼ The significant figures of a number are those which can be used with
confidence.
◼ This concept has two important implications:
1. An approximation is acceptable when it is exact for a given number of
significant figures.
2. There are magnitudes or constants that cannot be represented exactly:

 = 3.14159265...
17 = 4.123105...
Accuracy: closeness of measured/computed values to the "true" value (vs.
inaccuracy or bias)
Precision: closeness of measured/computed values with each other (spread
or scatter), relates to the number of significant figures (vs.
imprecision or uncertainty)

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 19


Number System and Numerical Error Analysis

◼ Accuracy and precision:


◼ The errors associated with
measurements can be
characterized observing
their accuracy and
precision.

◼ Accuracy refers to how


close the value is to the
true value.

◼ Precision refers to how


close are different
measured values using the
same method. (a) Inaccurate & imprecise
(b) Accurate & imprecise
(c) Inaccurate & precise
(d) Accurate & precise
◼ Numerical methods must be sufficiently exact (without bias) and precise
to satisfy the requirements of engineering problems.
ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 20
Number System and Numerical Error Analysis

1.2 Numbers Representation in Computer


◼ Representation of integer numbers:

◼ To represent base 10 numbers in binary form the signed


magnitude method is used. The first digit stores the sign (0,
positive and 1, negative). The remaining bits are used to store the
number.

◼ A computer working with words of 16 bits can store integer


numbers in the range -32768 to 32767.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 21


Number System and Numerical Error Analysis

◼ Floating point representation:


◼ This representation is used for fractional quantities.

◼ It has the fraction part, called mantissa, and an integer part,


called exponent or characteristic.

m * be

1
m1
b

◼ The mantissa is usually normalized, so that the value of m is


limited (b=2 in binary)
◼ we can say that 156.78 could be represented as 0.15678 X 103
as a floating point number in base 10 system.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 22


Number System and Numerical Error Analysis

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 23


Number System and Numerical Error Analysis

◼ Unfortunately, computers introduce errors in the


calculations. However, since many engineering problems
have not analytical solution, we are forced to use
numerical methods (approximations).

◼ The only option we have is to accept the error and try to


reduce it up to a tolerable level.

◼ The only way of minimizing the errors is by knowing


and understanding why they occur and how we can
diminish them.
ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 24
Number System and Numerical Error Analysis

1.3 Errors

Relative Error

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 25


Number System and Numerical Error Analysis
Example:
Let x = 3.141592653589793 is the value of the constant ratio 𝜋 correct to 15 decimal
places and x = 3.14159265 be an approximation of x* . Compute the following quantities:
a. The error.
b. The absolute error.
c. The relative error.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 26


Number System and Numerical Error Analysis

◼ Error definitions:
◼ True value = approximation + absolute error.

◼ Absolute error = true value - approximation .

◼ Relative error = absolute error / true value .

absolute error
t = 100%
true value

◼ In real cases not always one can know the true value, thus:
aproximate error
a = 100%
approximate value

◼ In many occasions, the error is calculated as the difference between


the previous and the actual approximations.

actual approximation − previous approximation


a = 100%
actual approximation

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 27


Number System and Numerical Error Analysis
◼ Thus, the stopping criterium of a numerical method can be:

a  s
 s = prefixed percent tolerance
◼ It is conveniente to relate the Errors with the number of significan
figures. If the following relation holds, one can be sure that at least n
significant figures are correct.

s = (0.5 * 102−n )%

Example: Error Estimates for Iterative Methods


▪ In mathematics, functions can often be represented by infinite series. For
example, the exponential function (Maclaurin series expansion)can be
computed using

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 28


Number System and Numerical Error Analysis

Solution:
• First, the prespecified tolerance eqn. can be employed to determine the
error criterion that ensures a result that is correct to at least three
significant figures:

• Note that the true value is e 0.5 = 1.648721 . . . . Add terms until the
absolute value of the approximate error estimate εa falls below a
prespecified error criterion εs conforming to three significant figures.

• The first estimate is simply with a single term. Thus, the first estimate is
equal to 1.
• The second estimate is then generated by adding the second term as in

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 29


Number System and Numerical Error Analysis

a  s
• we would continue the computation by adding another term, x2/2!, and
repeating the error calculations. The process is continued until |εa| < εs .
The entire computation can be summarized as

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 30


Number System and Numerical Error Analysis
Computer Algorithm for Iterative Calculations
For the above example, the series expansion can be expressed as

• The function is passed the value to be evaluated (x) along with a stopping
error criterion (es) and a maximum allowable number of iterations (maxit).
• If the user omits either of the latter two parameters, the function assigns
default values.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 31


Number System and Numerical Error Analysis

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 32


Number System and Numerical Error Analysis

◼ From Error Analysis


◼ Identify the possible sources of error
◼ Estimate the magnitude of the error
◼ Determine how to minimize and control error

◼ Sources of Error in Numerical Computations


I. Blunders (Gross Errors)

II. Modelling Errors

III. Data Uncertainty

IV. Discretization Errors

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 33


Number System and Numerical Error Analysis
1.3.2 Error Types
◼ Round-off Error : Computers represent numbers in finite number of
digits and hence some quantities cannot be represented exactly. The error
caused by replacing a number a by its closest machine number is called the
roundoff error and the process is called correct rounding.
Example: Display π in short, long and long scientific format with MATLAB.
>> format short ; pi
>> ans = 3.1416 (short data has 5 digits)
>> format long ; pi
>> ans = 3.141592653589793 (long data has 16 digits)
>> format long e ; pi
>> ans = 3.141592653589793e+000 (long e data has 16 digits)
◼ Error resulted from omission of the remaining significant figures is called round-off
error.
◼ Precision: All computations in MATLAB are done in double precision by default.
True value π = 3.141592653589793238462643 . . .
MATLAB pi = 3.141592653589793e+000
◼ The digits in the red color have been rounded-off under MATALB environment.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 34


Number System and Numerical Error Analysis
◼ Truncation Error: result from using numerical method (approximation)
in place of an exact mathematical procedure to find the solution.
◼ sometimes called chopping errors are occurred when chopping an infinite
number and replaced it by a finite number or by truncated a series after finite
number of terms.
Example: The velocity with respect to time of bungee jumper is given in the 1st-
order differential equation as below. Compute velocity of a free fall bungee
jumper with a mass of 70 kg. Use a drag coefficient of 0.25 kg/m.

where v = vertical velocity (m/s), t = time (s), g


= gravity acceleration ( 9.81 m/s2) , cd = drag
coefficient (kg/m), m = jumper’s mass (kg).

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 35


Number System and Numerical Error Analysis
Solution:
◼ A Mathematical expression of a given model can be
F = ma; F(kgm / s2 ) m(kg ) a(m / s2 )
F dv F dv FD + FU
a= ; = ; = ;
m dt m dt m

dv mg − cv 2
= ; c( Kg / s )
dt m

◼ Analytic solution (t=0, v=0):

◼ Approximate solution (“Euler method (forward)”):

 c 
v(ti+1 ) = v(ti ) +  g − v(ti )2(ti+1 − ti )
 m 

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 36


Number System and Numerical Error Analysis
Cont..
◼ To solve the problem numerically, one replaces the derivative by a
finite difference, thus transforming the problem into a very simple
one containing only simple algebraic operations:

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 37


Number System and Numerical Error Analysis
◼ Comparing solutions:
Numerical
solution, Dt =2sec
Approximate Approximat
t(sec) Exact 60
(Dt=2s.) (Dt=1s.)
0 0 0 0
50
2 16,422 19,62 17,819339
4 27,798 32,037357 29,697439

V (m/sec)
40
6 35,678 39,896213 37,615198 Exact
8 41,137 44,870026 42,893056 30
solution
10 44,919 48,017917 46,411195
Numerical
12 47,539 50,010194 48,756333 20

14 49,353 51,271092 50,319566


solution, Dt =1sec
10
16 50,611 52,069105 51,361594
18 51,481 52,574162 52,056193
0
20 52,085 52,893809 52,519203 0 2 4 6 8 10 12 14 16 18 20

T (sec)

What do you think from this result?

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 38


Number System and Numerical Error Analysis
1.4 Computational Problems and Algorithms
◼ A computational problem is a problem that can be solved step-by-
step with a computer.
◼ These problems usually have a well-defined input, constraints, and
conditions that the output must satisfied.
Algorithm : set of instruction /condition to get the desired output.

Example: Finding the maximum


Suppose the input consists of: 4 1 -4 0 9 9 3 5 8
The output should be 9.
One way to solve this problem is to check through the integers in the
list, one-by-one, and keep track of the maximum value so far. When
you reach the end of the list, your "maximum value so far" will also be
the maximum for the whole list.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 39


Number System and Numerical Error Analysis
Cont.….
Integers Scanned Maximum So Far

4 4 • When i=0, m=l0. Since we only scan


a single integer, it has to be the
41 4 maximum.
4 1 -4 4
• When we check another
integer li(i>0), only two things can
4 1 -4 0 4 happen:
• if this li is larger than m, then li has
4 1 -4 0 9 9
to be the maximum so far, so we
4 1 -4 0 9 9 9 update m to be li .
• if li is equal to or smaller than m,
4 1 -4 0 9 9 3 9
then m is still the maximum value so
4 1 -4 0 9 9 3 5 9
far.

4 1 -4 0 9 9 3 5 8 9

• Now, we have enough details to describe step-by-step, how to find the


maximum value from a list of integers. Such steps, which the computer can take
to solve a problem, is called an algorithm.

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 40


Number System and Numerical Error Analysis
Example: Let us take the number 11.1875 and convert it in to binary.
Algorithm: Integer part Start

Integer N to be
converted to base two Input(N)10

i=0

Divide N by 2 to get quotient Q &


remainder R

i=i+1 ai=R

No

Is Q=o ?

Yes

n=i
(N)10 = (an…a0)2

Stop

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 41


Number System and Numerical Error Analysis
Fraction part Start

Fraction F to be converted
to binary format Input(F)10

i=-1

Multiply F by 2 to get number


Before decimal, S
And after decimal, T

i=i-1
ai=S

No

Is T=o ?

Yes

n=i
(F)10=(a-1…an)2

Stop

ASTU – EPCE Dpt Tamiru G. G. (MSc) tamiru.getahun@astu.edu.et 42


Thank You For Your Attention!

Questions?

You might also like