%%% LQG design without integral action

A = [0 0; 1 -1];
B = [20; 0];
N = [20; 0];
C = [0 1];
D = 0;
P = ss(A,B,C,D);

Q1 = [0 0; 0 1];
Q2 = 1;
R1 = 1;
R2 = 1;

L = lqr(A,B,Q1,Q2);       % Design state feedback

Pest = ss(A,[B N],C,0);   % Plant model for Kalman design
Kest = kalman(Pest,R1,R2) % Design Kalman filter

ctrl1 = -lqgreg(Kest,L);

Mt = norm(feedback(P*ctrl1,1),inf)
Ms = norm(feedback(1,P*ctrl1),inf)

%%% LQG design with explicit integrator

Q1e = blkdiag(Q1,0.01);   % Extended cost function
Le = lqi(P,Q1e,Q2)        % Extended state feedback gain

ctrl2 = lqgtrack(Kest,Le,'1dof') % Combine Kalman filter,
                                 % integrator and extended
                                 % state feedback
                                 
Mt = norm(feedback(P*ctrl2,1),inf)
Ms = norm(feedback(1,P*ctrl2),inf)
                       
%%% Compare the two controllers

bode(ctrl1,ctrl2)
