% Problem 5.11
A=[ 0.915  0.051  0.038 0.015  0.038;
   -0.030  0.889 -0.001 0.046  0.111;
   -0.006  0.648  0.247 0.014  0.048;
   -0.715 -0.022 -0.021 0.240 -0.024;
   -0.148 -0.003 -0.004 0.090  0.026];

B=[0.010 0.122 0.036 0.562 0.115]';

C=[1 1 0 0 0;
   0 0 1 1 1];

D=[0; 0];

% 5.11(a) - Controllability and observability

% Controllability
disp('Controllability matrix:')
Co=ctrb(A,B)
r_Co=rank(Co)
if r_Co < size(A,1)
   disp('System is not controllable')
else
   disp('System is controllable')
end

% Observability
disp(' ')
disp('Observability matrix:')
Ob=obsv(A,C)
r_Ob=rank(Ob)
if r_Ob < size(A,1)
   disp('System is not observable')
else
   disp('System is observable')
end

% Problem 5.11(b)
[num,den]=ss2tf(A,B,C,D,1);
%printsys(num,den,'s')

[Nm1,Dm1]=minreal(num(1,:),den);
printsys(Nm1,Dm1)
[Nm2,Dm2]=minreal(num(2,:),den);
printsys(Nm2,Dm2)
if length(Nm1) < (size(A,1)+1) | length(Dm2) < (size(A,1)+1)
  disp('System is not controllable or observable!')
end