function [ZVe,NVe,Q,IN,OU,MT,NV,BRF]=struc041(SOR,OU,P1,P2)
%        [ZVe,NVe,Q,IN,OU,MT,NV,BRF]=struc041(SOR,OU,P1,P2)
%STRUC041 S(3 PT1): Par=[TN1 TN2 TN3];
%                   Reg=below - controller gain factoring out for delayed inputs
%                       not necessary here, because of one computation loop only,
%                       contrary to struc000.m
%                   P2=[undelayed(0)/delayed(1)  PI-type(0)/PID(1)];
%         Single control loop including computation switchover
%	            P2(1): switchover undelayed(0) / delayed(1) inputs
%               P2(2): switchover PI-type(0) / PID-type controller (1)
%         structure and simulation see SIST041;
%         plant: three first-order time-delay elements,

%AUXILIARY FILE for symbolical definition - no stand-alone calling !!!
%	  SOR:   switch - to shift between plant or control structure
%	  OU:    vector using continuous output numbers for discretized plant state
%            description
%     P1,P2: posibility of any parameter transfer via bod_es.m

% KEY WORDS: Digital Magnitude Optimum, symbolical plant and control structure
%            definition, application basis for: bod_es. / bod_esg.m / bod_esp.m

% Copyright (c) 2011-2023 Dr. Gert-Helge Geitner, TU Dresden,
%                     https://wwwpub.zih.tu-dresden.de/~geitner/ae_1_0e.htm
% Digital Magnitude Optimum reference: Geitner, G.-H.:
%                    Entwurf digitaler Regler für elektrische Antriebe.
%                    VDE-Verlag GmbH Berlin und Offenbach 1996
%                    ISBN 3-8007-1847-2

if SOR==0  %plant structure definition

% ****** beginning ******
    sz1='1;';  sn1='conv(conv([TN1 1],[TN2 1]),[TN3 1]);';
    Q=[1 0];   IN=[1];   OU=[1];
    MT=[0 1];
    NV=['TN1; TN2; TN3; '];
% ****** ending ******

s=0; zae=0; ZVe=[]; NVe=[]; BRF=0;
while s==0 zae=zae+1;  eval(['ZVe=[ZVe sz' num2str(zae) '];'],'s=1;');
                       eval(['NVe=[NVe sn' num2str(zae) '];'],'s=1;'); end

else  %control structure definition

%******* beginning ******
rn2='[1 -1];';             % definitions for all variants
BMA=[1]; BRF=1;
if P2(1)==0                % def. for undelayed input signal
   QQ=[1  2
       2 -1];
   BME=[2];
   if P2(2)==0	           % def. for undelayed input and PI-type controller
       rz2='[VR VRd1];';
       BM=[2
           0];
       NV=['VR; VRd1; '];
   else		               % def. for undelayed input and PID--type controller
       rz2='[VR VRd1 VRd2];';
       BM=[3
           0];
       NV=['VR; VRd1; VRd2; '];
   end
else                       % def. for delayed input signal
   QQ=[1  2 0
       2 -1 3
       3  0 0];
   BME=[3];
   if P2(2)==0	           % def. for delayed input and PI-type controller
       rz2='[VR VRd1];';   % no factoring out necessary: one loop only
       rz3='(VR+VRd1)/VR;'; rn3='[1 VRd1/VR];'; %no initial value problem
       BM=[2
           0];
       NV=['VR; VRd1; '];  %contrary to struc000.m
   else		                  % def. for delayed input and PID-type controller
       rz2='[VR VRd1 VRd2];'; % no factoring out necessary: one loop only
       rz3='(VR+VRd1+VRd2)/VR;'; rn3='[1 VRd1/VR VRd2/VR];';%no init problem
       BM=[3
           0];
       NV=['VR; VRd1; VRd2; ']; %contrary to struc000.m
   end
end
%****** ending ******

s=0; zae=length(OU); ZVe=[]; NVe=[];
while s==0 zae=zae+1;  eval(['ZVe=[ZVe rz' num2str(zae) '];'],'s=1;');
                       eval(['NVe=[NVe rn' num2str(zae) '];'],'s=1;'); end
Q=QQ;IN=BME;OU=BMA;MT=BM;
end
