function [af,bf,cf,df] = lqg(Z1,Z2,Z3,Z4,Z5,Z6)
% [SS_F] = LQG(SS_,W,V) or
% [AF,BF,CF,DF] = LQG(A,B,C,D,W,V) computes Linear-Quadratic-Gaussian
%    optimal controller via the "seperation principle", such that the 
%    cost function           
%                     T
%    J    = lim E{ int   |x' u'| W |x| dt} , W = |Q  Nc|, is minimized 
%     LQG   T-->inf   0            |u|           |Nc' R| 
%    and subject to the plant  
%                       dx/dt = Ax + Bu + xi
%                           y = Cx + Du + th
%
%    where the white noises {xi} and {th} have the cross-correlation
%    function with intensity V, i.e.
%
%           E{|xi| |xi th|'} = V delta(t-tau), V =  |Xi  Nf|.
%             |th|                                  |Nf' Th|
%
%    The LQG optimal controller F(s) is returned in SS_F or (af,bf,cf,df).
%    The standard state-space can be recovered by "branch".

% R. Y. Chiang & M. G. Safonov 8/86
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.
% -------------------------------------------------------------------------

inargs = '(A,B,C,D,W,V)';
eval(mkargs(inargs,nargin,'ss'))
%
[ns,ns] = size(A);
[nout,nin]= size(D);
%
% ------- Regular LQG problem:
%
Kf = lqrc(A',C',V); Kf = Kf';
Kc = lqrc(A,B,W);
%
% ------- Final Controller:
%
af = A - B*Kc - Kf*C + Kf*D*Kc;
bf = Kf; 
cf = Kc; 
df = zeros(nin,nout);
%
if xsflag
   af = mksys(af,bf,cf,df);
end
%
% ------- End of LQG.M -- RYC/MGS %
