See http://ctms.engin.umich.edu/CTMS/index.php?example=MotorSpeed§ion=SimulinkModeling
In [2]:
import numpy as np
import sympy as sy
import control.matlab as cm
import matplotlib.pyplot as plt
%matplotlib inline
In [48]:
# Set parameters of the model
I = 0.01 # kg m^2
m = 0.01 # V/rad/s
k = 0.01 # Nm/Amp
R = 1.0 # Ohm
L = 0.5 # H
a1 = k/I
a2 = m/L
a3 = R/L
b = 1.0/L
In [50]:
s,t = sy.symbols('s,t')
In [51]:
A = sy.Matrix([[0,1,0],[0,0,a1], [0,-a2,-a3]])
In [52]:
sIA = s*sy.eye(3) - A
sIA
Out[52]:
In [78]:
sIA.inv()
Out[78]:
In [79]:
B = sy.Matrix([[0],[0],[b]])
C = sy.Matrix([[1,0,0]])
In [80]:
sIAinv = sIA.inv()
G = C*sIAinv*B
print G
In [93]:
# Hard code G because of problems with sy.factor()
G = 2/(s*(s+1.99)*(s+0.01))
print G
In [105]:
Gs = cm.tf([2],[1, 2, 0.02, 0])
print Gs
h = 0.01
Gd = cm.c2d(Gs, h)
cm.nyquist(Gs)
plt.ylim(-6,6)
plt.xlim(-10,1)
Out[105]:
In [95]:
# Try to sample symbolically
#Y = sy.apart(G/s)
#print Y
#y = sy.inverse_laplace_transform(Y,s,t)
#print y
In [90]:
den = (s+1.99)*(s+0.01)*s
print den
den2=sy.expand(den)
print den2
print sy.simplify(den-den2)
den3 = sy.factor(den2)
print den3
print sy.simplify(den-den3)
In [66]:
y = sy.inverse_laplace_transform(Y,s,t)
print y
In [56]:
sIA.det()
Out[56]:
In [57]:
sIA.adjugate()
Out[57]:
In [58]:
Id=sIA*sIA.adjugate()/sIA.det()
In [59]:
for r in Id.tolist():
for c in r:
print sy.simplify(c)
In [82]:
np.sqrt(0.96)
Out[82]:
In [68]:
0.99*0.99
Out[68]:
In [69]:
sy.expand((s+1.99)*(s+0.01))
Out[69]:
In [3]:
# Simpler model
H2 = cm.tf([0.19, 0.15], [1, -1.5, 0.5], np.log(2))
H2
Out[3]:
In [5]:
H2(np.exp(1j*0.001))
Out[5]:
In [6]:
-1.32*0.25
Out[6]:
In [7]:
0.34-0.19*0.5
Out[7]:
In [21]:
w=0.001
cw = np.cos(w)
#sw = np.sin(w)
#cw = 1.0
sw = w
H21w = (0.19*(cw + 1j*sw) + 0.15) / ( (cw+1j*sw-1) * (cw+1j*sw - 0.5) )
print H21w
H22w = (0.19+0.15 + 1j*0.19*w)/( (-w**2/2.0 + 1j*w)*(0.5+1j*w))
H23w = (0.19+0.15 + 1j*0.19*w)/(1j*w*(0.5+1j*w))
print H22w
print ( (w+1j*0.5)*(0.34 + 1j*0.19*w) )/(-w*(w**2 + 0.25))
In [22]:
10**(0.25)
Out[22]:
In [ ]: