ma/cs375 fall 2002 1 ma/cs 375 fall 2002 lecture 6
DESCRIPTION
MA/CS375 Fall Finite Precision Effects Recall that we are computing in a finite precision computing paradigm. i.e. all non integer numbers are represented by a finite approximation. e.g. 1/2 is represented exactly, but 1/3 is not.TRANSCRIPT
MA/CS375 Fall 2002 1
MA/CS 375
Fall 2002Lecture 6
MA/CS375 Fall 2002 2
Exercise: Blending Two Images• Find one partner to work with (i.e. your neighbor)• Write a script which reads in two images.• Use the min function to find the minimum number of rows for
both pictures (MINrows)• Use the min function to find the minimum number of columns
for both pictures (MINcolumns)• Crop the images using
> pic1 = pic1(1:MINrows,1:MINcols,1:3) > pic2 = pic2(1:MINrows,1:MINcols,1:3)
• Create a third picture (pic3) which is the average of pic1 and pic2
• Plot pic1,pic2,pic3 in the same figure using subplot
• Put your names on the title, print it out and hand it in.
MA/CS375 Fall 2002 3
Finite Precision Effects
• Recall that we are computing in a finite precision computing paradigm.
• i.e. all non integer numbers are represented by a finite approximation.
• e.g. 1/2 is represented exactly, but 1/3 is not.
MA/CS375 Fall 2002 4
Non-Special IEEE Floating Point Double Precision
• A double precision (Matlab default) number is represented by a 64 bit binary number:
• Here s is the sign bit • m is the mantissa (length to be determined)• e is the exponent and 0 < e < 2047
1023( 1) 1. 2s em
MA/CS375 Fall 2002 5
A Convergent Binary Representation of Any Number
Between 0 and 1
1
1 where m 1 or 02
n
n nnn
m m
a similar representation in base 10:
1
1 where 0,1,2,3,4,5,6,7,8 or 910
n
n nnn
d d d
Volunteer ?
MA/CS375 Fall 2002 6
Finite Binary Approximations of a Real Number
N1
1 +T where 1 or 02
n N
n nnn
m m m
We can easily show that TN is bounded as:(think of Zeno’s paradox)
12N NT
MA/CS375 Fall 2002 7
Definition of a Derivative• Suppose we are given a function:
– where f is smooth and well defined at every point on the real numbers.
– we can define the derivative of f at a point x as:
– later on today we will try to use this definition (and see what goes wrong )
Review
:f
0
limdf x f x f xdx
MA/CS375 Fall 2002 8
Monster Functions
Due to some of these finite precision effectsthere are some odd behaviors of Matlabdocumented in:
“Numerical Monsters”, Christopher Essex,Matt Davison and Christian Schulzky.
MA/CS375 Fall 2002 9
Team Tasks
We are going to investigate several examplesfrom:
“Numerical Monsters”, Christopher Essex,Matt Davison and Christian Schulzky.
A.K.A “When Good Formulas Go Bad in Matlab”
MA/CS375 Fall 2002 10
Teams
• Form teams of four
• Choose one of the following monster exercises
• Remember to put your names in the titles
MA/CS375 Fall 2002 11
Monster #1• Consider:
• What should its behavior be as:
• Use subplots for the following 2 plots
• Plot this function at 1000 points in:
• Plot this function at 1000 points in:
• Label everything nicely, include your name in the title.
• In a text box explain what is going on, print it out and hand it in
1 1xf x
x
0x
1,1x
4 ,4
MA/CS375 Fall 2002 12
Monster #2
• Consider:
• What should its behavior be as:
• Plot this function at 1000 points in:
• Explain what is going on in a text box, label everything, print it out and hand it in.
log(1 )x xf x e e
x
0,50x
MA/CS375 Fall 2002 13
• Consider:
• What should its behavior be as:
• Plot this function at 1000 points in:
• Explain what is going on in a text box and in particular what happens at x=54, label everything, print it out and hand it in.
22 log (1 2 )x xf x
x
0,60x
Monster #3
MA/CS375 Fall 2002 14
• Consider:
• What should its behavior be as:
• Plot four subplots of the function at 1000 points in: for
• Now fix x=0.5 and plot this as a function of for
• Explain what is going on, print out and hand in.
sin sincos
x xg x x
0
0,2*x 1 4,1 7,1 12,1 15e e e e
0.1,0.01,0.001,...,1 20e
Monster #4
MA/CS375 Fall 2002 15
Explanations of Team Examples
MA/CS375 Fall 2002 16
• Consider:
• What should its behavior be as:
• Plot this function at 1000 points in:
• Plot this function at 1000 points in:
• Explain what is going on.
1 1xf x
x
0x
1,1x
4 ,4
Recall Monster #1
MA/CS375 Fall 2002 17
Monster #1((large+small)-large)/small
MA/CS375 Fall 2002 18
when we zoom in we see that the large+small operation is introducing order eps errors which we then divide with eps to get O(1) errors !.
Monster #1((large+small)-large)/small
MA/CS375 Fall 2002 19
when we zoom in we see that the large+small operation is introducing order eps errors which we then divide with eps to get O(1) errors !.
Each stripe is a regionwhere 1+ x is a constant(think about the gaps between numbers in finite precision)
Then we divide by x and the stripes look like hyperbola.
The formula looks like (c-1)/x with a new c for each stripe.
Monster #1((large+small)-large)/small
MA/CS375 Fall 2002 20
Recall Monster #2
• Consider:
• What should its behavior be as:
• Plot this function at 1000 points in:
• Explain what is going on in a text box, label everything, print it out and hand it in.
log(1 )x xf x e e
x
0,50x
MA/CS375 Fall 2002 21
Limit of log(1 )x xf x e e
log 1lim lim log 1 lim
1lim by l'Hopital's rule
1lim 11
xx x
xx x x
x
x
xx
xx
ef x e e
e
eee
e
MA/CS375 Fall 2002 22
Monster #2(finite precision effects from large*small)
1 as f x x
As x increases past 30 we seethat f deviates from 1 !!
MA/CS375 Fall 2002 23
As x increases past ~=36 we see that f drops to 0 !!
Monster #2 cont(finite precision effects from large*small)
MA/CS375 Fall 2002 24
• Consider:
• What should its behavior be as:
• Plot this function at 1000 points in:
• Explain what is going on. What happens at x=54?
22 log (1 2 )x xf x
x
0,60x
Recall Monster #3
MA/CS375 Fall 2002 25
Monster 3(finite precision large*small with binary stripes)
MA/CS375 Fall 2002 26
As we require more than 52 bits to represent 1+2^(-x) we see that the log term drops to 0.
Monster 3(finite precision large*small with binary stripes)
MA/CS375 Fall 2002 27
• Consider:
• What should its behavior be as:
• Plot four subplots of the function at 1000 points in: for
• Now fix x=0.5 and plot this as a function of for
• Explain what is going on, print out and hand in.
sin sincos
x xg x x
0
0,2*x 1 4,1 7,1 12,1 15e e e e
0.1,0.01,0.001,...,1 20e
RecallMonster #4
MA/CS375 Fall 2002 28
Monster 4 cont
0 0
0
sin sin sin cos cos( )sin sinlim lim
sin cos 1 cos( )sinlim
cos
x x x x x
x x
x
Behavior as delta 0 :
or if you are feeling lazy use the definition of derivative, and remember: d(sin(x))/dx = cos(x)
MA/CS375 Fall 2002 29
Monster 4 cont(parameter differentiation, delta=1e-4)
OK
MA/CS375 Fall 2002 30
Monster 4 cont (parameter differentiation, delta=1e-7)
OK
MA/CS375 Fall 2002 31
Monster 4 cont (parameter differentiation, delta=1e-12)
Worse
MA/CS375 Fall 2002 32
Monster 4 cont (parameter differentiation, delta=1e-15)
When we make the delta around about machine precision we see O(1) errors !.
Bad
MA/CS375 Fall 2002 33
Monster 4 cont (numerical instablitiy of parameter differentiation)
As delta gets smaller we see that the approximation improves, until delta ~= 1e-8 when it gets worse and eventually the approximate derivate becomes zero.
MA/CS375 Fall 2002 34
Approximate Explanation of Monster #4
2
2
'
ˆˆ ˆ( )
ˆ ˆ ˆ ( )
( )
ˆ ˆˆ ˆ
'
f x f x f x O
y xx O
f y f y O
f x O
f y f x f y f x O
f x O O
1) Taylor’s thm:
2) Round off errors
3) Round off in computation of f and x+delta
4) Put this together:
MA/CS375 Fall 2002 35
2ˆ ˆˆ ˆ '
ˆ ˆˆ ˆ
f y f x f x O O
f y f xO O
i.e. for or equivalently approximation error decreases as delta decreasesize.
BUT for round off dominates!.
810
810
MA/CS375 Fall 2002 36
Summary• Ok – so in the far limits of the range of finite
precision really dodgy things happen.
• But recall, the formula for the derivative of sine worked pretty well for a large range of numbers.
• Try to avoid working with two widely separated numbers.
MA/CS375 Fall 2002 37
Summary of Lecture 6• Today’s lecture was all about the way Matlab
(and in fact almost all languages) store representations of real numbers
• We found out:– because the set of possible double precision
numbers only covers a discrete subset of points on the real line there are finite gaps between consecutive numbers
– If A is “large” and B is very “small” the sum A+B actually results in A+B ~= A
– and so on..