EECS 110 Recitation 3 Ionut Trestian Northwestern University

  • Slides: 17
Download presentation
EECS 110 Recitation #3 Ionut Trestian Northwestern University

EECS 110 Recitation #3 Ionut Trestian Northwestern University

1 def recip(x): return 1. 0/x 0. 5 area 0 1 2 1 x

1 def recip(x): return 1. 0/x 0. 5 area 0 1 2 1 x y= 3 4 5 6 7 8 9 10 Numerical Integration Lab 2: Tuesday steps(low, hi, N) fsteps(recip, low, hi, N) finteg(f, low, hi, N)

1 def recip(x): return 1. 0/x 0. 5 area 0 1 x y= 1

1 def recip(x): return 1. 0/x 0. 5 area 0 1 x y= 1 2 3 4 5 6 7 8 9 10 1 0. 5 0 low = 1. 0 steps(low, hi, N) N == 9 == total number of steps (rectangles) fsteps(recip, low, hi, N) hi = 10. 0 finteg(f, low, hi, N)

Numerical Integration def frac. Steps(N): return [ x/float(N) for x in range(N) ] 0

Numerical Integration def frac. Steps(N): return [ x/float(N) for x in range(N) ] 0 42 fractional steps def steps(low, hi, N): return [ low + (hi-low)*x 1 42 41 42 for x in frac. Steps(N) ] x values 10 0 7 10 1 7 15 6 7 def fsteps(f, low, hi, N): return [ f(x) for x in steps(low, hi, N) ] y values 10 16 def finteg(f, low, hi, N): return sum(fsteps(f, low, hi, N))*(hi-low)/float(N) integral: heights width

An example closer to home Platt . . . Dorms (W) 0 . .

An example closer to home Platt . . . Dorms (W) 0 . . . S 22 23 24 25 Hw 2 Pr 2 26 27 28 An overworked NU student (S) leaves a pub after a “latenight” breakfast and, each moment, randomly stumbles toward Dorms (W) or toward Michigan Lake (E) Once the student arrives at the dorms or the Michigan Lake, the trip is complete. The program should then print the total number of steps taken. Write a program to model and analyze! this scenario. . . Michigan Lake 50 (E)

An example closer to home Platt . . . Dorm (W) 0 . .

An example closer to home Platt . . . Dorm (W) 0 . . . S 22 23 24 25 Hw 2 Pr 2 26 27 28 Michigan Lake 50 An overworked NU student (S) leaves a pub after a “latenight” breakfast and, each moment, randomly stumbles toward Dorms (W) or toward Michigan Lake (E) Once the student arrives at the dorm or the Michigan Lake, the trip is complete. The program should then print the total number of steps taken. Write a program to model and analyze! this scenario. . . rs() take a random step of +1 or -1 rw. Pos(s, nsteps) take nsteps random steps starting at s rw. Steps(s, low, hi) take random steps starting at s until you reach either low or hi (E)

rw. Pos(start, nsteps) • start = position of sleepwalker • nsteps = number of

rw. Pos(start, nsteps) • start = position of sleepwalker • nsteps = number of random steps taken by walker • Returns the position of the walker after nsteps random steps • Call to rs() + recursion

rw. Steps(start, low, hi) • • start = position of sleepwalker low = lowest

rw. Steps(start, low, hi) • • start = position of sleepwalker low = lowest position he can be at hi = highest position he can be at Returns the number of steps until walker reaches low or hi • Call to rs() + recursion.

Random walk analysis • Average final signed-displacement - Statistics on the output of rw.

Random walk analysis • Average final signed-displacement - Statistics on the output of rw. Pos() • Average final squared signed-displacement • Both take as input the number of steps of the random walker and the number of statistical runs

Python's Etch-a-Sketch from turtle import * reset() degrees! left(90) forward(50) right(90) backward(50) states if

Python's Etch-a-Sketch from turtle import * reset() degrees! left(90) forward(50) right(90) backward(50) states if the pen draws or not down() or up() color('green') width(5) done() and lots more! A new human-computer interface? http: //cs. northwestern. edu/~akuzma/classes/EECS 110 -s 09/misc/Turtle. Directions. htm for turtle help

hw 2 pr 3 spiral (I) 81 72. 9 90 close-up of innermost part

hw 2 pr 3 spiral (I) 81 72. 9 90 close-up of innermost part of the spiral… spiral( 100, 90, 0. 9 ) 100 spiral( init. Length, angle, multiplier )

hw 2 pr 3 spiral (II) • You can draw it inside out (stop

hw 2 pr 3 spiral (II) • You can draw it inside out (stop at 1000 pixels) or outside in (stop at 1 pixel) • Function takes as input length of segment to draw, angle and a scaling factor for the length of the segment • Uses recursion • Each call to spiral: – Draw line – Change orientation by angle – Update length of segment by scaling – Check base case – Recall spiral

hw 2 pr 3 sv. Tree (I) sv. Tree( 100, 4 ) sv. Tree(

hw 2 pr 3 sv. Tree (I) sv. Tree( 100, 4 ) sv. Tree( trunk. Length, levels ) and more! (if you want)

hw 2 pr 3 sv. Tree (II) • Function takes as input a segment

hw 2 pr 3 sv. Tree (II) • Function takes as input a segment length and the number of tree levels • Uses recursion • Each call to sv. Tree: • Decrease number of levels • Draw part of tree at this level • Check base case • Call recursively sv. Tree twice to draw the left and right subtrees • Don’t forget to change the angles before

The Koch curve (I) snowflake( 100, 0 ) snowflake( 100, 3 ) snowflake( 100,

The Koch curve (I) snowflake( 100, 0 ) snowflake( 100, 3 ) snowflake( 100, 1 ) snowflake( 100, 4 ) snowflake( 100, 2 ) snowflake( 100, 5 )

The Koch curve (II) • snow. Flake gets as input the segment size and

The Koch curve (II) • snow. Flake gets as input the segment size and the number of levels to draw • Helpful to have a function which draws the sides of the triangle. • The side drawing function: • If reached final level, draw full side • Otherwise just call the side drawing function on the 4 sides that we just broke into

Good Luck !

Good Luck !