% let us key in the example's Sylvester matrix

D0 = [0 0; 0 0]; D1 = [0 0; 0 1]; D2 =[1 0; 0 0];
N0 = [1 1; 0 1]; N1 = [0 0; 0 0]; N2= [0 0; 0 0];
OO = zeros(2);
Sm = [D0 D1 D2 OO; N0 N1 N2 OO; OO D0 D1 D2; OO N0 N1 N2];
svd(Sm);

% let us compute the row indexes of tht N columns
% and we see that the last row is linearly dependent using the qr
% decomposition

[Q,R] = qr(Sm');

% let us key in the desired closed-loop polynomials in diagonal form
F0 = [15 0; 0 5]; F1 = [17 0; 0 2]; F2 = [7 0; 0 1]; F3 = [1 0; 0 0];

Fb = [F0 F1 F2 F3];

% now, since the last column becomes linearly dependent, we reduce the 
% Sylvester matrix so that the solution becomes unique
Smr = Sm(1:7,1:7);
% this means that the highest power in the B matrix (B1' last column) is not required because
% the last row of the sylvester matrix (corresponding to the N matrix) has been shown to become linearly
% dependent on its previous rows. Therefore the last column of B1 is not
% required and set to zero. It will be later on reset to zero to find the
% correct B1.

% compute the solution using the inverse of the Sylvester matrix. It is now
% a square full rank matrix, thus invertible.
res1=Fb(1,1:7)*inv(Smr);
res2=Fb(2,1:7)*inv(Smr);

% finally add back the zeros so that the dimensions of the result is
% compatible with the original dimensions. This simply means adding a zero
% where B1 was set to zero
AB = [res1,0;res2,0]

% collect the result
A0 = AB(:,1:2); B0 = AB(:,3:4); A1 = AB(:,5:6); B1 = AB(:,7:8);


% changing topic, let us compute a pure state-feedback on the same system
% but starting from a realization of the transfer function matrix.
% The structure is quite simple so that a controller canonical form is 
% easily found. Otherwise proceed with the Structure theorem and the
% S-matrix (see previous lectures).

Am = [0 1 0; 0 0 0; 0 0 0]; Bm = [0 0; 1 0; 0 1]; Cm = [1 0 1; 0 0 1];

% one can check with mathematica that this gives the right transfer matrix
% Now choose an arbitrary F having the desired eigenvalues. Since this is
% quite different from pole assignement as such (because there is no
% observer involved) we choose three stable eigenvalues -2,-3,-4 and use a
% simple diagonal form
F = diag([-2,-3,-4]);

% pick an arbitrary Kb but so that the system is observable. Intuition
% tells that by mixing as many diagonal elements should lead to an
% observable system. Therefore for each output, we combine a set of states
% with identical weights 1,1,1
Kb = [1 1 1; 0 1 1];

% let us check observability
[ql,rl]=qr([Kb; Kb*F]);
T = lyap(Am,-F, -Bm*Kb);

% and compute our state feedback controller
K = Kb*inv(T);

% finally let us check that the desired eigenvalues are set at the right
% place
eig(Am - Bm*Kb)

% next week we will look how to put together the MIMO and the Lyap case,
% using the notion of an observer...
% finally, we will go back to the cyclic vectors and show a particularity
% of the MIMO setting when the structure of the A matrix is not compatible
% with the desired eigenvalues multiplicity.





