Advanced Computational Methods: ENGR 680
Advanced Computational Methods: ENGR 680
Third Edition
Tradition & Excellence Since 1821 Mechanical Engineering
TOPIC
ENGR 680
02 Advanced Computational
Methods
Associative ( x y) z x ( y z)
( x y) z x ( y z)
Commutative x y yx
x y y x
Identity x0 x
elements
1 x x
Inverse x ( x) 0
Element
if x 0, x (1 / x) 1
02 - 2
Numbers in Computers: Binary Numbers
Binary • In computers, numbers are stored in the binary number system.
Number Only digits 0 & 1 are stored in memory to represent all possible
numbers.
Example 1= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
2= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
3= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
13= 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
02 - 3
Covert from Binary to Decimal: Integers
Integers • Integers: For a binary number dm-1 dm-2 … d2 d1d0, the decimal
value, N, is
m 1
N di 2i d 0 d1 2 d 2 2 2 d m 1 2 m 1
i 0
dm-1 dm-2 … d2 d1 d0
N=
+ + +
2m-1 2m-2 … 22 21 20
Since dm-1 , dm-2 , …, d2, d1 and d0 are either 1 or 0, N is the
sum of those 2i with a non-zero di , i = 1, 2,…, m.
1 1 … 1 0 1
N= 2m-1+ 2m-2 …
+ 22 + 20
Example • (1101)2 = 1 + 0(21) + 1(22) + 1(23) = 13
0 0 0 0 1 1 0 1
02 - 4
23 + 22 + 20 = 8 + 4 + 1 = 13
Covert from Binary to Decimal: Fractions
Integers • Fractions: For a binary number .d-1 d-2 … d-(m+1) d-m, the decimal
value, N, is
m
N d i 2 i d 1 2 1 d 2 2 2 d m 2 m
i 1
1 1 … 1 0 1
N= 2-1 + 2-2 … + 2-(m+2)+ 20
Example • (.1101)2 = 1(2-1) + 1(2-2) + 0(2-3)+ 1(2-4) = 0.8125
1 1 0 1 0 0 0 0
02 - 5
2-1 + 2-2 + 2-4 =0.5+0.25+0.0625= 0.8125
Fixed Point Notation
Fixed Point • The binary point is not stored in the computer, but is remembered
Notation by the programmer (program), e.g., for a 8-bit number, the
program can assume the binary point is after the 5th bit.
Example The binary point is not stored
1 0 0 1 1 1 0 1
23 20 2-1 2-2 2-4 = 9.8125
Number • The biggest positive number is stored in the computer, when
Range every bit is stored with a digit 1.
1 1 1 1 1 1 1 1
27 26 25 24 23 22 21 2 0 = 255 ~28
For a m-bit computer, the range of positive fixed-point
integers is about 2m
02 - 6
Fixed Point Notation: Limitations
Limit • If N bits are used, then only 2N things can be represented. 8 bits can
Precision represent 256 positive integers, 256 fractional numbers, and so on.
• For the 8-bit computer, with the binary point after the 4th bit, here is
a number line showing the 256 values that can be represented with
this fixed notation:
Number • Very large numbers and very small fractions cannot be represented
Range with this limited bits (2m for a m-digit computer).
02 - 7
Floating Point Notation: Decimal
Decimal • In the decimal system we can get round the range problem using
Floating scientific notation or float point notation.
Notation
Scientific Floating Point
Notation Notation
1,245,000,000,000 = 1.2451012 = 0.12451013
0.00001245 = 1.24510-5 = 0.124510-4
-0.00001245 = -1.24510-5 = -0.124510-4
02 - 8
Floating Point Notation: Binary Numbers
Binary • If we change the base into 2 in the previous slide, a decimal
Number number can always be written as:
N.n = M 2 E
Decimal Value Sign bit Mantissa Base Exponent
Only the Sign bit, Mantissa, the exponent and its sign need to
be stored in the computer.
Example • 5.75 = 5.75 20 = 101.11 20 = 101.11 20
= 2.875 21 = 10.111 21 = 10.111 21
= 1.4375 22 = 1.0111 22 = 1.0111 210 Binary
=0.71875 23 = .10111 23 = .10111 211 numbers
In a Suppose we use 16-bits words, with 5 bits for the exponent:
Computer + . 10111 2 + 11
0 1 0 1 1 1 0 0 0 0 0 0 0 0 1 1
Sign bit Mantissa, M Sign of E Exponent, E
The above scheme is for illustration only, the real storage
02 - 9 scheme in a computer is different (see IEEE 754).
Floating Point Notation: MATLAB©
Donald E. Floating-point computation is by nature inexact, and it is not
Knuth: difficult to misuse it so that the computed answers consist
almost entirely of 'noise'.
Range • Only a finite number of bits (32 for most PCs) are available for
a computer. The range of decimal numbers stored is also finite.
MATLAB© uses a double precision format (64 bits) to store
floating point numbers (with 16 significant decimal digits) and
32 bits to store integers, The ranges floating numbers is
Floating numbers: ~ 1.7*10–308 to 1.7*10308 (or ~ 2–1024 to
21024) .
Precision • As with the binary fixed point notation, use a finite number of
bits in the binary floating point notation can also represent only
a finite number of decimal numbers in a computer.
• Therefore, a binary floating point representation in a computer
is always an approximation for most numbers.
• Floating point relative accuracy in MATLAB© is
>> eps=2.2204e-016 (machine dependent = the distance
from 1.0 to the next largest floating point number which is
smaller than 1.0).
02 - 10
Number in Computers: Errors
See Real_num_command.m
02 - 12
Complex Numbers: Basic Concepts
Complex z x iy, i 1 Im
Num x = Re z = real part, z
y = Im z = imaginary part y
r
Magnitude r z x 2 y 2 (Length or Modulus)
Argument, x (Multi-valued: +2k)
arctan
y (Principal: [– , ])
Polar angle x Re
Complex z x iy
Conjugate ( z ) ( z ), r ( z ) r ( z ) z
Basic z1 x1 iy1 , z2 x2 iy2
Operations
z1 z2 ( x1 x2 ) i ( y1 y2 )
2
z1z2 ( x1x2 y1 y2 ) i ( x1 y2 y1x2 ) z zz
z1 z1 z2 z1z2 x1x2 y1 y2 y x x y
i 1 2 1 2
z2 z2 z2 z2 2 x12 y12 x12 y12
i
x Re
z x iy r (cos i sin ) re
Command Format
z=x+iy, z=x+jy Complex number
z=r*exp(i*theta) Polar form
abs(z) Magnitude z x2 y 2
angle(z) Angle in radians (-, ); = tan–1(y/x)
conj(z) Complex conjugate x - iy
imag(z) Complex imaginary part y
real(z) Complex real part x
Plotting
compass(x,y) Draw complex numbers as arrows on polar plot
feather(x,y) Draw complex numbers as arrows on linear plot
See Complex_num.m
02 - 15
Vectors in High Dimensions
Definition • An n dimensional vector x Rn (Rn is the set of of all vectors
with n real components) of real numbers is defined as
x = [x1, x2, …, xn] = x1e1 + x2e2 + … + xnen
e1 , e2 , … , en is a set of base (orthonormal) vectors
Addition x y x1 y1 x2 y2 xn yn
02 - 17
Vectors: Linear Dependence
Linear • Let x1, x2, …, xm be a set of n-vectors in Rn. Then a linear
Combination combination of these vectors is a sum of the form
k1x1 k2x2 kmxm
where k1, k2, …, km are nonzero scalars.
Linear • Let x1, x2, …, xm be a set of vectors in Rn. Then the set is said
Dependence to be linearly independent if and only if none of the vectors can
be expressed as a linear combination of the remaining vectors.
Theorem • Let x1, x2, …, xm be a set of vectors in Rn . The set is linearly
independent if the vector equation
k1x1 k2x2 kmxm 0
is only true when k1= k2 = … = km = 0
Linearly DEPENDENT: at least one non-zero scalar, ki,
exists such that
k1x1 k2x2 kmxm 0
Notes • The highest number of linear independent vectors in Rn is n.
• A mutually orthogonal set of vectors is linearly independent.
02 - 18
Vectors: MATLAB© Commands
Operation MatLab Command Math Form
Standard Addition (subs) xy [x1y1, x2y2, …, xnyn]
Linear Scalar Multpl k*x [kx1 , kx2 , … , kxn]
Algebra
Operation Transpose x’ Column Vector
Dot/inner Prod x*y’, dot(x,y) x1 y1 + x2 y2 + … + xn yn
Cross Prod (3D) cross(x,y) xy
Norm norm(x) x
Special Multiplication x.*y [x1 y1 , x2 y2 , …, xn yn]
element-by- Division x./y [x1/y1 , x2/y2 , …, xn/yn]
element
Exponentiation x.^m (m: scalar) [x1m, x2m, …, xnm]
operation in
MATLAB© Function f(x) [f (x1) , f (x2) , …, f (xn) ]
Augment [x y] [x1, x2, …, xn, y1, y2, …, yn]
02 - 19