2.29 Numerical Fluid Mechanics Fall 2011 - Lecture 2
2.29 Numerical Fluid Mechanics Fall 2011 - Lecture 2
=> Numerical Solution (matrix inversion, eigenvalue problem, root finding, etc)
4. Error Types
Round-off error: due to representation by computers of numbers with a finite
number of digits (significant digits)
Truncation error: due to approximation/truncation by numerical methods of exact
mathematical operations/quantities
Other errors: model errors, data/parameter input errors, human errors.
2.29
PFJL Lecture 2,
For n digits:
2.29
1
2
s 10n
PFJL Lecture 2,
2.29
PFJL Lecture 2,
Number Representations
Number Systems:
Base-10:
1,23410 = 1 x 103 + 2 x 102 + 3 x 101 + 4 x 100
Computers (0/1): base-2 11012 = 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
=1310
First bit is the sign (0,1), remaining bits used to store the number
For a 16-bits computer:
Example: -1310 = 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
Largest range of numbers: 215-1 largest number => -32,768 to 32,767
(from 0 to 1111111111111111)
x = m be
Mantissa
Mantissa/Significand
= fractional part
b
e
Base
Exponent
PFJL Lecture 2,
Examples
Decimal
Binary
=> General
2.29
PFJL Lecture 2,
Example
(Chapra and Canale, pg 61)
Consider hypothetical
Floating-Point machine in
base-2
7-bits word =
1 for sign
3 for signed exp.
(1 sign, 2 for exp.)
3 for mantissa
Largest and smallest
positive number
represented are ?
2.29
PFJL Lecture 2,
Example
(Chapra and Canale, pg 61)
Consider hypothetical
Floating-Point machine
in base-2
Scan of page removed due to copyright restrictions.
Please see p.61 in:
Chapra, S., and R. Canale. Numerical Methods for Engineers. 6th ed.
McGraw-Hill Higher Education, 2009, p. 61. ISBN: 9780073401065.
7-bits word =
1 for sign
3 for signed exp.
3 for mantissa
Largest number is: 7 = 2(2+1) (2-1+ 2-2+ 2-3)
Sign
nb
Sign
exp
21
20
2-1
2-2
2-3
Sign
Sign
exp
21
20
2-1
2-2
2-3
2.29
PFJL Lecture 2,
Absolute Error
2.29
x E
x
2
E
x x
2
PFJL Lecture 2,
Arithmetic Operations
assuming
Result has exponent of largest number:
,
Absolute Error
Relative Error
Unbounded
for m1 m2 0
Division:
Relative Error
Bounded
2.29
PFJL Lecture 2,
Digital Arithmetics
Finite Mantissa Length
function c = radd(a,b,n)
%
% function c = radd(a,b,n)
%
% Adds two real numbers a and b simulating an arithmetic unit with
% n significant digits, and rounding-off (not chopping-off) of numbers.
radd.m
Limited precision
addition in MATLAB
% If the inputs a and b provided do not have n digits, they are first
% rounded to n digits before being added.
%--- First determine signs
sa=sign(a);
sb=sign(b);
%--- Determine the largest number (exponent)
if (sa == 0)
la=-200; %this makes sure that if sa==0, even if b is very small, it will have the largest exponent
else
la=ceil(log10(sa*a*(1+10^(-(n+1))))); %This determines the exponent on the base. Ceiling is used
%since 0<log10(mantissa_base10)<=-1. The 10^etc. term just
%properly increases the exponent estimated by 1 in the case
%of a perfect log: i.e. log10(m b^e) is an integer,
%mantissa is 0.1, hence log10(m)=-1, and
%ceil(log10(m b^e(1+10^-(n+1))) ~< ceil(e +log10(m)+log10(1+10^-(n+1)))=e.
end
if (sb == 0)
lb=-200;
else
lb=ceil(log10(sb*b*(1+10^(-(n+1)))));
end
lm=max(la,lb);
2.29
PFJL Lecture 2,
10
radd.m, continued
%--- Shift the two numbers magnitude to obtain two integers with n digits
f=10^(n);
%this is used in conjunction with the round function below
at=sa*round(f*sa*a/10^lm); %sa*a/10^lm shifts the decimal point such that the number starts with 0.something
%the f*(*) then raises the number to a power 10^n, to get the desired accuracy
%of n digits above the decimal. After rounding to an integer, any figures that
%remain below are wiped out.
bt=sb*round(f*sb*b/10^lm);
% Check to see if another digit was added by the round. If yes, increase
% la (lb) and reset lm, at and bt.
ireset=0;
if ((at~=0) & (log10(at)>=n))
la=la+1; ireset=1;
end
if ((bt~=0) & (log10(bt)>=n))
lb=lb+1; ireset=1;
end
if (ireset)
lm=max(la,lb);
at=sa*round(f*sa*a/10^lm);
bt=sb*round(f*sb*b/10^lm);
end
ct=at+bt;
%adds the two numbers
sc=sign(ct);
%The following accounts for the case when another digit is added when
%summing two numbers... ie. if the number of digits desired is only 3,
%then 999 +3 = 1002, but to keep only 3 digits, the 2 needs to be wiped out.
if (sc ~= 0)
if (log10(sc*ct) >= n)
ct=round(ct/10)*10;
%
'ct'
end
end
%-----This basically reverses the operation on line 34,38
% (it brings back the final number to its true magnitude)
c=ct*10^lm/f;
2.29
PFJL Lecture 2,
11
2.29
PFJL Lecture 2,
12
Smearing: occurs when terms in sum are larger than the sum
e.g. series of mixed/alternating signs
PFJL Lecture 2,
13
Initial guess x0
Test
a=26;
%Number for which the sqrt is to be computed
n=10;
%Number of iteration in recursion
g=2;
%Initial guess
% Number of Digits
MATLAB script
dig=5;
sq(1)=g;
heron.m
for i=2:n
sq(i)= 0.5*radd(sq(i-1),a/sq(i-1),dig);
end
'
i
value
'
[ [1:n]' sq']
hold off
hold on
plot(sq,'r')
plot(a./sq,'r-.')
plot((sq-sqrt(a))/sqrt(a),'g')
legend('sqrt','xn','s/xn','Relative Err')
grid on
Recursion Algorithm
2.29
PFJL Lecture 2,
14
% Horners scheme
% for evaluating polynomials
a=[ 1 2 3 4 5 6 7 8 9 10 ];
n=length(a) -1 ;
z=1;
b=a(1);
% Note index shift for a
for i=1:n
b=a(i+1)+ z*b;
end
p=b
Horners Scheme
+
( b0=a0 )
>> horner
p =
General order n
55
Recurrence relation
2.29
PFJL Lecture 2,
15
Recursion: Order of
Operations Matter
Tends to:
If
N=20; sum=0; sumr=0;
b=1; c=1; x=0.5;
xn=1;
% Number of significant digits in computations
dig=2;
ndiv=10;
for i=1:N
recur.m
a1=sin(pi/2-pi/(ndiv*i));
a2=-cos(pi/(ndiv*(i+1)));
% Full matlab precision
xn=xn*x;
addr=xn+b*a1;
addr=addr+c*a2;
ar(i)=addr;
sumr=sumr+addr;
z(i)=sumr;
% additions with dig significant digits
add=radd(xn,b*a1,dig);
add=radd(add,c*a2,dig);
% add=radd(b*a1,c*a2,dig);
% add=radd(add,xn,dig);
a(i)=add;
sum=radd(sum,add,dig);
y(i)=sum;
end
sumr
2.29
Remedy:
'
i
delta
Sum
res=[[1:1:N]' ar' z' a' y']
delta(approx) Sum(approx)'
recur.m
hold off
a=plot(y,'b'); set(a,'LineWidth',2);
Contd.
hold on
a=plot(z,'r'); set(a,'LineWidth',2);
a=plot(abs(z-y)./z,'g'); set(a,'LineWidth',2);
legend([ num2str(dig) ' digits'],'Exact','Error');
PFJL Lecture 2,
16
recur.m
>> recur
b = 1; c = 1; x = 0.5;
dig=2
i
delta
Sum
delta(approx) Sum(approx)
..\codes_2\recur.png
res =
1.0000
2.0000
3.0000
4.0000
5.0000
6.0000
7.0000
8.0000
9.0000
10.0000
11.0000
12.0000
13.0000
14.0000
15.0000
16.0000
17.0000
18.0000
19.0000
20.0000
2.29
0.4634
0.2432
0.1226
0.0614
0.0306
0.0153
0.0076
0.0037
0.0018
0.0009
0.0004
0.0002
0.0001
0.0000
0.0000
-0.0000
-0.0000
-0.0000
-0.0000
-0.0000
0.4634
0.7065
0.8291
0.8905
0.9212
0.9364
0.9440
0.9478
0.9496
0.9505
0.9509
0.9511
0.9512
0.9512
0.9512
0.9512
0.9512
0.9512
0.9512
0.9512
0.5000
0.2000
0.1000
0.1000
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0.5000
0.7000
0.8000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
0.9000
PFJL Lecture 2,
17
Error Propagation
Spherical Bessel Functions
Differential Equation
Solutions
2.29
PFJL Lecture 2,
18
Error Propagation
Spherical Bessel Functions
Forward Recurrence
Forward Recurrence
Unstable
Backward Recurrence
Millers algorithm
Stable
with
2.29
N ~ x+20
Numerical Fluid Mechanics
19
PFJL Lecture 2,
19
Error Propagation
Eulers Method
Differential Equation
Example
Discretization
Finite Difference (forward)
Recurrence
euler.m
2.29
PFJL Lecture 2,
20
Error Analysis
Numerical Instability Example
Backward Recurrence
Evaluate Integral
Recurrence Relation:
Proof :
3-digit Recurrence:
> y2 !!
< 0 !!
Correct
2.29
PFJL Lecture 2,
21
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.