Transcript Lecture 1

Numerical Computation
Lecture 1: Taylor’s Theorem and
Floating-Point Arithmetic
United International College
Computer Representation
• To do numerical computations, we need to
solve two basic problems:
– Calculating functions on a computer
– Representing Real Numbers on a computer
Calculating Functions
• How do we calculate functions on a
computer?
• For Example, how do we calculate sin(3.14)?
– Problem: A computer can only do basic arithmetic
like addition, subtraction, multiplication, division.
– A function like sin(x) cannot be computed with a
finite set of these simple operations!
Calculating Functions
• Solution: Taylor’s Theorem

2 n 1
x
• This gives sin(x) =
(1) n
(2n  1)!
n 0

(a = 0)
Calculating Functions

2 n 1
x
• sin(x) =
(1) n
(2n  1)!
n 0

• This looks good! We are only adding, multiplying
(powers) and dividing. But, this is an infinite sum!!
Computers cannot do this.
• What if we only take a finite number of terms in the
sum? What is the error in this approximation of
sin(x)?
Calculating Functions
(from Numerical Analysis Course Notes, pg. 1)
• We will use this theorem repeatedly in this class!!
Calculating Functions
• Example: Find an approximation for f(x) = sin x
expanded about c = 0 using n = 3:
• Solution: Solving for f(n)(x) is fairly easy for this
function. We find that
• So,
Calculating Functions
• Example:
• This says that the error in approximating sin(x) by (xx3/6) is at most x4/24 for any x.
• We say that sin(x) = (x-x3/6) + Ο(x4).
• Definition: We say that a function f(h) is in the class
O(hk) if there is some constant C such that
|f(h)| ≤ C|h|k
for all h sufficently small.
Calculating Functions
• Practice: Find an O(h3) approximation to ln(1+h)
• Solution: Do together on the board.
• Practice: Suppose that f is in O(hm) and g is in O(hn)
for positive numbers m and n. Show that the product
function fg is in O(hm+n).
• Solution: Do together on the board.
Loss of Precision
• We now know how to approximate the value of a
function using the finite arithmetic available on a
computer. But, this arithmetic can itself have
problems.
• Example: Suppose that x = 1.2345678 x 10-5 .Then,
If the computer can only keep 8
significant digits, then if we subtract 1 from this, we
get 6.2 x 10-6 . This means that we have lost 6
significant digits of accuracy just by subtraction!
Loss of Precision
• Solution: Rationalize the denominator.
• Then, we get 6.17281995 x 10-6 and we have
preserved the accuracy of the original number.
Loss of Precision
• Problem: How can subtractive cancellation cause
problems when we calculate ex – cos(x) near 0?
• Class Work:
– How can we solve this problem using Taylor’s
series?
– How can we use Taylor’s series to find a stable O(x5
) approximation to ex – cos(x) near 0?
Break
Floating Point Numbers
• We have seen how a computer can calculate the
values of functions like sin(x), ln(1+x), cos(x), etc,
using finite Taylor’s series. We have also looked at
how simple arithmetic can lead to Loss of Precision.
• But we have not looked at how a computer actually
stores real numbers and does arithmetic on numbers.
• Modern computers use a representation of real
numbers called Floating Point Representation.
Floating Point Numbers
• In floating point representation a real number x is
represented as
x = ±(1 + f) 2e-E
where 0 ≤ f < 1 , E (Excess) is a fixed integer, and e
is an integer.
• The sign (parity) of the number is represented by one
binary digit: 0 – positive, 1 – negative.
• Numbers can be represented in single or double
precision.
IEEE Standard
(底數 尾數)
Floating Point Numbers
• Single Precision:
– f is represented by an integer < 223
( / 223 for term 1+f )
– -126 ≤ e-E ≤ 127 (an integer)
– E = 127
Floating Point Numbers
• Example: Show the single precision representation of
the normalized number + 26 x 1.01000111001 (in
binary form)
• Solution: The sign is positive. E=127. The
representation of the exponent requires e-127 = 6, so
e=133=128+4+1. The floating point number in
memory is stored as:
0 10000101 01000111001000000000000
Floating Point Numbers
• Note:
– Finite f implies finite precision.
– Finite e implies finite range.
– Floating point numbers have discrete spacing,
a maximum and a minimum.
Floating Point Numbers
• Double Precision:
– f is represented by an integer < 252
( / 252 for term 1+f )
– -1022 ≤ e-E ≤ 1023 (an integer)
– E = 1023
– Matlab uses primarily double-precision
arithmetic.
Floating Point Numbers
• In floating point arithmetic, two numbers cannot be
arbitrarily close to each other.
• For example, from the number 1, the next largest
number is 1 + 2-52. (Why?)
• Definition: The machine epsilon (eps) is the distance
from 1 to the next larger floating-point number.
– Double precision: eps = 2-52
Floating Point Numbers
• Class Practice: Verify the information in this
table:
Floating Point Numbers in Matlab
• Class Practice: Exercise 1.34 in Moler says:
– Explain the output produced by typing into Matlab:
t = 0.1
n = 1:10
e = n/10 - n*t
How is round-off contributing to the strange behavior shown
here?
Floating Point Numbers in Matlab
• Class Practice: Exercise 1.35 in Moler:
– What does this programs do? What are the last two values
of x printed? Why are these the last two lines??
x = 1; while 1+x > 1, x = x/2, pause(.02), end
Reading Assignment
• Try using Matlab in the CS lab. (F201)