%  Example 6.3.  Linear quadratic regulator gain computation.
clear
%  Define the plant model.
A=1;
B=1;
%  Define the cost function parameters.
Q=3;
R=1;
%  Compute the steady-state LQR gain.  Note:  This is computed to verify
%  the analytic results subsequently.
Kss=lqr(A,B,Q,R)
%  Generate the time-varying gain using the given expression.
t=0:.01:10;
K=(27*exp(2*(10-t))+5*exp(-2*(10-t)))./(9*exp(2*(10-t))-5*exp(-2*(10-t)));
%  Plot the results.
set(0,'DefaultAxesFontName','times')
set(0,'DefaultAxesFontSize',16)
set(0,'DefaultTextFontName','times')
figure(1)
clf
plot(t,K)
xlabel('Time (sec)')
ylabel('Optimal feedback gain')
grid
ylim([0 10])
