Professional Documents
Culture Documents
Lecture Static Color 04 - 004 PDF
Lecture Static Color 04 - 004 PDF
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Outline
Calculus Review
Limits, Continuity, and Convergence
Differentiability, Rolles, and the Mean Value Theorem
Extreme Value, Intermediate Value, and Taylors Theorem
Algorithms
Algorithms, Pseudo-Code
Fundamental Concepts
http://www-rohan.sdsu.edu/jmahaffy
Spring 2010
Joe Mahaffy, hmahaffy@math.sdsu.edui
(1/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(2/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Convergence
Differentiability
If the theory is sound, when our programs fail we look for bugs in
the code!
Rolles Theorem
Mean Value Theorem
Extreme Value Theorem
Intermediate Value Theorem
Taylors Theorem
Calculus Review
(3/63)
Calculus Review
(4/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Limit / Continuity
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Example: Continuity at x0
1
Definition (Limit)
A function f defined on a set X of real numbers X R has the limit L
at x0 , written
lim f (x) = L
0.8
xx0
0.6
if given any real number > 0 ( > 0), there exists a real number > 0
( > 0) such that |f (x) L| < , whenever x X and 0 < |x x0 | < .
0.4
0.2
0.2
0.4
0.8
0.6
Here we see how the limit x x0 (where x0 = 0.5) exists for the
function f (x) = x + 21 sin(2x).
xx0
Calculus Review
(5/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
(6/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
0.8
1.5
0.6
0.4
0.2
0.5
0
0
The function
f (x) =
0.2
0.4
0.6
0.8
1
2
1
2
x + sin(2x)
x + sin(2x) + 1
The function
1
f (x) =
0
x < 0.5
x > 0.5
Calculus Review
0.4
0.6
0.8
lim f (x) = 0 6= 1
x0.5
0.2
(7/63)
Calculus Review
(8/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Continuity / Convergence
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
1
2
3
4
N2
k>=N
Let x = {xn }
n=1 be an infinite sequence of real (or complex
numbers). The sequence x converges to x (has the limit x) if
> 0, N() Z+ : |xn x| < n > N(). The notation
N1
A sequence in z = {zk }
k=1 converges to z0 C (the black dot) if
for any (the radius of the circle), there is a value N (which
depends on ) so that the tail of the sequence zt = {zk }
k=N is
inside the circle.
lim xn = x
Calculus Review
(9/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Differentiability
Calculus Review
(10/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Illustration: Differentiability
Theorem
0.8
0.6
0.4
0.2
f (x) f (x0 )
f (x0 ) = lim
xx0
x x0
0.2
0.4
0.6
0.8
lim
xx0
exists.
Calculus Review
f (x) f (x0 )
x x0
Calculus Review
(12/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Wiki-Link
Wiki-Link
f(c)=[f(b)f(a)] / [ba]
f(c)=0
a
a
Calculus Review
(13/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
(14/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Wiki-Link
Wiki-Link
c2 = b
a < c2 < b
c1 = a
a < c1 < b
a
Calculus Review
(15/63)
Calculus Review
(16/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Taylors Theorem
Wiki-Link
Pn (x) =
k=0
f (k) (x
k!
0)
(xx0 ) ,
Rn (x) =
f (n+1) ((x))
(n + 1)!
(n+1)
(xx0 )
sin(x)
t5(x)
40
sin(x)
t9(x)
30
20
20
10
10
10
-10
-10
-10
-20
-20
-20
-30
-30
-40
-40
Calculus Review
(17/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
sin(x)
t13(x)
30
20
-30
P5 (x)
-40
P9 (x)
P13 (x)
1
1
1
1 13
1
1 11
P13 (x) = x x 3 + x 5 x 7 + x 9
x +
x
9!
11!
13!
| 3! {z 5! } 7!
|
P5 (x)
{z
P9 (x)
Calculus Review
(18/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
f (x) = sin(x)
40
30
Suppose f C n [a, b], f (n+1) on [a, b], and x0 [a, b]. Then
x (a, b), (x) (x0 , x) with f (x) = Pn (x) + Rn (x) where
n
X
Calculus Review
(19/63)
Try this!!!
Calculus Review
(20/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Most versions of MatLab have a symbolic package that includes Maple, so this symbolic package can help with derivatives.
Often easier to play to the strengths of each language and let
Maple find the Taylor coefficients to employ in the MatLab
code.
MatLab provides relatively efficient numerical programs that
are similar and based on C Programming.
A MatLab code is provided to show the convergence of the
Taylor series to the cosine function with increasing numbers
of terms. This is shown on the Maple worksheet also, and
the code is accessible through my webpage.
Joe Mahaffy, hmahaffy@math.sdsu.edui
Calculus Review
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(21/63)
yaxis
8
1
0.5
0.5
1.5
xaxis
2.5
3.5
Calculus Review
(22/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Finite Precision
A single char
26
1
25
0
24
0
23
1
22
1
21
0
20
1
(23/63)
(24/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Finite Precision
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Description
The sign bit 0=positive, 1=negative
The characteristic (exponent)
The mantissa
c=
10
X
ck 2k ,
k=0
S (1 bit)
u
u
1
0
1
0
C (11 bits)
2047 (max)
2047 (max)
2047 (max)
2047 (max)
0
0
k=0
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(25/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(26/63)
c=
10
X
ck 2k ,
m=
k=0
51
X
mk
52k
2
r = (1)s 2c1023 (1 + f ),
k=0
c=
10
X
ck 2k ,
m=
k=0
3
1
0
210 1023
= 1 21 = 3.0
r1 = (1) 2
1+
2
2
Example #2: The Smallest Positive Real Number
0 00000000000 000000000000000000000000000000000000000000000000001
M (52 bits)
.0uuuuuu (with at least one 1 bit)
.1uuuuuu
.0000000
.0000000
.0000000
.0000000
From: http://www.freesoft.org/CIE/RFC/1832/32.htm
51
X
mk
m=
52k
2
r = (1)s 2c1023 (1 + f ),
Where
Bits
1
11
52
(27/63)
51
X
mk
52k
2
k=0
1
1
1
1
0
1023
r3 = (1) 2
1 + + 2 + + 51 + 52
2 2
2
2
1
= 21023 2 52 10308
2
(28/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
1 of 3
Consider r = 3.0
1023
0 10000000000 100000000000000000000000000000000000000000000000000
0 00000000000 000000000000000000000000000000000000000000000000010
1023
0 10000000000 100000000000000000000000000000000000000000000000001
The difference is
(29/63)
3 of 3
21023
= 2971 1.99 10292 .
252
Thats a fairly significant gap!!!
The number of atoms in the observable universe can be
estimated to be no more than 1080 .
Computer Arithmetic & Finite Precision
2
4.4 1016 .
52
2
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(30/63)
1
3.0, 3.0 + 51
2
is
2 of 3
0 00000000000 000000000000000000000000000000000000000000000000001
(32/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Theorem
Floating point numbers represent intervals!
0.d1 d2 d 10n
Examples
(2)
0 di 9, i 2,
nZ
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(34/63)
Important!!!
1) Representation Roundoff.
|p p |
2) Cancellation Consider:
|p p |
,
|p|
p 6= 0
0.12345678012345 101
0.12345678012344 101
0.10000000000000 1013
(35/63)
(36/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
with x0 = 1
1
e
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(37/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
xn+1 = 1 (n + 1)xn
with
n
0
1
2
3
4
5
6
7
8
9
10
xn
0.63212056
0.36787944
0.26424112
0.20727665
0.17089341
0.14553294
0.12680236
0.11238350
0.10093197
0.09161229
0.08387707
n!
1
1
2
6
24
120
720
5.04e+003
4.03e+004
3.63e+005
3.63e+006
n
11
12
13
14
15
16
17
18
19
20
xn
0.07735223
0.07177325
0.06694778
0.06273108
0.05903379
0.05545930
0.05719187
0.02945367
1.55961974
30.19239489
(38/63)
n!
3.99e+007
4.79e+008
6.23e+009
8.72e+010
1.31e+012
2.09e+013
3.56e+014
6.4e+015
1.22e+017
2.43e+018
x0 = 1
From the recursive relation
1
1
1
1
=1
+
+ ...
e
2!
3!
4!
1
1
1
+
...
2!
3!
4!
1
2
2
=
+
...
3
4!
5!
3!
3!
3!
+
...
=
4!
5!
6!
x1
1 x0 =
x2
1 2x1
x3
1 3x2
.
.
.
xn
1 nxn1 =
n!
n!
n!
+
...
(n + 1)!
(n + 2)!
(n + 3)!
(39/63)
1
1
+ ... 0
n+1
(n + 1)(n + 2)
as
n .
(40/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Matlab code
Algorithms, Pseudo-Code
Fundamental Concepts
Algorithms
Definition (Algorithm)
clear
x(1) = 1-1/exp(1);
s(1) = 1;
f(1) = 1;
for i = 2:21
x(i) = 1-(i-1)*x(i-1);
s(i) = 1/i;
f(i) = (i-1)*f(i-1);
end
n = 0:20;
z = [n; x; s; f];
fprintf(1, \n\n n x(n) 1/(n+1) n!\n\n)
fprintf(1, %2.0f %13.8f %10.8f %10.3g\n,z)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
Pseudo-code
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
(42/63)
Stability
Definition (Pseudo-code)
Pseudo-code is an algorithm description which specifies the
input/output formats.
Definition (Stability)
An algorithm is said to be stable if small changes in the input,
generates small changes in the output.
for i = 1,2,. . . ,n
Set xi = ai + i h
then ...
else
(43/63)
(44/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
Error Growth
pn =
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
Example BF-1.3.3
1 of 2
10
pn1 pn2 ,
3
Example BF-1.3.3
The recursive equation
Algorithms, Pseudo-Code
Fundamental Concepts
n = 2, 3, . . . ,
n
1
pn = c1
+ c2 3n
3
(45/63)
2 of 2
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
(46/63)
p1 = 0.33333
which modifies
c1 = 1.0000,
c2 = 0.12500 105
Exponential Growth
(47/63)
(48/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
Key Concepts
Rate of Convergence
{n }
n=1
{n }
n=1
1
n = +
n
n = + O(n )
{n }
n=1
Algorithms, Pseudo-Code
Fundamental Concepts
is usually chosen to be
hence
1
n = p
n
n = + O
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
(49/63)
Algorithms, Pseudo-Code
Fundamental Concepts
Suppose
It follows that
n = 0 + O
1
n3
since
lim G (h) = 0,
h0
If K > 0:
1
1
|n | = 3 + O
6n
n5
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(50/63)
Note:
1
1
1
+
O
=
O
,
O
n3
n5
n3
n
n
Hence
and
lim F (h) = L
h0
|F (h) L| K |G (h)|
as n
(51/63)
(52/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Algorithms, Pseudo-Code
Fundamental Concepts
3
h
5
|(h)| = + O h
6
Hence
It follows that
lim (h) = 0 + O h3
h0
Note:
O h3 + O h5 = O h3 ,
since
h5 h3 ,
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
as
h0
(53/63)
Introduction
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(54/63)
1 of 4
(55/63)
(56/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
2 of 4
3 of 4
n
1
|b0 a0 |
|bn an | =
2
ak1 + bk1
be the mid-point.
2
If f (mk ) = 0, were done, otherwise
(mk , bk1 )
if f (mk ) < 0
(ak , bk ) =
(ak1 , mk )
if f (mk ) > 0
Given (ak1 , bk1 ), let mk =
we can take
mn+1 =
n+1
1
dn =
|b0 a0 |
2
c = mn+1 dn ,
Joe Mahaffy, hmahaffy@math.sdsu.edui
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(57/63)
4 of 4
Convergence is slow:
At each step we gain one binary digit in accuracy. Since
101 23.3 , it takes on average 3.3 iterations to gain one
decimal digit of accuracy.
Note: The rate of convergence is completely independent of the
function f .
We are only using the sign of f at the endpoints of the interval(s)
to make decisions on how to update. By making more effective
use of the values of f we can attain significantly faster
convergence.
First an example...
Joe Mahaffy, hmahaffy@math.sdsu.edui
an + bn
2
(59/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(58/63)
Example, 1 of 2
x 2
2
sin(x) = 0
ak
1.5000
1.7500
1.8750
1.8750
1.9062
1.9219
1.9297
1.9336
1.9336
1.9336
bk
2.0000
2.0000
2.0000
1.9375
1.9375
1.9375
1.9375
1.9375
1.9355
1.9346
mk+1
1.7500
1.8750
1.9375
1.9062
1.9219
1.9297
1.9336
1.9355
1.9346
1.9341
f (mk+1 )
0.2184
0.0752
0.0050
0.0358
0.0156
0.0054
0.0002
0.0024
0.0011
0.0004
(60/63)
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
Example, 2 of 2
0.1
0.1
0.1
0.1
0.1
0.2
0.2
0.2
0.3
0.3
0.3
0.4
0.4
0.5
1.5
1.6
1.7
1.8
1.9
0.1
0.5
1.6
1.7
1.8
1.9
0.1
0.5
0.1
0.1
0.2
0.2
0.2
0.3
0.3
0.3
0.4
0.4
1.5
1.6
1.7
1.8
1.9
0.1
0.5
1.6
1.7
1.8
1.9
0.1
0.5
0.1
0.1
0.2
0.2
0.2
0.3
0.3
0.3
0.4
0.4
1.6
1.7
1.8
1.9
0.5
1.7
1.8
1.9
1.5
1.6
1.7
1.8
1.9
1.5
1.6
1.7
1.8
1.9
0.1
1.5
1.6
0.4
1.5
0.1
0.5
1.5
0.1
0.4
1.5
1.6
1.7
1.8
1.9
0.5
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(61/63)
Stopping Criteria
When do we stop?
We can (1) keep going until successive iterates are close:
|mk+1 mk | <
or (2) close in relative terms
|mk+1 mk |
<
|mk+1 |
or (3) the function value is small enough
|f (mk+1 )| <
No choice is perfect. In general, where no additional information about f
is known, the second criterion is the preferred one (since it comes the
closest to testing the relative error).
Joe Mahaffy, hmahaffy@math.sdsu.edui
Matlab code
0.4
1.5
0.1
0.1
0.5
Calculus Review
Computer Arithmetic & Finite Precision
Algorithms
Solutions of Equations of One Variable
(63/63)
(62/63)