EECS 110 Recitation #3 - Northwestern University

advertisement
EECS 110
Recitation #3
Ionut Trestian
Northwestern University
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
2
3
4
5
6
7
8
9
10
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 fracSteps(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 fracSteps(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
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
Hw2 Pr2
Platt
...
Dorms
(W)
0
...
S
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
Hw2 Pr2
Platt
...
Dorm
(W)
0
...
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
rwPos(s, nsteps)
take nsteps random
steps starting at s
rwSteps(s, low, hi)
take random steps starting at s until
you reach either low or hi
(E)
rwPos(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
rwSteps(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 rwPos()
• 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 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/EECS110-s09/misc/TurtleDirections.htm
for turtle help
hw2pr3 spiral (I)
81
72.9
90
close-up of innermost
part of the spiral…
spiral( 100, 90, 0.9 )
100
spiral( initLength, angle, multiplier )
hw2pr3 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
hw2pr3 svTree (I)
svTree( 100, 4 )
svTree( trunkLength, levels )
and more! (if you want)
hw2pr3 svTree (II)
• Function takes as input a segment length and the
number of tree levels
• Uses recursion
• Each call to svTree:
• Decrease number of levels
• Draw part of tree at this level
• Check base case
• Call recursively svTree 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, 1 )
snowflake( 100, 4 )
snowflake( 100, 2 )
snowflake( 100, 5 )
The Koch curve (II)
• snowFlake 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 !
Download