Quality Python Homework Help
Quality Python Homework Help
number of poles: 2
1.2 Behavior
Does the unit-sample response of the system converge or diverge
as n → ∞?
converge or diverge: diverge
Briefly explain.
1.3 Coefficients
Determine A and B.
A= 1 B -13/36
=
1.4 Behavior
Briefly explain.
2 Geometry OOPs
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distanceTo(self, p):
return math.sqrt((self.x - p.x)**2 + (self.y -
p.y)**2)
def __str__(self):
return ’Point’+str((self.x, self.y))
__repr__ = __str__
>>> p =
Polygon([Point(0,0),Point(1,0),Point(1,1),Point(0,1)])
>>> p.perimeter()
4.0
class Polygon:
def __init__(self, p):
self.points = p
def perimeter(self):
p = self.points
n = len(p)
per = 0
for i in range(n):
per +=
p[i].distanceTo(p[(i+1)%n])
return per
2.2 Rectangles
2.3 Edges
class Edge:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def length(self):
return self.p1.distanceTo(self.p2)
def determinant(self):
return self.p1.x * self.p2.y - self.p1.y *
self.p2.x
def __str__(self):
return ’Edge’+str((self.p1, self.p2))
__repr__ = __str__
>>> p =
Polygon([Point(0,0),Point(2,0),Point(2,1),Point(0,1)])
>>> p.sumForEdges(lambda e: 1)
or def area(self):
return 0.5*self.sumForEdges(lambda e:
e.determinant())
or
def aux(e): return e.determinant()
def area(self): return
0.5*self.sumForEdges(aux)
Assume that the system starts at rest and that the input x[n] is the
unit-step signal u[n].
Find y[4] and enter its value in the box below.
y[4] = 21
0 1 0 0
1
1 1 1 0
2
2 1 2 1
5
3 1 5 2
10
4 1 10 5
3.2 Block Diagrams
21
p0 = 2 A = 2/3 p1 = −1 B = 1/
3
4 Robot SM
def inpClassifier(inp):
if inp.sonars[3] < 0.5: return ’frontWall’
elif inp.sonars[7] < 0.5: return
’rightWall’
else: return ’noWall’
r = Robot(’NotFollowing’,
[Rule(’NotFollowing’, ’noWall’,
’NotFollowing’, 0.1, 0.0),
Rule(’NotFollowing’, ’frontWall’,
’Following’, 0.0, 0.0),
Rule(’Following’, ’frontWall’, ’Following’,
0.0, 0.1)], inpClassifier)
Assume it is an errorif a combination of state and input type
occurs that is not covered in the rules. In that case, the state will
not change and the output will be an action with zero velocities.
4.1 Simulate
We’d like the robot to start at the origin (i.e., x=0, y=0,
theta=0), then move forward (along x axis) until it gets within
0.5 meters of a wall, then move backward until it is close to the
origin (within 0.02 m), and then repeat this cycle of forward
and backward moves indefinitely. Assume that the robot never
moves more than 0.01 m per time step.
Write an input classifier for this behavior.
def chargeAndRetreatClassifier(inp):
if inp.sonars[3] < 0.5: return
’frontWall’ el
if inp.odometry.x < 0.02: return
’origin’
else: return ’between’
4.3 Robot instance
Complete the definition of the Robot class below; use the match
procedure you defined above. Recall that, at each step, the
output must be an instance of the io.Action class; to initialize an
instance of io.Action, you must provide a forward and a
rotational velocity. If a combination of state and input type
occurs that is not covered in the rules, remain in the same state
and output an action with zero velocity.
class Robot(sm.SM):
def __init__(self, start, rules,
inpClassifier):
self.startState = start self.rules =
rules
self.inpClassifier = inpClassifier
def getNextValues(self, state, inp):
inpType = self.inpClassifier(inp)
r = match(self.rules, inpType, state)
if r:
return (r.nextState, io.Action(r.outFvel,
r.outRvel)
else:
return (state, io.Action(0, 0))
5 Feedback
5.1 Generalization
Enter FA or FB or FC or FD or None: FC
Y = FE = F(X + Y)
Y − FY = FX
H − HF = F
H = F + HF
Procedures in sf
sf.Cascade(sf1, sf2)
sf.FeedbackSubtract(sf1, sf2)
sf.FeedbackAdd(sf1, sf2)
sf.Gain(c)
def inpClassifier(inp):
if inp.sonars[3] < 0.5: return
’frontWall’
elif inp.sonars[7] < 0.5: return
’rightWall’
else: return ’noWall’
Next, we create a class for defining “rules.” Each rule specifies
the next state, forward velocity and rotational velocity that should
result when the robot is in a specified current state and receives a
particular type of input. Here is the definition of the class Rule:
class Rule:
def __init__(self, currentState, inpType, nextState,
outFvel, outRvel):
self.currentState = currentState
self.inpType = inpType
self.nextState = nextState
self.outFvel = outFvel
self.outRvel = outRvel
Finally, we will specify the new state machine class called Robot,
which takes a start state, a list of Rule instances, and a procedure
to classify input types. The following statement creates an
instance of the Robot class that we can use to control the robot in
a Soar brain.
r = Robot(’NotFollowing’,
[Rule(’NotFollowing’, ’noWall’, ’NotFollowing’,
0.1, 0.0),
Rule(’NotFollowing’, ’frontWall’, ’Following’, 0.0,
0.0),
Rule(’Following’, ’frontWall’, ’Following’, 0.0,
0.1)],
inpClassifier)
Assume it is an errorif a combination of state and input type
occurs that is not covered in the rules. In that case, the state will
not change and the output will be an action with zero velocities.