Introduction to Numerical Computing and MATLAB
Introduction to Numerical Computing and MATLAB
University of Saskatchewan
January, 2013
1.1 Motivation for the Course
1
What is Matlab?
2
Why Matlab?
3
Poor computing: cost and opportunity
4
Some infamous software bugs
5
Disasters caused by poor numerical
computing
6
• Patriot missile failure, February 25, 1991
A Scud missile killed 28 soldiers in Dhahran, Saudi
Arabia, when the Patriot missile meant to intercept
it miscalculated the Scud’s position. The error
came from an integer to floating-point conversion
of 9.5 × 10−8 per tenth of a second multiplied by
the product of the up-time of the Patriot (100
hours) and the velocity of the Scud (1676 metres
per second); i.e., 573 metres.
7
• Toyota hybrid vehicle recall, February 8, 2010
Toyota recalled more than 400,000 hybrid vehicles
in 2010 because the software that controlled the
anti-lock brakes was not responsive enough in
regenerating braking after hitting a bump. This
software glitch cost Toyota an estimated $3B.
8
A Matlab Tutorial
See intro.m
9
1.2 Floating-Point Arithmetic
10
So we must work with a finite number of digits
for a finite number of numbers!
11
Before then, every computer could (and often did!)
have its own brand of floating-point arithmetic!
12
We use numbers without thinking
13
We use base 10 for our number system.
1.23456 × 102.
14
Thus any number is expressible in the form
±d1.d2d3 . . . × 10e,
1 3 3 3
= + 2 + 3 + ....
3 10 10 10
15
For example, if we can store 3 digits, then 2.17 is
exactly representable, but 2.1415 must be represented
approximately, e.g., by 2.14.
but
16
On errors . . .
Eabs = |x − x̂|,
|x − x̂|
Erel = .
|x|
Notes:
17
Floating-point numbers
18
The concepts behind floating-point arithmetic on a
computer follow these principles, except things are
done in base 2.
The rest of our study of floating-point arithmetic will
focus on the various nuances of dealing with real
computer hardware.
In case you were wondering, base 2 has been adopted as
the base for computer arithmetic primarily for economic
reasons (although it turns out that small bases are a
good idea for other reasons).
It is cheapest to manufacture transistors and detect
whether they have a voltage on them (1) or not (0).
This leads to 2 (binary) voltage states that can be
reliably created and measured, and hence base 2.
In contrast, ternary (or base 3) arithmetic could rely
on whether a transistor had a certain voltage (+1), no
voltage (0), or a reverse voltage (–1).
Unfortunately, such transistors are not as cheap to
manufacture, nor is the production and detection of
the voltage states as reliable.
19
Floating-Point Numbers
x = ±(1 + f ) · 2e.
0 ≤ f < 1.
20
A Toy Floating-Point Number System
−2, −1, 0, 1.
21
Some things to notice:
22
Double Precision
−1022 ≤ e ≤ 1023.
23
Note 2. The entire fractional part of a floating-point
number is not f , but 1+f , which has 53 bits. However
the leading 1 doesn’t need to be stored. In effect,
the IEEE double-precision format packs 65 bits of
information into a 64-bit word.
24
Ubiquitous, innocuous round-off error
1 0 0 0 1 1
= + + + +
10 21 22 23 24 25
0 0 1 1
+ 6+ 7+ 8+ 9
2 2 2 2
0 0 1 1
+ 10 + 11 + 12 + 13 + . . .
2 2 2 2
25
A simple example of round-off error
format long
a = 4/3
b = a - 1
c = 3*b
e = 1 - c
26
Machine Epsilon
27
machine is not “floating-point zero”
In summary,
28
• Recall that if any computation is asked to produce
a value larger than realmax, it is said to overflow.
The result is an exceptional floating-point value
called infinity or Inf in Matlab.
Inf is represented by taking e = 1024 and f = 0.
Inf can make sense in calculations;
e.g., 1/Inf = 0 and Inf+Inf = Inf.
29
• If any computation is asked to produce a value
smaller than realmin, it is said to underflow.
This involves subnormal (or denormal) floating-
point numbers in the interval between realmin
and machine∗realmin.
The smallest positive subnormal number is
2−52−1022 ≈ 4.94 × 10−324.
Any results smaller than this are set to 0.
On machines without subnormal numbers, any
results less than realmin are set to 0.
The subnormal numbers live between 0 and
the smallest positive normalized number (recall
floatgui.m).
They provide an elegant way to handle underflow,
but their importance for computation is rare.
Subnormal numbers are represented by the special
value e = −1023, so the biased exponent (e + 1023)
is 0.
30
• Matlab uses the floating-point system to handle
integers.
Mathematically, the numbers 3 and 3.0 are
equivalent, but many programming languages would
use different representations for the two.
Matlab does not make such a distinction!
We sometimes use the term flint to describe a
floating-point number whose value is an integer.
Floating-point operations on flints do not introduce
any round-off error, as long as the results are not
too large.
Specifically:
– Individual addition, subtraction, and multiplication
of flints produce the exact flint result, if the result
is not larger than 253.
Beware of intermediate steps exceeding this limit!
31
Computing with Polynomials and
Horner’s Method
or
y=-1+x.*(7+x.*(-21+x.*(35+x.*(-35+x.*(21+x.*(-7+x))))));
See hornerDemo.m
32
Horner’s Method
33
Easy as 1, 2, 3
34
Easy as 1, 2, 3
s = 0;
for i=1:n,
s = s + x(i);
end
35
Compensated Summation
3.1416+0.023072 = (3.1+0.0416)+(0.02307+0.000002).
Then
ŝ = a1 + (a2 + b1).
Now
ŝ − a = b1 + 0,
and thus
36
Compensated Summation
for i=1:n,
temp = s; % temporary sum
y = x(i) + e; % read in the next element and compensate
s = temp + y; % form sum from temporary element and
% compensated element
e = (temp - s) + y; % compute estimated error and carry forward
end
See cSumDemo.m
37
Compensated Summation
38