eecs 110: lec 6: fractals and trutles aleksandar kuzmanovic northwestern university
TRANSCRIPT
![Page 1: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/1.jpg)
EECS 110: Lec 6: Fractals and Trutles
Aleksandar Kuzmanovic
Northwestern University
http://networks.cs.northwestern.edu/EECS110-s15/
![Page 2: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/2.jpg)
Midterm and Final (tentative)
• Midterm:– Wednesday 4/29/2015– 9:30am – 11:30am– Tech L361
• Final:– Wednesday 6/3/2015– 9:30am – 11:30am– Tech L361
![Page 3: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/3.jpg)
EECS 110 Today
hw2 due Sunday evening…
Fractals and Turtles!
The Koch Curve
![Page 4: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/4.jpg)
Quiz Write each of these functions concisely using list comprehensions…
Write
def count(e,L):
Write
def lotto(Y,W):
input: e, any element L, any list or stringoutput: the # of times L contains e example: count('f', 'fluff') == 3
input: Y and W, two lists of lottery numbers (ints)
output: the # of matches between Y & W
example: lotto([5,7,42,44],[3,5,7,44]) == 3
Y are your numbers
W are the winning numbers
Name(s):
Remember True == 1 and False == 0
Extra! Write
def divs(N):
input: N, an int >= 2output: the number of positive divisors of Nexample: divs(12) == 6 (1,2,3,4,6,12)
![Page 5: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/5.jpg)
Quiz
![Page 6: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/6.jpg)
Quiz
LC = [x==e for x in L] return sum( LC )
count(e,L)
![Page 7: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/7.jpg)
Quiz
lotto(Y,W)LC = [c in Y for c in W]return sum( LC )
![Page 8: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/8.jpg)
Quiz
divs(N)LC = [ N%c==0 for c in range(1,N+1)] return sum( LC )
![Page 9: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/9.jpg)
Quiz
LC = [x==e for x in L] return sum( LC )
count(e,L)
divs(N)
lotto(Y,W)LC = [c in Y for c in W]return sum( LC )
LC = [ N%c==0 for c in range(1,N+1)] return sum( LC )
![Page 10: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/10.jpg)
return to recursion
Composing functions into specific applications
Creating general functions that will be useful everywhere (or almost…)
what applications?
![Page 11: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/11.jpg)
1 102 3 4 5 6 7 8 9
1
0.5
0
y = 1 x area
steps(low,hi,N) fsteps(recip,low,hi,N)
def recip(x): return 1.0/x
finteg(f,low,hi,N)
Numerical IntegrationLab 2: Tuesday (yesterday)
![Page 12: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/12.jpg)
1 102 3 4 5 6 7 8 9
1
0.5
0
1 102 3 4 5 6 7 8 9
1
0.5
0
low = 1.0 hi = 10.0
y = 1 x area
steps(low,hi,N)
N == 9 == total number of steps (rectangles)
fsteps(recip,low,hi,N)
def recip(x): return 1.0/x
finteg(f,low,hi,N)
![Page 13: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/13.jpg)
Numerical Integration
def fracSteps(N):
return [ x/float(N) for x in range(N) ] 0 1 41
424242
def steps(low,hi,N):
return [ low + (hi-low)*x for x in fracSteps(N) ]0 1 6
77710 10 15
def fsteps(f,low,hi,N):
return [ f(x) for x in steps(low,hi,N) ]
def finteg(f,low,hi,N): return sum(fsteps(f,low,hi,N))*(hi-low)/float(N)
x values
fractional steps
y values
integral: heights
10 16
1610width
![Page 14: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/14.jpg)
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)
![Page 15: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/15.jpg)
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
![Page 16: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/16.jpg)
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)
![Page 17: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/17.jpg)
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 powerPrint(b, p): """ Returns(?) b**p for p >= 0 """ if p == 0: print 1 else: print b*powerPrint(b, p-1)
![Page 18: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/18.jpg)
A random aside…
import random
random.choice( L )
random.uniform(low,hi)
random.choice( ['north', 'case', 'west'] )
random.uniform(41.9,42.1)
chooses 1 element from the list L
chooses a random float from low to hi
for more explanation, try dir(random) or help(random)
How likely is this to return 42 ?
How would you get a random int from 0 to 9?
![Page 19: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/19.jpg)
A random function…
from random import *
def guess( hidden ): """ guesses the user's hidden # """ compguess = choice( range(100) )
if compguess == hidden: # at last! print( 'I got it!’ )
else: guess( hidden )
![Page 20: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/20.jpg)
The final version
from random import *import time
def guessFinal( 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 + guessFinal( hidden )
![Page 21: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/21.jpg)
The two Monte Carlos
Monte Carlo casino, Monaco
Making random numbers work
for you!
Monte Carlo methods, Math/CS
![Page 22: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/22.jpg)
Monte Carlo in action
Suppose you roll two dice.What are the chances that you roll doubles?
def countDoubles( N ): """ inputs a # of dice rolls outputs the # of doubles """ if N == 0: return 0 # zero rolls, zero doubles… else: d1 = choice( [1,2,3,4,5,6] ) d2 = choice( range(1,7) ) if d1 != d2: return countDoubles( N-1 ) # not doubles else: return # doubles!
one roll of the dice
input is the total number of rolls
what should the last line be?
![Page 23: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/23.jpg)
Monte Carlo in action
Suppose you roll two dice.What are the chances that you roll doubles?
def countDoubles( N ): """ inputs a # of dice rolls outputs the # of doubles """ if N == 0: return 0 # zero rolls, zero doubles… else: d1 = choice( [1,2,3,4,5,6] ) d2 = choice( range(1,7) ) if d1 != d2: return countDoubles( N-1 ) # not doubles else: return 1 + countDoubles( N-1 ) # doubles!
one roll of the dice
input is the total number of rolls
![Page 24: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/24.jpg)
Monty Hall
Let’s make a deal ’63-’86
inspiring the “Monty Hall paradox”
![Page 25: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/25.jpg)
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!
![Page 26: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/26.jpg)
Monte Carlo Monty Hall
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 carDoor = choice([1,2,3]) # where is the car?
if init == carDoor and sors == 'stay': result = 'Car!' elif init == carDoor and sors == 'switch': result = 'Spam.' elif init != carDoor and sors == 'switch': result = 'Car!' else: result = 'Spam.'
print( 'You get the', result ) if result == 'Car!': return 1 + MCMH( init, sors, N-1 ) else: return 0 + MCMH( init, sors, N-1 )
Your initial choice! 'switch' or 'stay'number of times to play
![Page 27: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/27.jpg)
An example closer to home
......25 26 27 28 502423220
An overworked NU student (S) leaves a pub after a “late-night” breakfast and, each moment, randomly
stumbles toward Dorms (W) or toward Michigan Lake (E)
Dorms
Michigan Lake
(E)
(W)
Platt
Write a program to model and analyze! this scenario...
Hw2 Pr2
S
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.
![Page 28: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/28.jpg)
An example closer to home
......25 26 27 28 502423220
Dorm Michigan Lake
(E)
(W)
Platt
Write a program to model and analyze! this scenario...
Hw2 Pr2
S
rs() rwPos(s, nsteps) rwSteps(s, low, hi)
take a random step of +1 or -1
take nsteps random steps starting at s
take random steps starting at s until you reach either low or hi
An overworked NU student (S) leaves a pub after a “late-night” 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.
![Page 29: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/29.jpg)
uses a basic random-walk model with unequal step probabilities
Gel electrophoresis
Used to separate proteins and nucleic acids (DNA) from a
biological sample. Molecules with different properties travel
different distances.
one of many applications for random walks…
![Page 30: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/30.jpg)
Monte Carlo Applications
text on MC approaches to protein folding
(a) start configuration (b) end (c) 3d-model
folding @ home
![Page 31: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/31.jpg)
Python's Etch-a-Sketch
A new human-computer interface?
from turtle import *
reset()
left(90)
forward(50)
right(90)
backward(50)
down() or up()
color('green')
width(5)
done()
and lots more!
for turtle help
degrees!
states if the pen draws or not
http://networks.cs.northwestern.edu/EECS110-s15/misc/TurtleDirections.htm
![Page 32: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/32.jpg)
Etch-a-Sketch ?www.gvetchedintime.com
No way this is real…
except that it is !
![Page 33: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/33.jpg)
Recursive Graphics
def tri(): """ draws a polygon """ forward(100) left(120) forward(100) left(120) forward(100) left(120)
there is no tri … Could we tri this with recursion?(1)
![Page 34: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/34.jpg)
Recursive Graphics
def tri(): """ draws a polygon """ forward(100) left(120) forward(100) left(120) forward(100) left(120)
there is no tri … Could we tri this with recursion?(1)
def tri(N): """ Plots a triangle """ if N == 0: return else: forward(100)
left(120) tri(N-1)
![Page 35: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/35.jpg)
"Quiz"
def chai(size): """ mystery! """ forward(size) left(90) forward(size/2.0) right(90) right(90) forward(size) left(90) left(90) forward(size/2.0) right(90) backward(size)
What does chai draw?(1)
Turtle Graphics
Finish rwalk to draw a "stock-market-type" random path of nsteps steps. Use recursion!
(2)
Name(s):
from random import *
def rw(nsteps): """ moving for nsteps steps, 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)
![Page 36: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/36.jpg)
def chai(size): """ mystery! """ forward(size) left(90) forward(size/2.0) right(90) right(90) forward(size) left(90) left(90) forward(size/2.0) right(90) backward(size)
What does chai draw?(1)
Why are there two identical commands in a row?
![Page 37: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/37.jpg)
Finish rwalk to draw a "stock-market-type" random path of nsteps steps. (2)
one possible result of rw(20)
from random import *
def rw(nsteps): """ moving for nsteps steps, 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)
What if we didn't go back to the starting pose?
![Page 38: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/38.jpg)
hw2pr3 spiral
100
90
81
72.9
spiral( initLength, angle, multiplier )
close-up of innermost part of the spiral…
spiral( 100, 90, 0.9 )
![Page 39: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/39.jpg)
hw2pr3 svTree
svTree( trunkLength, levels )
svTree( 100, 4 )
and more! (if you want)
![Page 40: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/40.jpg)
Help! My turtle window froze!
Your turtle window becomes unresponsive after your programruns. Type:
>>> done()
to unlock it (but then you have to close it)
![Page 41: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/41.jpg)
The Koch curve
snowflake( 100, 0 )snowflake( 100, 1 ) snowflake( 100, 2 )
snowflake( 100, 3 ) snowflake( 100, 4 ) snowflake( 100, 5 )
![Page 42: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/42.jpg)
Have fun!
fill(1) color("blue")http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/misc/TurtleDirections.htm
![Page 43: EECS 110: Lec 6: Fractals and Trutles Aleksandar Kuzmanovic Northwestern University](https://reader035.vdocuments.site/reader035/viewer/2022062321/56649ef15503460f94c02b5b/html5/thumbnails/43.jpg)
Have a great weekend!
good luck with hw2…