```python
import sympy as sy
import numpy as np
sy.init_printing(use_latex=True)
```

## Problem 1
### (a)
Choosing sampling time and calculating the discrete-time poles. The time constant of the first-order response is $T=120$s. Rise time is $T_r = 2.2T = 264$s. Rule-of-thumb is 4-10 sampling periods per rise time.


```python
T = 120
Tr = 2.2*120
print [Tr/10, Tr/4]
```

    [26.4, 66.0]



```python
h = 40.0
pc = -1.0/120
pd = np.exp(pc*h)
print pd
```

    0.716531310574


### (b)
Backward difference approximation of given continuous-time controller


```python
s,z = sy.symbols('s, z')
h = sy.symbols('h', positive=True)
F = (16*s+1)/(100*s+1)
H = sy.simplify(F.subs(s, (z-1)/(z*h)))
print H
```

    (h*z + 16*z - 16)/(h*z + 100*z - 100)



```python

```


```python
p1,p2,p3,p4 = sy.symbols('p1, p2, p3, p4') 

sy.expand((z-0.7+sy.I*0.1)*(z-0.7-sy.I*0.1))

```

## Problem 2
Calculate the pulse transfer function from the disturbance d to the output y



```python
B = 0.6*z + 0.5
A = z*(z**2 - 1.9*z + 0.9)
S, R = sy.symbols('S, R')

H_dy = (B/A) / (1 + (B/A)*(S/R))
sy.simplify(H_dy)
```


```python
H_dys = sy.simplify(H_dy)
s=sy.latex(H_dys)
print s
```

    \frac{R \left(0.6 z + 0.5\right)}{R z \left(z^{2} - 1.9 z + 0.9\right) + S \left(0.6 z + 0.5\right)}


## Problem 3
Find the diagonal form of the system in problem 2, but without the time delay


```python
H = sy.simplify(z*B/A)
sy.apart(H)
```

Set up the state-space model. Make sure it is correct.


```python
Phi = sy.Matrix([[0.9, 0], [0, 1]])
Gamma = sy.Matrix([[1],[1]])
Cm = sy.Matrix([[-10.4, 11.0]])

Htest = Cm*(z*sy.eye(2)-Phi).inv()*Gamma
Htest
                
```


```python
H
```

### (c) Finding the feedback gain


```python
l1, l2 = sy.symbols('l1, l2')
L = sy.Matrix([[l1, l2]])
sy.factor((z*sy.eye(2) - (Phi - Gamma*L)).det(), z)
```


```python
sy.simplify(sy.expand((z-0.6+sy.I*0.3)*(z-0.6-sy.I*0.3)))
```

# Check with matlab


```python
%load_ext pymatbridge
```

    Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge-4d29d37c-5632-4ec9-913b-670fa8e16c20
    Send 'exit' command to kill the server
    .....MATLAB started and connected!


    /home/kjartan/anaconda2/lib/python2.7/site-packages/IPython/nbformat.py:13: ShimWarning: The `IPython.nbformat` package has been deprecated. You should import from nbformat instead.
      "You should import from nbformat instead.", ShimWarning)



```python
%%matlab  --size 800,400

%Mean arteriar pressure, automatic anasthesia model
% Plant
G = tf([1], [120 1 0]);

a = 1/100;
b = 1/160;
F0 = zpk([-b], [-a], a/b);

K = 1e-2;
F = K*F0;

Gc = feedback(G*F, 1);
step(Gc, 1000)
```


```python
%%matlab --size 800,400 -o L,y,t
h = 40.0;
Phi = [0.9 0; 0 1]; 
Gamma = [1;1]; 
C = [-10.4  11.0]; 
D = 0;
sys = ss(Phi, Gamma, C, D, h);
L = place(Phi, Gamma, [0.6+i*0.3 0.6-i*0.3]);
sys_cl = ss(Phi-Gamma*L, Gamma, C, D, h);
[y, t] = step(sys_cl);
```


```python
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure()
plt.plot(t, y)
```


```python

```


```python

```

6cbc50fd86d76e4bb487aafa08bee0733b6df059
ipynb
Jupyter Notebook
misc-notebooks/.ipynb_checkpoints/Final-exam-spring16-checkpoint.ipynb
kjartan-at-tec/mr2007-computerized-control
16e35f5007f53870eaf344eea1165507505ab4aa
MIT
2020-11-07T05:20:37.000Z
2020-12-22T09:46:13.000Z
misc-notebooks/MR2007-final-exam-spring-2016.ipynb
alfkjartan/control-computarizado
5b9a3ae67602d131adf0b306f3ffce7a4914bf8e
MIT
2020-06-12T20:44:41.000Z
2020-06-12T20:49:00.000Z
misc-notebooks/.ipynb_checkpoints/Final-exam-spring17-checkpoint.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