DOC PREVIEW
MIT 6 01 - Work for Week 2

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

6.081, Spring Semester, 2007—Work for Week 2 1MASSACHVSETTS INSTITVTE OF TECHNOLOGYDepartment of Electrical Engineering and Computer Science6.081—Introduction to EECS ISpring Semester, 2007Work for Week 2Issued: Tuesday, Feb. 13This handout contains:• Software Lab for Tuesday, February 13• Pre-Lab exercises due Thursday, February 15 at 2PM; you should come and do them in lab onWednesday, February 14.• Robot Lab for Thursday, February 15 (read this before coming to Lab)• Post-lab exercises due Thursday Feb. 22. There is no lecture on Tuesday, Feb. 20 (MIT virtualMonday).Higher-order procedures; Behaviors and utility functionsThis week’s work gives you practice with higher-order procedures, i.e., procedures that manipu-late other procedures. Tuesday’s lecture and post-class lab cover Python’s support for functionalprogramming, including list comprehension. Wednesday’s homework and Thursday’s lab applyhigher-order procedures to the tasks of specifying robot behaviors with the aid of utility functions.Tuesday Software Lab: Higher-order pr oceduresThis session gives you practice with Python’s basic tools for functional programming: higher-orderprocedures and list comprehension.A higher-order procedure is a proc edure that takes takes procedures as inputs and/or returnsprocedures as results. The basic example we’ll work with now is a procedure called plot whichtakes a function f as input and graphs it.Start by loading the file feb-13-class.py. It begins with the following code:# import the graphing package from SoaRimport SoaRfrom SoaR.Util.GraphingWindow import GraphingWindow# import sine and cosineimport Numericfrom Numeric import sinfrom Numeric import cos# import the version of addition for use with reduceimport operatorfrom operator import add as add# set up a variable for the plotting windowplotWindow = None6.081, Spring Semes ter, 2007—Work for We ek 2 2# plot a given function fdef plot(f):global plotWindowif plotWindow: plotWindow.close()xmax=10xvals = [i/100. for i in range(0,int(xmax*100))]yvals = [f(x) for x in xvals]ymax = reduce(max,[abs(y) for y in yvals])graphmax = 1.1 * ymaxplotWindow = GraphingWindow(500, 500, 0, xmax, -graphmax, graphmax, "Plot")plotWindow.graphContinuous(f)plotWindow.helpIdle()The plot procedure opens a graphing window and plots f in the interval from 0 to 10. The systemthen waits (hangs) until you close the plotting window with the mouse.1Note that the input f toplot is a procedure.Question 1. What do you expect plot(lambda x: x) to produce? Try it and s ee.Question 2. Make a plot of the sine function. Do you have to write plot(lambda x: sin(x))or can you simply write plot(sin)? Try it and see. Make sure to ask if the result isn’twhat you expect.Observe how plot uses list comprehension and reduce with max to compute the ranges for the axes.If you’ve programmed before, you probably expected that this would be done using some kind ofloop. One of the hallmarks of functional style is to use functions operating on sets of elements,rather than writing explicit loops with for or while.Now let’s plot something more interesting—superpositions of sine waves of increasing frequency:sin x +12sin 2x +13sin 3x +14sin 4x + ···out to more and more terms.Let’s call saw(n) the function whose value at x is the sum of the first n −1 terms of this sequence.Then we can compute saw asdef saw(n):return lambda x: reduce(add,[1.0/j * sin(j*x) for j in range(1,n)])(This definition is included in the file you loaded.) Here saw is a procedure that takes n as inputand returns a function of x, which we can pass to plot. Observe again how we’ve used listcomprehension and reduce with add rather than write an explicit loop to sum the terms.Question 3. Plot saw(n) for various values of n, starting with n = 2, which is just a sine wave(do you see why?) up to large values of n, like 100. It should become obvious why we’venamed the procedure saw.1We’re embarrassed about setting things up this way. It’s a kludge to get around a bad interaction between theSoaR robot system and Python’s Idle editor.6.081, Spring Semes ter, 2007—Work for We ek 2 3As you’ll learn later (in 18.03 and/or 6.003), any periodic function can be expressed as a Fourierseries, i.e., a s uperposition of sines and cosines of the forma1sin x + b1cos x + a2sin 2x + b2cos 2x + ··· + aksin kx + bkcos kx + ···Question 4. Define and plot the function squareW ave(n) that produces partial sums of theFourier expansion of a square wave:sin x +13sin 3x +15sin 5x +17sin 7x + ···Do this with list comprehension, using a construct of the form:[<expression in j> for j in <range> if <condition>]Question 5. Do the same thing for a triangle wave, whose Fourier expansion iscos x +19cos 3x +125cos 5x +149cos 7x + ···Now let’s rewrite these programs using an “even more functional” style, s imilar to what you’ll doin lab this week.The following procedure addf (included in the file you loaded) takes as inputs two functions f andg and returns a function whose value at any x is f (x) + g(x):def addf(f,g):return lambda x: f(x)+g(x)The important thing to observe is that the inputs to addf, as well as the result, are functions, i.e.,addf transforms functions to functions.Similarly, we have scalef, which takes a function f and a number s and return the function whosevalue at x is s × f(x), and expandf, which produces the function whose value at x is f (s × x).Convince yourself that an equivalent way to write the saw procedure isdef saw2(n):return reduce(addf,[scalef(expandf(sin,j),1.0/j) f or j in range(1,n)])and make some plots. The definitions of addf, scalef, expandf, and saw2 are included in the fileyou loaded.Question 6. Write similar definitions for square waves and triangle waves and check the resultsby plotting them.For the rest of this period, play around with these procedures as you have time, doing things thatinterest you. Here are some ideas to try. You needn’t do all of them.6.081, Spring Semes ter, 2007—Work for We ek 2 4Question 7. Change the s quare, triangle, and sawtooth wave definitions to use cosines in placeof sines and vice versa, and see what kind curves this produces. Or try some other Fourierseries.Question 8. Generalize the plot procedure so that you can specify the range for x.Question 9. Change the plot procedure to use the procedure plotWindow.graphDiscreterather than plotWindow.graphContinuous. This is the sort of plotting we’ll be


View Full Document

MIT 6 01 - Work for Week 2

Documents in this Course
Week 1

Week 1

3 pages

Op-Amps

Op-Amps

8 pages

Op-Amps

Op-Amps

6 pages

Syllabus

Syllabus

14 pages

Planning

Planning

14 pages

Load more
Download Work for Week 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Work for Week 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Work for Week 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?