+ All Categories
Home > Documents > EECS 110 Recitation #3 Ionut Trestian Northwestern University.

EECS 110 Recitation #3 Ionut Trestian Northwestern University.

Date post: 02-Jan-2016
Category:
Upload: claud-douglas
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
17
EECS 110 Recitation #3 Ionut Trestian Northwestern University
Transcript

EECS 110Recitation #3

Ionut TrestianNorthwestern University

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

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)

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

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.

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.

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

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://cs.northwestern.edu/~akuzma/classes/EECS110-s09/misc/TurtleDirections.htm

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 )

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( trunkLength, levels )

svTree( 100, 4 )

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, 1 ) snowflake( 100, 2 )

snowflake( 100, 3 ) snowflake( 100, 4 ) 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 !


Recommended