7. The Discrete vs The Continuous (Cont`d) The Setting One

7. The Discrete
vs
The Continuous (Cont’d)
The 1991 Patriot Missile Disaster
Elementary
misperceptions
about the
finiteness
of computer
arithmetic.
30+ died.
Screen Granularity
Finite Arithmetic
More practice with iteration and conditionals.
The Setting
External clock counts time in tenths
of seconds.
One-Tenth in Binary
Exact:
.00011001100110011001100110011…
Targeting software needs time to
compute trajectories. The method:
Time = (# external clock ticks) x (1/10)
The problem is here
Error
Time = (# external clock ticks) x (1/10)
Error = (# external clock ticks) x
(.000000095)
Patriot System used:
.00011001100110011001100110011…
Error = .000000095sec every clock tick
After 100 hours…
Error = (100x60x60*10)*.000000095
= .34 secs
Missed target by 500 meters.
1
Table Æ Plot
8. Plotting Continuous
Functions
sin(x)
1
0.8
0.6
x
0.00
1.57
3.14
4.71
6.28
Linspace
Array Operations
sin(x)
0.0
1.0
0.0
-1.0
0.0
0.4
0.2
0
−0.2
−0.4
−0.6
−0.8
−1
−1
0
1
2
3
4
5
6
7
Plot based on 5 points
Table Æ Plot
Table Æ Plot
sin(x)
x
0.000
0.784
1.571
2.357
3.142
3.927
4.712
5.498
6.283
sin(x)
0.000
0.707
1.000
0.707
0.000
-0.707
-1.000
-0.707
0.000
sin(x)
1
0.8
1
0.6
0.8
0.4
0.6
0.2
0.4
0
0.2
−0.2
0
−0.4
−0.2
−0.6
−0.4
−0.8
−0.6
−1
−0.8
−1
−1
−1
0
1
2
3
4
5
6
0
Generating Tables and Plots
sin(x)
0.000
0.707
1.000
0.707
0.000
-0.707
-1.000
-0.707
0.000
2
3
4
5
6
7
Plot based on 200 points—looks smooth
Plot based on 9 points
x
0.000
0.784
1.571
2.357
3.142
3.927
4.712
5.498
6.283
1
7
linspace
x = linspace(0,2*pi,9);
y = sin(x);
plot(x,y)
x = linspace(1,3,5)
x : 1.0
1.5
2.0
2.5
3.0
sin(x)
“x is a table of values”
1
0.8
0.6
0.4
0.2
“x is an array”
0
−0.2
−0.4
−0.6
“x is a vector”
−0.8
−1
−1
0
1
2
3
4
5
6
7
2
linspace
Linspace Syntax
x = linspace(0,1,101)
linspace(
x : 0.00 0.01 0.02
…
,
Right
Endpoint
Built-In Functions Accept Arrays
x
0.00
1.57
3.14
4.71
6.28
sin(x)
0.0
1.0
0.0
-1.0
0.0
)
0.99 1.00
Left
Endpoint
0.00 1.57 3.14 4.71 6.28
,
sin
Return Array of Function-evals
sin
0.00 1.00 0.00 -1.00 0.00
x
0.00
1.57
3.14
4.71
6.28
And…
sin(x)
0.0
1.0
0.0
-1.0
0.0
Can We Plot This?
Examples
x = linspace(0,1,200);
y = exp(x);
plot(x,y)
Number
of
Points
f ( x) =
sin(5 x) exp(− x / 2)
1+ x2
-2 <= x <= 3
x = linspace(1,10,200);
y = log(x);
plot(x,y)
3
Can We Plot This?
f ( x) =
sin(5 x) exp(− x / 2)
1+ x2
-2 <= x <= 3
Must Learn
How to Operate on Arrays
Yes!
x = linspace(-2,3,200);
y = sin(5*x).*exp(-x/2)./(1 + x.^2)
plot(x,y)
Look at four simpler plotting
challenges.
Array operations
Example 1
Scale (*)
3
2
a:
10
s:
2
c:
20
8
-5
16
-10
1
0
c = s*a
−1
−2
−3
0
2
4
6
8
10
12
14
f ( x) = 2 sin( x) − cos(3x) + .1* sin(20 x)
Addition
c = a + b
Subtraction
a:
10
8
-5
b:
2
4
1
c:
12
12
-4
c = a - b
a:
10
8
-5
b:
2
4
1
c:
8
4
-6
4
E.g.1 Sol’n
Example 2.
6
5
4
x = linspace(0,4*pi,200);
y1 = sin(x);
y2 = cos(3*x);
y3 = sin(20*x);
y = 2*y1 - y2 + .1*y3;
plot(x,y)
3
2
1
0
−1
−5
0
f ( x) =
Exponentiation
c = a.^s
.^
a:
10
s:
2
c:
100
8
-5
25
Reciprocation
a:
10
5
1+ x2
Shift
c = a + s
64
5
a:
10
s:
2
c:
12
8
-5
10
-3
E.g.2 Sol’n
8
-5
x = linspace(-5,5,200);
y = 5./(1+ x.^2);
plot(x,y)
c = 1./a
c:
.1
.125
-.2
5
Example 3.
1
Negation
0.8
0.6
0.4
a:
10
8
-5
c:
-10
-8
5
0.2
0
−0.2
c = -a
−0.4
−0.6
−0.8
0
0.5
1
1.5
2
2.5
3
f ( x) = exp(− x / 2) sin(10 x)
Scale (/)
c = a/s
a:
10
s:
2
c:
5
Multiplication
8
-5
c = a .* b
4
-2.5
.*
E.g.3 Sol’n
a:
10
8
-5
b:
2
4
1
c:
20
32
-5
Example 4.
40
30
20
10
x = linspace(0,3,200);
y = exp(-x/2).*sin(10*x);
plot(x,y)
0
−10
−20
−30
−40
−8
−6
−4
f ( x) =
−2
0
2
4
6
8
.2 x 3 − x
1.1 + cos( x)
6
Division
a:
c = a ./ b
./
10
E.g.4 Sol’n
8
-5
b:
2
4
1
c:
5
2
-5
x = linspace(-2*pi,2*pi,200);
y = (.2*x.^3 - x)./(1.1 + cos(x));
plot(x,y)
Question Time
Question Time
How many errors in the following
statement given that
x = linspace(0,1,100):
How many errors in the following
statement given that
x = linspace(0,1,100):
Y = (3*x .+ 1)/(1 + x^2)
Y = (3*x .+ 1)/(1 + x^2)
Y = (3*x + 1) ./ (1 + x.^2)
A. 0
B. 1
C. 2
D. 3
E. 4
Question Time
Does this assign to y the values
sin(0o), sin(1o),…,sin(90o)?
x = linspace(0,pi/2,90);
y = sin(x);
A. Yes
B. No
A. 0
B. 1
C. 2
D. 3
E. 4
Question Time
Does this assign to y the values
sin(0o), sin(1o),…,sin(90o)?
%x = linspace(0,pi/2,90);
x = linspace(0,pi/2,91);
y = sin(x);
A. Yes
B. No
7
Ellipse Perimeter Estimates
Ellipse Perimeter Estimates
6.5
b = linspace(.1,1,100);
a = 1; h = (a - b)./(a + b);
P1 = pi*(a+b);
P2 = pi*sqrt(2*(a^2+b.^2));
P3 = pi*sqrt(2*(a^2+b.^2)-…
((a - b).^2)/2);
P4 = pi*(a + b).*(1 + h/8).^2;
plot(b,P1,b,P2,b,P3,b,P4)
Plotting an Ellipse
2
5.5
5
4.5
4
3.5
3
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Solution
a = input(‘Major semiaxis:’);
B = input(‘Minor semiaxis:’);
2
⎛ x⎞ ⎛ y⎞
⎜ ⎟ +⎜ ⎟ =1
⎝a⎠ ⎝b⎠
Better:
(a cos(t ), b sin(t ) )
6
0 <= t <= 2π
t = linspace(0,2*pi,200);
x = a*cos(t);
y = b*sin(t);
plot(x,y)
axis equal off
a = 5, b = 3
4
3
2
1
0
−1
−2
−3
−4
−6
−4
−2
0
2
4
6
8