Download presentation
Presentation is loading. Please wait.
Published byNorah Baker Modified over 9 years ago
1
EECS 110 Recitation #3 Ning Xia Northwestern University
2
11023456789 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 Integration Lab 2: Tuesday
3
11023456789 1 0.5 0 11023456789 1 0.5 0 low = 1.0hi = 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)
4
Numerical Integration def fracSteps(N): return [ x/N for x in range(N) ] 0141 42 def steps(low,hi,N): return [ low + (hi-low)*x for x in fracSteps(N) ] 016 777 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 10 width
5
An example closer to home... 25262728502423220 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) DormsMichigan 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.
6
An example closer to home... 25262728502423220 DormMichigan 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.
7
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
8
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.
9
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
10
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-s14/misc/TurtleDirections.htm
11
hw2pr3 spiral (I) 100 90 81 72.9 spiral( initLength, angle, multiplier ) close-up of innermost part of the spiral… spiral( 100, 90, 0.9 )
12
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
13
hw2pr3 svTree (I) svTree( trunkLength, levels ) svTree( 100, 4 ) and more! (if you want)
14
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
15
The Koch curve (I) snowflake( 100, 0 ) snowflake( 100, 1 )snowflake( 100, 2 ) snowflake( 100, 3 )snowflake( 100, 4 )snowflake( 100, 5 )
16
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
17
Good Luck !
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.