function OptPreFi = bo_prefi_op03(PLTu,C_ref,PLTd,C_sys,PFi_sys,OrdBal,Tol)
%function OptPreFi = bo_prefi_op03(PLTu,C_ref,PLTd,C_sys,PFi_sys(,OrdBal,Tol))
% Magnitude Optimum: optional prefilter optimization as a second step
%                    in case that overshoot should be decreased more
%    SAME as bo_prefi_opt, BUT two plant model inputs >>>PLTu and PLTd<<<
% PLEASE NOTE: this function is based on LTI representation of all 
%  transfer functions - e.g. in contrast to bo_u_gen / bo_d_gen / dr_d_wedz
% PLTu      LTI plant:      reference system
% C_ref     LTI controller: reference system desired, e.g. bo_u_gen(02)
% PLTd      LTI plant:      system to be optimized
% C_sys     LTI controller: system to be optimized, e.g. bo_d_gen(02)
% PFi_sys   LTI pre-filter: system to be optimized, e.g. bo_d_gen(02)
% OrdBal     --> optional: desired prefilter order,      default: 4
% Tol        --> optional: tolerance of function balred, default: sqrt(eps)
% OptPreFi  LTI pre-filter optimized in order to replace PFi_sys

if     nargin>7  error('Too much input arguments');
elseif nargin<5  error('Not enough input arguments'); 
else   if nargin<7 Tol=sqrt(eps); end % default of minreal
       if nargin<6 OrdBal=4;  end     % default here, no order input
       if ~issiso(PLTu)|~issiso(C_ref)|~issiso(PLTd)...
                       |~issiso(C_sys)|~issiso(PFi_sys)
       error('One or more input arguments 1 till 5 are not LTI type');end
end
ToSwi=get(PLTd,'ioDelay');                 % system features dead time ?
if ToSwi~=0%MATLAB cannot handle delay annulment in tf division - see below
    Tt_num=[-(1/120)*ToSwi^3  0.1*ToSwi^2  -0.5*ToSwi  1]; %Pade 3. order
    Tt_den=[+(1/120)*ToSwi^3  0.1*ToSwi^2   0.5*ToSwi  1];
    set(PLTd,'ioDelay',0);              % reset dead time in LTI plant model
    PLTd=series(PLTd,tf(Tt_num,Tt_den)); % use Pade approximation instead
end

TSw=get(PLTu,'ioDelay');                 % system features dead time ?
if TSw~=0%MATLAB cannot handle delay annulment in tf division - see below
    Tt_nu=[-(1/120)*TSw^3  0.1*TSw^2  -0.5*TSw  1]; %Pade 3. order
    Tt_de=[+(1/120)*TSw^3  0.1*TSw^2   0.5*TSw  1];
    set(PLTu,'ioDelay',0);              % reset dead time in LTI plant model
    PLTu=series(PLTu,tf(Tt_nu,Tt_de)); % use Pade approximation instead
end

ClL_u =feedback(series(C_ref,PLTu),1);
ClL_um=minreal(ClL_u,Tol);                % TOL as second argument possible
ClL_d =series(PFi_sys,feedback(series(C_sys,PLTd),1));
ClL_dm=minreal(ClL_d,Tol);                % TOL as second argument possible

PFi_x=ClL_um/ClL_dm;                     % tf devision excludes dead time !
PFi_xm=minreal(PFi_x,Tol);               % TOL as second argument possible
FiEquOrd=series(PFi_xm,PFi_sys);         % both filter here
FiEquOrdm=minreal(FiEquOrd,Tol),         % TOL as second argument possible
% for order difference 1:
%FiBO=series(FiEquOrdm,tf(1,[0.1  1])),   %
OptPreFi=balred(FiEquOrdm,OrdBal); % action impossible if ord_num > ord_den

