33
Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic practice with iteration and condition

Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

9. The Discrete vs

The Continuous

Finite Arithmetic

More practice with iteration and conditionals.

Page 2: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Screen Granularity

After howmany halvingswill thedisks disappear?

Page 3: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Xeno’s Paradox

• A wall is two feet away.

• Take steps that repeatedly halve the remaining distance.

• You never reach the wall because the distance traveled after n steps =

1 + ½ + ¼ + … + 1/2n = 2 – 1/2n

Page 4: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Problem: “Xeno” Disks

xFirst disk hasradius 1 and center ( 1/2 , 0).

The disks aretangent to each other and havecenters on x-axis

Page 5: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Problem: Xeno Disks

x The radius of a disk is half the radius of the disk to its left.

Draw 20 disks.

Page 6: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Variable Definitions

x: the x-value of the left tangent point

for a given circle.

d : the diameter of a given circle

Page 7: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Preliminary Notes

Disk x d-------------------------------------------------------

1 0 1 2 0+1 1/2

3 0+1+1/2 1/4

Page 8: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Pseudocode

x = 0; d = 1for k=1:20

Draw the next disk. Update x and d.

end

Page 9: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Refinement

Draw the next disk

Draw disk with diameter dand left tangent point (x,0)

DrawDisk(x+d/2,0,d/2,’y’)

Page 10: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Refinement

Update x and d?

Disk x d-------------------------------------------------------

1 0 1 2 0+1 1/2

3 0+1+1/2 1/4

Next x is current x + current d.Next d is one-half current d.

Page 11: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Refinement

Update x and d.

Next x is current x + current d.Next d is one-half current d.

x = x + d;d = d/2;

Page 12: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Solution

x = 0;

d = 1;

for k = 1:20

DrawDisk(x+d/2,0,d/2,'y')

x = x+d;

d = d/2;

end

Page 13: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Output

Shouldn’t there be 20 disks?

Page 14: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Screen is an Array of Dots*

Disks smaller than the dots don’t show up.The 20th disk has radius < .000001

*Called “Pixels”

Page 15: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Finiteness

It shows up all over the place in computing.

Page 16: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Plotting Continuous Functions

Can only display a bunch of dots

Another “collision” between the infiniteand the finite. (More later.)

Page 17: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

The Discrete Display of Sine

N = 100;X_spacing = 4*pi/N;Dot_radius = X_spacing/3;for k=0:N x = k*X_spacing; y = sin(x); DrawDisk(x,y,Dot_Radius,'r')end

Page 18: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

The Moral

To produce realistic plots/renderingsyou must appreciate screen

granularity.

Page 19: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Memory Hardware is finite.

Computer cannot store never-endingdecimals like pi, sqrt(2), 1/3.

Similar Finite “Behavior” with Computer Arithmetic

Page 20: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Question Time

Does this script print anything?

k = 0;

while 1 + 1/2^k > 1

k = k+1;

end

k = k

A. Yes B. No E. None of these

Page 21: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Similar “Behavior” for Computer Arithmetic

Suppose you have a calculator witha window like this:

+ 312 4 -

Representing 2.41 x 10-3

Page 22: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Add:

+ 312 4 -

+ 301 0 -

+ 313 4 -Result:

Page 23: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Add:

+ 312 4 -

+ 401 0 -

+ 312 5 -Result:

Page 24: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Add:

+ 312 4 -

+ 501 0 -

+ 322 4 -Result:

Page 25: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Add:

+ 312 4 -

+ 601 0 -

+ 312 4 -Result:

Page 26: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Add:

+ 312 4 -

+ 601 0 -

+ 312 4 -Result:

Not enough room

to represent .00241

1

Page 27: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Regarding the Question…

k = 0;

while 1 + 1/2^k > 1

k = k+1;

end

k = k

The following loop does terminate and the concluding value of k that is displayed is 53.

Page 28: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

The Moral

To produce reliable numerical resultsyou must appreciate floating pointarithmetic.

Page 29: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

The 1991 Patriot Missile Disaster

Elementarymisperceptionsabout the finitenessof computerarithmetic.30+ died.

Page 30: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

The Setting

External clock counts time in tenthsof seconds.

Targeting software needs time to compute trajectories. The method:

Time = (# external clock ticks) x (1/10)

The problem is here

Page 31: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

One-Tenth in Binary

Exact: .00011001100110011001100110011…

Patriot System used:

.00011001100110011001100110011…

Error = .000000095sec every clock tick

Page 32: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

Error

Time = (# external clock ticks) x (1/10)

Error = (# external clock ticks) x (.000000095)

Page 33: Insight Through Computing 9. The Discrete vs The Continuous Finite Arithmetic More practice with iteration and conditionals

Insight Through Computing

After 100 hours…

Error = (100x60x60*10)*.000000095 = .34 secs

Missed target by 500 meters.