function [gamma,C] = wsens(P,W,tol,s)
%[gamma,C] = msenswsens(P,W,tol,s)
%
% Weighted sensitivity H-inf solver for the problem
%
%  min || W/(1+P*C) ||
%   C
%
% Inputs:
%  P   - transfer function of the plant
%  W   - weighting function (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 < 2,
   error('usage:  [C,gamma] = wsens(P,W)');
 elseif nargin == 2
   tol = 0.000001;
 elseif nargin == 3
   s = 'ric';
 end;
 G = minreal(ss([W -W*P; eye(size(P.num,1)) -P]));
 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)));
