0% found this document useful (0 votes)
68 views22 pages

2.29 Numerical Fluid Mechanics Fall 2011 - Lecture 2

The document discusses numerical fluid mechanics and covers topics such as approximation and round-off errors, number representations in computers, arithmetic operations and how they can result in errors, and recursion algorithms like Heron's method and Horner's scheme where the order of computations is important.

Uploaded by

Apel_Apel_King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views22 pages

2.29 Numerical Fluid Mechanics Fall 2011 - Lecture 2

The document discusses numerical fluid mechanics and covers topics such as approximation and round-off errors, number representations in computers, arithmetic operations and how they can result in errors, and recursion algorithms like Heron's method and Horner's scheme where the order of computations is important.

Uploaded by

Apel_Apel_King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

2.

29 Numerical Fluid Mechanics



Fall 2011 Lecture 2

REVIEW Lecture 1
1. Syllabus, Goals and Objectives
2. Introduction to CFD
3. From mathematical models to numerical simulations (1D Sphere in 1D flow)
Continuum Model Differential Equations

=> Difference Equations (often uses Taylor expansion and truncation)

=> Linear/Non-linear System of Equations

=> 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

Numerical Fluid Mechanics


1

PFJL Lecture 2,

2.29 Numerical Fluid Mechanics



REVIEW Lecture 1, Contd
Approximation and round-off errors
Significant digits: Numbers that can be used with confidence
xa x
xa
x xn1
n
s
xn

Absolute and relative errors Ea xa x , a


Iterative schemes and stop criterion: a

For n digits:

2.29

1
2

s 10n

Numerical Fluid Mechanics


2

PFJL Lecture 2,

Numerical Fluid Mechanics TODAYs Outline



Approximation and round-off errors
Absolute and relative errors
Number representations
Arithmetic operations
Errors of arithmetic/numerical operations
Recursion algorithms (Heron, Horners scheme, etc):

Order of computations matter

Truncation Errors, Taylor Series and Error Analysis


Taylor series:
Use of Taylor Series to derive finite difference schemes (first-order Euler
scheme and forward, backward and centered differences)
Error propagation numerical stability
Error estimation
Error cancellation
Reference: Chapra and Canale,
Chaps 3.1-3.4 and 4.1-4.4
Condition numbers

2.29

Numerical Fluid Mechanics


3

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

Integer Representation (signed magnitude method):

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)

Floating Number Representation


Sign
Signed
Exponent
2.29

x = m be
Mantissa

Mantissa/Significand
= fractional part

b
e

Base

Numerical Fluid Mechanics


4

Exponent
PFJL Lecture 2,

Floating Number Representation

Examples

Decimal

Binary

Convention: Normalization of Mantissa m (so as to have no zeros on the left)

0.01234 => 0.1234 10-1


12.34
=> 0.1234 102
Decimal
Binary

=> General

2.29

Numerical Fluid Mechanics


5

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.
(1 sign, 2 for exp.)
3 for mantissa
Largest and smallest
positive number
represented are ?
2.29

Numerical Fluid Mechanics


6

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

Smallest positive number is: 0.5 2-3

Sign

Sign
exp

21

20

2-1

2-2

2-3

2.29

Numerical Fluid Mechanics


7

PFJL Lecture 2,

Consequence of Floating Point Reals



Limited range of quantities can be represented
Min number (Underflow Error) and Max number (Overflow)

Finite Number of quantities can be represented within the


range (limited precision) => Quantizing errors
Quantizing errors treated either by round-off or chopping.

Interval x between numbers increases as numbers grow


in magnitude
For t = number of significant digits in mantissa and rounding,
E = b1-t = Machine Epsilon
Relative Error

Absolute Error

2.29

x E

x
2

E
x x
2

%Determine machine epsilon in matlab


%
eps=1
while (eps+1>1)
eps=eps/2;
end
eps*2

Numerical Fluid Mechanics


8

PFJL Lecture 2,

Arithmetic Operations

Shift mantissa of smallest number,

1. Addition and Subtraction

assuming
Result has exponent of largest number:
,
Absolute Error

Relative Error
Unbounded
for m1 m2 0

2. Multiplication and Division


Multiplication:

Add exp, multiply mantissa, normalize and chop/round

Division:

Subtract exp, divide mantissa, normalize and chop/round

Relative Error
Bounded

2.29

Numerical Fluid Mechanics


9

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

Numerical Fluid Mechanics


10

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

Numerical Fluid Mechanics


11

PFJL Lecture 2,

11

Matlab additions and quantizing effect



EXAMPLES

radd (100,4.9,1) = 100

radd (100,4.9,2) = 100

radd (100,4.9,3) = 105

>> radd (99.9,4.9,1)= 100

>> radd (99.9,4.9,2)= 100

>> radd (99.9,4.9,3) = 105

2.29

NOTE: Quantizing effect peculiarities


>> radd (0.095,-0.03,1) =0.06



>> radd (0.95,-0.3,1)= 1

Difference come from MATLAB round:
>> round(10^1*0.095/10^(-1))
9

>> round(10^1*0.95/10^(0))

10

Numerical Fluid Mechanics


12

PFJL Lecture 2,

12

Issues due to Digital Arithmetic

Large number of additions/subtractions (recursion), e.g.


add 1

100,000 times vs.


add 0.00001 100,000 times.

Adding large and small numbers


Subtractive cancellation
Round-off errors induced when subtracting nearly equal numbers, e.g.
roots of polynomials

Smearing: occurs when terms in sum are larger than the sum

e.g. series of mixed/alternating signs

Inner products: very common computation, but prone to


round-off errors
2.29

Numerical Fluid Mechanics


13

PFJL Lecture 2,

13

Recursion: Herons Device

Numerically evaluate square-root

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

plot([0 n],[sqrt(a) sqrt(a)],'b')

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

Mean of guess and its reciprocal

Recursion Algorithm

2.29

Numerical Fluid Mechanics


14

PFJL Lecture 2,

14

Recursion: Horners scheme to

evaluate polynomials by recursive additions



horner.m

Goal: Evaluate polynomial

% 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

For home suggestion: utilize radd.m for all


additions above and compare the error of
Horners scheme to that of a brute force
summation, for both z negative/positive
Numerical Fluid Mechanics
15

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

Result of small, but significant

term destroyed by subsequent

addition and subtraction of almost

equal, large numbers.

Remedy:

Change order of additions

'
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');

Numerical Fluid Mechanics


16

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

Numerical Fluid Mechanics


17

PFJL Lecture 2,

17

ps: Bessel functions are only used as


example, no need to know everything
about them for this class.

Error Propagation
Spherical Bessel Functions

Differential Equation

Solutions

2.29

Numerical Fluid Mechanics


18

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

Central Finite Difference

2.29

Numerical Fluid Mechanics


20

PFJL Lecture 2,

20

Error Analysis

Numerical Instability Example

Backward Recurrence

Evaluate Integral

Recurrence Relation:
Proof :

3-digit Recurrence:

> y2 !!
< 0 !!

Correct

Exercise: Make MATLAB script

2.29

Numerical Fluid Mechanics


21

PFJL Lecture 2,

21

MIT OpenCourseWare
http://ocw.mit.edu

2.29 Numerical Fluid Mechanics


Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like