% Exercise1 
A=0.9; B=0.5; D=1;

M=6; H=zeros(M,1);
for i=1:M
    H(i)=D*A^(i-1)*B
end

% Create Hankel matrices for L=2 using all M=6 impulse responses

H22 = [H(2) H(3) H(4) H(5)
     H(3) H(4) H(5) H(6)]
H12=[H(1) H(2) H(3) H(4)
     H(2) H(3) H(4) H(5)]

% H12*O2*C4 and H22=O2*A*C4

% Create O2 and C4. J=4
[U,S,V]=svd(H12); diag(S)
n=1;
U1=U(:,1:n); S1=S(1:n,1:n); V1=V(:,1:n);
OL=U1; CJ=S1*V1';

%Estimated model matrices from Realization theory
a=pinv(OL'*OL)*OL'*H22*CJ'*pinv(CJ*CJ')
d=OL(1,:); 
b=CJ(:,1);