function [gamma,C] = msens(P,Ws,Wk,tol,s)
%[gamma,C] = msens(P,Ws,Wk,tol,s)
%
% Mixed sensitivity H-inf solver for the problem
%
%      ||  Ws/(1+P*C)  ||
%  min ||              ||
%   C  || Wk*C/(1+P*C) ||
%
% Inputs:
%  P   - transfer function of the plant
%  Ws  - weighting function for 1/(1+PC) (bi-proper and bi-stable)
%  Wk  - weighting function for C/(1+PC) (bi-proper and bi-stable)
%  tol - tolerance (optional, default 10^(-6))
%  s   - solver (optional, either 'ric' or 'lmi', default is 'ric')
%
% Outputs:
%  gamma - minimal achevable norm
%  C     - the optimal controller
%
 if nargin < 3,
   error('usage:  [C,gamma] = wsens(P,Ws,Wt)');
 elseif nargin == 3
   tol = 1E-6;
   s = 'ric';
 elseif nargin == 4
   s = 'ric';
 end;
 G = ss([Ws;0;1])*ss([1,-P])+[0,0; 0,ss(Wk); 0,0];
 Pg = ltisys(G.a,G.b,G.c,G.d);
 if s == 'lmi'
   [gamma,Ca] = hinflmi(Pg,size(P.num),0,tol);
 else
   [gamma,Ca] = hinfric(Pg,size(P.num),0,1000,tol);
 end;
 [Ac,Bc,Cc,Dc] = ltiss(Ca);
 C = tf(minreal(ss(Ac,Bc,Cc,Dc)));
