EECS 110 Lec 6 Fractals and Trutles Aleksandar

  • Slides: 36
Download presentation
EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University http: //cs. northwestern.

EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University http: //cs. northwestern. edu/~akuzma/classes/EECS 110 -s 09/

EECS 110 Today hw 2 due Sunday evening… Fractals and Turtles! The Koch Curve

EECS 110 Today hw 2 due Sunday evening… Fractals and Turtles! The Koch Curve

return to recursion Composing functions into specific applications what applications? Creating general functions that

return to recursion Composing functions into specific applications what applications? Creating general functions that will be useful everywhere (or almost…)

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

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

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

1 def recip(x): return 1. 0/x 0. 5 y= area 0 1 x 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

When good programs go bad… def power(b, p): """ Returns b**p for p >=

When good programs go bad… def power(b, p): """ Returns b**p for p >= 0 """ if p == 0: return 1 else: return b*power(b, p)

print: Making programs talk to you! Debugging had to be discovered. I can remember

print: Making programs talk to you! Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. - Maurice Wilkes

When good programs go bad… def power(b, p): """ Returns b**p for p >=

When good programs go bad… def power(b, p): """ Returns b**p for p >= 0 """ print "p is", p, "; b is", b if p == 0: return 1 else: return b*power(b, p)

Careful! print != return def power(b, p): """ Returns b**p for p >= 0

Careful! print != return def power(b, p): """ Returns b**p for p >= 0 """ if p == 0: return 1 else: return b*power(b, p-1) def power. Print(b, p): """ Returns(? ) b**p for p >= 0 """ if p == 0: print 1 else: print b*power. Print(b, p-1)

A random aside… import random for more explanation, try dir(random) or help(random) random. choice(

A random aside… import random for more explanation, try dir(random) or help(random) random. choice( L ) chooses 1 element from the list L random. choice( ['north', 'case', 'west'] ) How would you get a random int from 0 to 9? random. uniform(low, hi) chooses a random float from low to hi random. uniform(41. 9, 42. 1) How likely is this to return 42 ?

A random function… from random import * def guess( hidden ): """ guesses the

A random function… from random import * def guess( hidden ): """ guesses the user's hidden # """ compguess = choice( range(100) ) if compguess == hidden: print 'I got it!' else: guess( hidden ) # at last!

The final version from random import * import time def guess. Final( hidden ):

The final version from random import * import time def guess. Final( hidden ): """ guesses the user's hidden # """ compguess = choice( range(100) ) print 'I choose', compguess time. sleep(0. 05) if compguess == hidden: # at last! print 'I got it!' return 0 else: return 1 + guess. Final( hidden )

The two Monte Carlos Making random numbers work for you! Monte Carlo methods, Math/CS

The two Monte Carlos Making random numbers work for you! Monte Carlo methods, Math/CS Monte Carlo casino, Monaco

Monte Carlo in action Suppose you roll two dice. What are the chances that

Monte Carlo in action Suppose you roll two dice. What are the chances that you roll doubles? input is the total number of rolls def count. Doubles( N ): """ inputs a # of dice rolls outputs the # of doubles """ if N == 0: return 0 # zero rolls, zero doubles… else: d 1 = choice( [1, 2, 3, 4, 5, 6] ) one roll of d 2 = choice( range(1, 7) ) the dice if d 1 != d 2: return count. Doubles( N-1 ) # not doubles else: return # doubles! what should the last line be?

Monte Carlo in action Suppose you roll two dice. What are the chances that

Monte Carlo in action Suppose you roll two dice. What are the chances that you roll doubles? input is the total number of rolls def count. Doubles( N ): """ inputs a # of dice rolls outputs the # of doubles """ if N == 0: return 0 # zero rolls, zero doubles… else: d 1 = choice( [1, 2, 3, 4, 5, 6] ) one roll of d 2 = choice( range(1, 7) ) the dice if d 1 != d 2: return count. Doubles( N-1 ) # not doubles else: return 1 + count. Doubles( N-1 ) # doubles!

Monty Hall Let’s make a deal ’ 63 -’ 86 inspiring the “Monty Hall

Monty Hall Let’s make a deal ’ 63 -’ 86 inspiring the “Monty Hall paradox”

Monte Carlo Monty Hall Suppose you always switch to the other door. . .

Monte Carlo Monty Hall Suppose you always switch to the other door. . . What are the chances that you will win the car ? Run it (randomly) 1000 times and see!

Monte Carlo Monty Hall Your initial choice! 'switch' or 'stay' number of times to

Monte Carlo Monty Hall Your initial choice! 'switch' or 'stay' number of times to play def MCMH( init, sors, N ): """ plays the same "Let's make a deal" game, N times returns the number of times you win the car """ if N == 0: return 0 # don't play, can't win car. Door = choice([1, 2, 3]) # where is the car? if init == car. Door and sors == 'stay': result = 'Car!' elif init == car. Door and sors == 'switch': result = 'Spam. ' elif init != car. Door and sors == 'switch': result = 'Car!' else: result = 'Spam. ' print 'You get the', result if result == 'Car!': else: return 1 + MCMH( init, sors, N-1 ) return 0 + MCMH( init, sors, N-1 )

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

An example closer to home. . . Dorms (W) 0 Platt S Hw 2 Pr 2 . . . 22 23 24 25 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 Hw 2

An example closer to home Platt . . . Dorm (W) 0 Hw 2 Pr 2 . . . S 22 23 24 25 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)

Gel electrophoresis one of many applications for random walks… uses a basic randomwalk model

Gel electrophoresis one of many applications for random walks… uses a basic randomwalk model with unequal step probabilities Used to separate proteins and nucleic acids (DNA) from a biological sample. Molecules with different properties travel different distances.

Monte Carlo Applications folding @ home text on MC approaches to protein folding (a)

Monte Carlo Applications folding @ home text on MC approaches to protein folding (a) start configuration (b) end (c) 3 d-model

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

Etch-a-Sketch ? www. gvetchedintime. com No way this is real… except that it is

Etch-a-Sketch ? www. gvetchedintime. com No way this is real… except that it is !

Recursive Graphics there is no tri … def tri(): """ draws a polygon """

Recursive Graphics there is no tri … def tri(): """ draws a polygon """ forward(100) left(120) (1) Could we tri this with recursion?

Recursive Graphics there is no tri … def tri(): """ draws a polygon """

Recursive Graphics there is no tri … def tri(): """ draws a polygon """ forward(100) left(120) (1) Could we tri this with recursion? def tri(N): """ Plots a triangle """ if N == 0: return else: forward(100) left(120) tri(N-1)

Name(s): (1) What does chai draw? Turtle Graphics (2) "Quiz" Finish rwalk to draw

Name(s): (1) What does chai draw? Turtle Graphics (2) "Quiz" Finish rwalk to draw a "stock-market-type" random path of nsteps. Use recursion! from random import * def chai(size): """ mystery! """ forward(size) left(90) forward(size/2. 0) right(90) backward(size) def rw(nsteps): """ moving for nsteps, randomly 45 deg. left/up or right/down """ if nsteps == 0: return if choice(['left', 'right']) == 'left': else: # 'right' one possible result of rw(20)

(1) What does chai draw? def chai(size): """ mystery! """ forward(size) left(90) forward(size/2. 0)

(1) What does chai draw? def chai(size): """ mystery! """ forward(size) left(90) forward(size/2. 0) right(90) backward(size) Why are there two identical commands in a row?

(2) Finish rwalk to draw a "stock-market-type" random path of nsteps. from random import

(2) Finish rwalk to draw a "stock-market-type" random path of nsteps. from random import * def rw(nsteps): """ moving for nsteps, randomly 45 deg. left/up or right/down """ if nsteps == 0: return if choice(['left', 'right']) == 'left': left(45) forward(20) right(45) else: # 'right‘ right(45) forward(20) left(45) return rw(nsteps-1) one possible result of rw(20) What if we didn't go back to the starting pose?

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

hw 2 pr 3 spiral 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 sv. Tree( 100, 4 ) sv. Tree( trunk. Length, levels

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

Help! My turtle window froze! Your turtle window becomes unresponsive after your program runs.

Help! My turtle window froze! Your turtle window becomes unresponsive after your program runs. Type: >>> done() to unlock it (but then you have to close it)

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

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

Have fun! http: //cs. northwestern. edu/~akuzma/classes/EECS 110 -s 09/misc/Turtle. Directions. htm fill(1) color("blue")

Have fun! http: //cs. northwestern. edu/~akuzma/classes/EECS 110 -s 09/misc/Turtle. Directions. htm fill(1) color("blue")

Have a great weekend! good luck with hw 2…

Have a great weekend! good luck with hw 2…