A = [0 1 0; 0 0 1; 0 2 -1], B = [0 1; 1 1;0 0], C = [1 0 0], D = [0 0];
rank(ctrb(A,B));
rank(obsv(A,C))
P = [-2; -1-j; -1+j];
K = place(A,B,P);
Pobsv = [-9; -10; -11];
L = place(A',C',Pobsv)';
sysOL = ss(A,B,C,D)
 
a = 
       x1  x2  x3
   x1   0   1   0
   x2   0   0   1
   x3   0   2  -1
 
b = 
       u1  u2
   x1   0   1
   x2   1   1
   x3   0   0
 
c = 
       x1  x2  x3
   y1   1   0   0
 
d = 
       u1  u2
   y1   0   0
 
Continuous-time model.

sys_comp = ss([A-B*K B*K; zeros(size(A)) A-L*C],[B; zeros(size(B))],[C zeros(size(C))],D)
 
a = 
               x1          x2          x3          x4          x5          x6
   x1          -2           0   5.551e-16           2           1  -5.551e-16
   x2           0          -1        -0.5           0           1         1.5
   x3           0           2          -1           0           0           0
   x4           0           0           0         -29           1           0
   x5           0           0           0        -272           0           1
   x6           0           0           0        -776           2          -1
 
b = 
       u1  u2
   x1   0   1
   x2   1   1
   x3   0   0
   x4   0   0
   x5   0   0
   x6   0   0
 
c = 
       x1  x2  x3  x4  x5  x6
   y1   1   0   0   0   0   0
 
d = 
       u1  u2
   y1   0   0
 
Continuous-time model.

sys_ctrl = ss(A-B*K,B,C,D)
 
a = 
              x1         x2         x3
   x1         -2          0  5.551e-16
   x2          0         -1       -0.5
   x3          0          2         -1
 
b = 
       u1  u2
   x1   0   1
   x2   1   1
   x3   0   0
 
c = 
       x1  x2  x3
   y1   1   0   0
 
d = 
       u1  u2
   y1   0   0
 
Continuous-time model.

t = 0:.01:5;

[y_ctrl,t_ctrl,x_ctrl]=initial(sys_ctrl,[1 1 1]',t);
[y_ol,t_ol,x_ol]=initial(sysOL,[1 1 1]',t);
[y_comp,t_comp,x_comp]=initial(sys_comp,[1 1 1 1 1 1]',t);
figure(1); plot(t_ol,y_ol,'b');
eig(A)

figure(2)
plot(t_comp,y_comp,'r', t_ctrl,y_ctrl, 'g--');
figure(2)
plot(t_comp,y_comp,'r', t_ctrl,y_ctrl, 'k--');
figure(2)
legend('Compensated system, u = -K \hat x', 'Controlled system, u = -Kx')
{Warning: Unable to interpret TeX string "\hat x"} 
figure(2)

Pobsv = [-10; -10+10*j; -10-10*j];
L = place(A',C',Pobsv)';


sys_comp = ss([A-B*K B*K; zeros(size(A)) A-L*C],[B; zeros(size(B))],[C zeros(size(C))],D)
 
a = 
               x1          x2          x3          x4          x5          x6
   x1          -2           0   5.551e-16           2           1  -5.551e-16
   x2           0          -1        -0.5           0           1         1.5
   x3           0           2          -1           0           0           0
   x4           0           0           0         -29           1           0
   x5           0           0           0        -373           0           1
   x6           0           0           0       -1685           2          -1
 
b = 
       u1  u2
   x1   0   1
   x2   1   1
   x3   0   0
   x4   0   0
   x5   0   0
   x6   0   0
 
c = 
       x1  x2  x3  x4  x5  x6
   y1   1   0   0   0   0   0
 
d = 
       u1  u2
   y1   0   0
 
Continuous-time model.

[y_comp,t_comp,x_comp]=initial(sys_comp,[1 1 1 1 1 1]',t);
figure(2);
hold on; plot(t_comp, y_comp,'b-.');
legend('Compensated system, u = -K \hat x', 'Controlled system, u = -Kx','New compensated sys')
{Warning: Unable to interpret TeX string "\hat x"} 
