function [C1,C2,C3,C4,C5,C6] = bo_check(order,PLT,CON,FIL)
% [C1,C2,C3,C4,C5,C6] = bo_check(order,PLT,CON,FIL)
% Accuracy check for fulfillment of basic Magnitude Optimum equations
% order:    number of controller degrees of freedom
%           == number of relevant outputs Cx to be defined, x={1,2,3,4,5,6}
% PLT:     LTI plant transfer function,
%              inclusive of dead time approximation, if any dead time
% CON:     LTI controller transfer function,
%              inclusive of additional controller filter polynomial, if any
% FIL:     LTI pre-filter transfer function, if any
% C1,C2,C3,C4,C5,C6: fulfillment of optimization equations 1 till 6,
%                    values are set to empty if not applicable

if (mod(order,1)~=0)||order<1||order>6 error('Invalid input "order"'), end
Gbw=tf(1,1);              % BackWard transfer function assumed to be unity
if ~exist('FIL','var') FIL=Gbw; end
G0=series(CON,PLT);
Gg=feedback(G0,Gbw);
Gg2=series(FIL,Gg);
[bep,apo]=tfdata(Gg2,'v');
B=flip(bep); A=flip(apo);
ML=max([length(B)  length(A)]);
if ML<13 B(14)=0; A(14)=0; end % not adequate: B(ML+1)=0, A(ML+1)=0,
                               % so prepared till condition 6 ONLY!
%condition 0
C0=B(1)-A(1); if C0~=0 warning('Basic condition B0=A0=1 violated'), 
             disp('C0=A0-B0 should be identically with zero, but is not:'),
             C0,
              end

warning OFF BACKTRACE
% condition 1 (P-, I-, D-) --- must be calculated in any case
if ML<3 Warning('At least one parameter zero for condition 1'), end
C1= ( B(2)^2-2*B(3)*B(1) )...
   -( A(2)^2-2*A(3)*A(1) );

if order==6          % condition 6 (PID4)
if ML<13 warning('At least one parameter zero for condition 6'), end
C6=  ( B(7)^2-2*B(6)*B(8)+2*B(5)*B(9)-2*B(4)*B(10)+2*B(3)*B(11)...
                                     -2*B(2)*B(12)+2*B(1)*B(13) )...
   - ( A(7)^2-2*A(6)*A(8)+2*A(5)*A(9)-2*A(4)*A(10)+2*A(3)*A(11)...
                                     -2*A(2)*A(12)+2*A(1)*A(13) );
else C6=[]; end

if order>4           % condition 5 (PID3)
if ML<11 warning('At least one parameter zero for condition 5'), end
C5=  ( B(6)^2-2*B(5)*B(7)+2*B(4)*B(8)-2*B(3)*B(9)+2*B(2)*B(10)...
                                     -2*B(1)*B(11) )...
   - ( A(6)^2-2*A(5)*A(7)+2*A(4)*A(8)-2*A(3)*A(9)+2*A(2)*A(10)...
                                     -2*A(1)*A(11) );
else C5=[]; end

if order>3           % condition 4 (PID2)
if ML<9  warning('At least one parameter zero for condition 4'), end
C4=  ( B(5)^2-2*B(4)*B(6)+2*B(3)*B(7)-2*B(2)*B(8)+2*B(1)*B(9) )...
   - ( A(5)^2-2*A(4)*A(6)+2*A(3)*A(7)-2*A(2)*A(8)+2*A(1)*A(9) );
else C4=[]; end

if order>2            % condition 3 (PID)
if ML<7  warning('At least one parameter zero for condition 3'),end
C3=  ( B(4)^2-2*B(3)*B(5)+2*B(2)*B(6)-2*B(1)*B(7) )...
   - ( A(4)^2-2*A(3)*A(5)+2*A(2)*A(6)-2*A(1)*A(7) );
else C3=[]; end

if order>1             % condition 2 (PI-, PD-)
if ML<5  warning('At least one parameter zero for condition 2'),end
C2=  ( B(3)^2-2*B(2)*B(4)+2*B(1)*B(5) )...
   - ( A(3)^2-2*A(2)*A(4)+2*A(1)*A(5) );
else C2=[]; end
warning ON BACKTRACE
end

