





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Goals:
Welcome to your first 6.01 design lab! These labs are generally intended to let you explore concepts introduced in the lecture, working together with a lab partner. Each design lab consists of instruc tions for setup, and a set of problems, which are done with robots, instrumentation, computers, and electronics components in the laboratory. Each problem is specified by its objectives , the documentation of the resources provided, and occasionally detailed guidance of the steps involved. Problem answers are generally entered into the 6.01 Online Tutor. Some problem specifications will be provided entirely on the Tutor.
Due to the late start of classes this term, today’s design lab is a bit unusual. Design labs are gener ally done with partners, but this one should be done individually.
Laptops : Please use a lab laptop unless you have already installed Python 2.6.x on yours. If you have not installed Python, please do so after.
Python : If you have already worked through the Python programming tutor and/or have had other Python experience, then go ahead and do the problems below. If not , then please work through the Python tutor.
Some of the software and design labs contain the command athrun 6.01 getFiles. Please disregard this instruction; the same files are available on the 6.01 OCW Scholar site as a .zip file, labeled Code for [Design or Software Lab number].
2 PCAP Exercises The following are three problems covering concepts of Primitives, Combination, Abstraction, and Patterns.
the nth^ Fibonacci number. Recall the definition of fib ⎧
(n): ⎨ 0 if n =^0 fib(n) = 1 if n = 1 ⎩ (^) fib(n− 1 ) + fib(n− 2 ) if n > 1
Resources:
Detailed guidance : We recommend using idle , an integrated development enviroment for Python. In the Terminal window, type
For example:
Resources:
you do
print V2(1.1, 2.2)
it prints
V2[1.1, 2.2]
Exactly what gets printed as a result of this statement depends on how you’ve defined your
string. Don’t worry about making this beautiful!
respectively. For example,
v = V2(1.0, 2.0) v.getX()
v.getY()
a = V2(1.0, 2.0) b = V2(2.2, 3.3) print a.add(b) V2[3.2, 5.3] print a.mul(2) V2[2.0, 4.0] print a.add(b).mul(-1) V2[-3.2, -5.3]
Step 4. A cool thing about Python is that you can overload the arithmetic operators. So, for example, if
def add(self, v): return self.add(v)
then you can do
print V2(1.1, 2.2) + V2(3.3, 4.4) V2[4.4,6.6]
scalar will always be the second argument.
pasting it into the tutor problem Wk.1.3..
3 Polynomial Class
ing algebraic operations on polynomials. Your class should behave as de scribed in the following sample transcript:
p1 = Polynomial([1, 2, 3]) p 1.000 z**2 + 2.000 z + 3. p2 = Polynomial([100, 200]) p1.add(p2)
v = V2(1.0, 2.0) >>> v.getX() 1. >>> v.getY() 2. ### Step 3. Define the add and mul methods, so that you get the following behavior: >>> a = V2(1.0, 2.0) >>> b = V2(2.2, 3.3) >>> print a.add(b) V2[3.2, 5.3] >>> print a.mul(2) V2[2.0, 4.0] >>> print a.add(b).mul(-1) V2[-3.2, -5.3] Step 4. A cool thing about Python is that you can overload the arithmetic operators. So, for example, if ### you add the following method to your V2 class def add(self, v): return self.add(v) then you can do >>> print V2(1.1, 2.2) + V2(3.3, 4.4) V2[4.4,6.6] ### Add to the class the add method, which should call your add method to add vectors, and ### the mul method, which should call your mul method to multiply the vector by a scalar. The scalar will always be the second argument. ### Test your implementation in idle until it seems correct to you. ### Wk.1.3.4 Check your results by copying the text of your procedure from idle and pasting it into the tutor problem Wk.1.3.. 3 Polynomial Class ### Objective: Define a Python class Polynomial which provides methods for perform ing algebraic operations on polynomials. Your class should behave as de scribed in the following sample transcript: >>> p1 = Polynomial([1, 2, 3]) >>> p 1.000 z2 + 2.000 z + 3. >>> p2 = Polynomial([100, 200]) >>> p1.add(p2) 1.000 z2 + 102.000 z + 203. p1 + p 1.000 z**2 + 102.000 z + 203. p1(1)
p1(-1)
(p1 + p2)(10)
p1.mul(p1) 1.000 z4 + 4.000 z3 + 10.000 z2 + 12.000 z + 9. p1 * p 1.000 z4 + 4.000 z3 + 10.000 z2 + 12.000 z + 9. p1 * p2 + p 100.000 z3 + 401.000 z2 + 702.000 z + 603. p1.roots() [(-1+1.4142135623730947j), (-1-1.4142135623730947j)] p2.roots() [-2.0] p3 = Polynomial([3, 2, -1]) p3.roots() [-1.0, 0.33333333333333331] (p1 * p1).roots() Order too high to solve for roots.
to return a real-valued result when it raises a real number to a fractional power. It returns a
2j. Try to do things as simply as possible. Don’t do anything twice. If you need some extra procedures to help you do your work, you can put them in the same file as your class definition, but outside the class (so, put them at the end of the file, with no indentation).
So for example, include
def add(self, other): return self.add(other) def mul(self, other): return self.mul(other) def call(self, x): return self.val(x)
Also, in order to have your polynomials printed out nicely by the Python shell, you can add this line to your class:
def repr(self): return str(self)
Wk.1.3.6 After you have debugged in idle, check and submit your results by copying the text of your class and associated definitions from idle and pasting it into the tutor problem Wk.1.3..
puting the value of a polynomial, it structures the computation of
a^ n^ n−^1 nx^ +^ an− 1 x^ +^ · · ·+^ a 2 x^ +^ a 1 x^ +^ a 0
as
( · · · (anx + an− 1 )x + · · ·+ a 1 ) x + a 0
In other words, we start with an, multiply the entire result by x, add an− 1 , multiply by x, and so on, until we reach a 0. For example, we’d evaluate 8x^3 − 3x^2 + 4x + 1 as
(( 8 · x − 3 ) · x + 4 ) ·x + 1
tions it takes to evaluate a polynomial using Horner’s rule, compared to the usual way.