```python
import numpy as np
import sympy as sy
import control.matlab as cm
import matplotlib.pyplot as plt
%matplotlib inline
```


```python
s,z,h = sy.symbols('s, z, h')
```


```python
Y = (s+1)/(s*s*(s+3))
sy.apart(Y)
```




    -2/(9*(s + 3)) + 2/(9*s) + 1/(3*s**2)




```python
H = sy.Integer(2)/sy.Integer(9) + h/(3*(z-1)) - 2*(z-1)/(9*(z-sy.exp(-3*h)))
print H
```

    h/(3*z - 3) - (2*z - 2)/(9*z - 9*exp(-3*h)) + 2/9



```python
H = sy.together(H)
print H
```

    (3*h*(z*exp(3*h) - 1) - 2*(z - 1)**2*exp(3*h) + 2*(z - 1)*(z*exp(3*h) - 1))/(9*(z - 1)*(z*exp(3*h) - 1))



```python
(B,A) = sy.fraction(H)
print B
```

    3*h*(z*exp(3*h) - 1) - 2*(z - 1)**2*exp(3*h) + 2*(z - 1)*(z*exp(3*h) - 1)



```python
sy.simplify(B - sy.exp(3*h)*(2*(z-1)*(z-sy.exp(-3*h)) + 3*h*(z-sy.exp(-3*h)) - 2*(z-1)**2))
```




    0




```python
np.log(2)/3
```




    0.23104906018664842




```python
Hc = sy.simplify(H/(H+1))
print Hc
```

    (3*h*(z*exp(3*h) - 1) - 2*(z - 1)**2*exp(3*h) + 2*(z - 1)*(z*exp(3*h) - 1))/(3*h*(z*exp(3*h) - 1) - 2*(z - 1)**2*exp(3*h) + 11*(z - 1)*(z*exp(3*h) - 1))



```python
Hc1 = Hc.subs(h, sy.log(2)/3)
(Bc,Ac) = sy.fraction(Hc1)
print sy.collect(sy.expand(Ac), z)
```

    18*z**2 + z*(-25 + 2*log(2)) - log(2) + 7



```python
sy.simplify(sy.expand( (11*z-11 +sy.log(2))*(z-0.5)-2*(z-1)**2) )
```




    9*z**2 - 12.5*z + z*log(2) - 0.5*log(2) + 3.5




```python
sy.simplify(sy.expand( A + B)).subs(h, sy.log(2)/3)
```




    18*z**2 - 25*z + 2*z*log(2) - log(2) + 7




```python
12.5-np.log(2)
```




    11.806852819440055




```python
3.5-np.log(2)/2
```




    3.1534264097200273




```python
Bcp = sy.Poly(Bc, z)
Acp = sy.Poly(Ac,z)
a = []
for el in Acp.coeffs():
    a.append(float(sy.N(el)))
print a
b = []
for el in Bcp.coeffs():
    b.append(float(sy.N(el)))
print b

sys = cm.tf(b,a,np.log(2)/3.0)
print sys
Td = np.log(2)/3*np.arange(80)
(yd,td) = cm.step(sys, Td)
plt.plot(td,yd[0], '*')
plt.xlim([0,20])
plt.savefig("stepresponse.pdf")
```


```python
Bp = sy.Poly(B.subs(h, sy.log(2)/3))
Ap = sy.Poly(A.subs(h, sy.log(2)/3))
a = []
for el in Ap.coeffs():
    a.append(float(sy.N(el)))
print a
b = []
for el in Bp.coeffs():
    b.append(float(sy.N(el)))
print b

sys_o = cm.tf(b,a,np.log(2)/3.0)
cm.nyquist(sys_o, np.linspace(0,2*np.pi, 800))
plt.ylim(-2,2)
plt.xlim(-1,1)
```


```python
H1 = cm.tf([1],[1, -2.2, 1.4, -0.2], 0.5)
H2 = cm.tf([1, 0],[1, -1.2, .2], 0.5)
H3 = cm.tf([4, 1],[1, 1, .16], 1)
H4 = cm.tf([0.25], [1, -1.5, 0.5])
```


```python
cm.nyquist(H4, np.linspace(0,2*np.pi, 800))
plt.ylim(-5,5)
plt.xlim(-2,2)
```


```python
import khcontrol as khc

z = np.exp(1j*np.linspace(0,np.pi, 800))
zz = H4(z)
(zr,zi) = khc.nyq(H4, 800)

#nq = []
#for w in np.linspace(0,2*np.pi, 200):
#    #nq.append(H4(np.exp(1j*w)))
#    nq.append(H4(1j*w))
plt.figure()
plt.plot(np.real(zz), np.imag(zz))
plt.ylim(-5,2)
plt.xlim(-2,1)
xy = np.column_stack((np.real(zz),np.imag(zz)))
np.savetxt('nyquist150907.out', xy)

```


```python
np.zeros(3)
```




    array([ 0.,  0.,  0.])




```python
1600.0/3
```




    533.3333333333334




```python

```

271a2339405ffb0cff6636bcea5e4272cd6f5d77
ipynb
Jupyter Notebook
misc-notebooks/MR2007-partial-exam-1-dummy.ipynb
kjartan-at-tec/mr2007-computerized-control
16e35f5007f53870eaf344eea1165507505ab4aa
MIT
2020-11-07T05:20:37.000Z
2020-12-22T09:46:13.000Z
misc-notebooks/MR2007-partial-exam-1-dummy.ipynb
alfkjartan/control-computarizado
5b9a3ae67602d131adf0b306f3ffce7a4914bf8e
MIT
2020-06-12T20:44:41.000Z
2020-06-12T20:49:00.000Z
misc-notebooks/MR2007-partial-exam-1-dummy.ipynb
kjartan-at-tec/mr2007-computerized-control
16e35f5007f53870eaf344eea1165507505ab4aa
MIT
2021-03-14T03:55:27.000Z
2021-03-14T03:55:27.000Z
Qwen/Qwen-72B
1. YES
2. YES