warning off
A=[0 1 ; 0 0 ];
R1=eye(2);
C1=[1 0];
C2=[0 1];
B=[0;1]
wv = logspace(-2,4);

for R2=1e-8;%[1e-2 1e-4 1e-6 1e-8]
  L = (lqr(A',C1',R1,R2))'
  reg1 = tf(ss(A-L*C1,L,C1,0))
  reg2 = tf(ss(A-L*C1,L,C2,0))
  reg3 = tf(ss(A-L*C1,B,C1,0))
  reg4 = tf(ss(A-L*C1,B,C2,0))
  figure(1)
  subplot(221)
  loglog(wv,squeeze(freqresp(reg3,wv)),'b','Linewidth',2)
  axis([1e-2 1e4 1e-4 1e1])
  title('u -> x1hat','Fontsize',16,'Fontweight','bold')
%  set(gca,'Fontsize',16,'Fontweight','bold')
  hold on; grid on
  subplot(223)
  loglog(wv,squeeze(freqresp(reg4,wv)),'r','Linewidth',2)
  title('u -> x2-hat','Fontsize',16,'Fontweight','bold')
  hold on;grid on
%  set(gca,'Fontsize',16,'Fontweight','bold')
  axis([1e-2 1e4 1e-4 1e1])

  subplot(222)
  loglog(wv,squeeze(freqresp(reg1,wv)),'b','Linewidth',2)
  axis([1e-2 1e4 1e-4 1e1])
  title('y-> x1-hat','Fontsize',16,'Fontweight','bold')
%  set(gca,'Fontsize',16,'Fontweight','bold')
  hold on; grid on
  subplot(224)
  loglog(wv,squeeze(freqresp(reg2,wv)),'r','Linewidth',2)
  title('y->x2hat','Fontsize',16,'Fontweight','bold')
  hold on;grid on
%  set(gca,'Fontsize',16,'Fontweight','bold')
  axis([1e-2 1e4 1e-4 1e1])
end

%print -depsc reducedobsfig.eps

figure(2)
Q1 = diag([1 1]);
for Q2 = [1e-2 1e-4 1e-6 1e-8]
  K = lqr(A,B,Q1,Q2)
  reg = ss(A-B*K-L*C1,L,K,0*K*L);
  zpk(reg)
  loglog(wv,abs(squeeze(freqresp(reg,wv))),'b','Linewidth',2)
  hold on
end
title('Q1=diag([1 1])','Fontsize',16,'Fontweight','bold')
set(gca,'Fontsize',16,'Fontweight','bold')
axis([1e-2 1e4 1e-2 1e4])



figure(3)
Q1 = diag([1 0]);
for Q2 = [1e-2 1e-4 1e-6 1e-8]
  K = lqr(A,B,Q1,Q2)
  reg = ss(A-B*K-L*C1,L,K,0*K*L);
  zpk(reg)
  loglog(wv,abs(squeeze(freqresp(reg,wv))),'b','Linewidth',2)
  hold on
end
title('Q1=diag([1 0])','Fontsize',16,'Fontweight','bold')
set(gca,'Fontsize',16,'Fontweight','bold')
axis([1e-2 1e4 1e-2 1e4])
