function [erg,z1,z2]=zust_au2(T,Tei,TM,TF,TL,SV)
%ZUST_AU2 state control structure 1; 3 Koeff.; automated; here plant only
% [erg(,z1,z2]=zust_au2(T,Tei,TM,TF,TL(,SV)) - application of the Discontinuous
% Magnitude Optimum(BOD) to calculate coefficients of a discontinuous state control
% structure - example by Brandenburg: at 35(1987)7, pp. 275/83;
% quod vide: Tei=4; TM=30; TF=5; und TL=30;60;120  *** automated calculation ***
%      T:	   sampling time of the discontinuous control in ms
%      Tei:	   first order time delay (model of closed current control loop) in ms
%      TM:	   motor time constant (integrator) in ms
%      TF:	   spring time constant (integrator) in ms
%      TL:	   load time constant  (integrator) in ms
%      (,SV):  optionally: switch for operation control, default SV==[1 0 0 0]
%                element1: choice active column of BM, example={1,2,3}, default=1;
%                element2: display "removed states": 0=no/1=yes; default=0;
%                element3: display of partial results: 0=no/1=yes; default=0;
%                element4: display & diagnostics of fsolve: 0=no/1=yes; default=0;
%      erg:	   results as vector [Kdelta Km Kn]
%      z1,z2:  please see MATLAB function fsolve (out2,out3)

% KEY WORDS: Discontinuous Magnitude Optimum, BOD, state control structure,
%            elastic drive train, optimization, undelayed input signals,
%            delayed input signals

%  ***** preconditions: existence of functions zust_aug2.m, minreal2.m ****
%  Copyright (c) 1997-2023. All Rights Reserved. 
%                     Dr. Gert-Helge Geitner, TU Dresden,
%                     https://wwwpub.zih.tu-dresden.de/~geitner/ae_1_0e.htm
%  Reference regarding Discontinuous Magnitude Optimum: Geitner, G.-H.:
%                     Entwurf digitaler Regler fuer elektrische Antriebe.
%                     publisher: VDE-Verlag GmbH Berlin und Offenbach 1995
%                     ISBN 3-8007-1847-2

narginchk(5,6)  % obsolete versions: error(nargchk(5,6,nargin))
if   nargin==5 SV=[1 0 0 0]; %default - control vector SV
else [x,y]=size(SV);
     if     x~=1|y~=4 error('SV - incorrect dimension');     
  elseif ~(SV(1)==1|SV(1)==2|SV(1)==3) error('SV - incorrect calculation column!');
  elseif ~(SV(2)==0|SV(2)==1) error('SV - incorrect switch "removed states"!');
  elseif ~(SV(3)==0|SV(3)==1)
                     error('SV - incorrect switch "display of partial results"!');
  elseif ~(SV(4)==0|SV(4)==1) error('SV - incorrect switch "choice of options"!');
     end;
end
BRF=SV(1); %choice of a calculation column of matrix BM
SDF=SV(2); %switch dispflag = display "removed states"
SAK=SV(3); %switch operation control = display of partial results
SOP=SV(4); %switch options = fsolve using display & diagnostics

S=[1 1 1];	   % S=[Kdelta Km Kn] **** switch for weighting factors
RFV=[1 2 3];   % RFV=[1 2 3]	  **** sequence vector with [Kdelta Km Kn]
   if sum(RFV)~=6 error('Reihenfolgevektor falsch'), end
BM=[2 1 3; 1 1 0; 0 1 0; 0 0 0]; %COLUMN - number of calculations per fsolve call
% Definition of numerators and denominators of all blocks (transfer functions)
  n001=1;                          d001=[Tei 1];
  n002=1;                          d002=[TM 0];
  n003=1;                          d003=1;
  n004=1;                          d004=[TF 0];
  n005=1;                          d005=[TL 0];
% declaration of the number of blocks (transfer functions) and definition of an 
% unconnected diagonal state model of the system using the above defined
% numerator and denominator polynomials:
%nblocks=5;
%blkbuild;      % because of global variable access for script files only!!!
% define an unconnected diagonal system
[a,b,c,d]=tf2ss(n001,d001);  s0n=abs('n000'); s0d=abs('d000');
for t=2:5  HUS=fix(t/100); ZEV=fix(t/10); sk=[0 HUS ZEV-HUS*10 t-ZEV*10];
           s1=char(s0n+sk); s2=char(s0d+sk);
           [at,bt,ct,dt]=tf2ss(eval(s1),eval(s2));
           [a,b,c,d]=append(a,b,c,d,at,bt,ct,dt);
end;       %  !!!Thus algorithm prepared for three ASCII positions (0...9)!!!
% definition of the connections between the blocks (transfer functions):
Q=[ 1  0  0
    2  1 -4
    3  2 -5
    4  3  0
    5  4  0];    in =[1];  ou=[3 4 2 5];
[ac,bc,cc,dc]=connect(a,b,c,d,Q,in,ou); % connected system
[ap,bp,c,d]=minreal2(ac,bc,cc,dc,sqrt(eps),SDF); % state elimination
[a,b]=c2d(ap,bp,T);                     % z-transform
b(1,4)=0; d(4,4)=0;                     % rebuilding of a symmetrical
                                        % structure=number of blocks
                                        % a,b,c,d: invariable part
PA=sum(S);		% number of parameters
BA=sum(BM);		% initial values per variant (column)
if PA~=BA(BRF) error('Incorrect calculation sequence'), end
[i,j]=min(BM);		% i: Minima (0!) per column; j: associated row number
SZ=j(BRF)-1;		% loop counter for fsolve calling
enr=1;			% result number
erg=[];			% initial value
for iS=1:SZ
 if iS~=1&SAK==1 erg, end % operation control  ****
 TAZ=BM(iS,BRF);          % transient number
%Before changing fsolve and options: AW=ones(1,TAZ); -->After changing:
 AW=randn(1,TAZ);         % variable number of initial values
%Before changing fsolve and options:
 %q=fsolve('zust_aug1',AW',[0 0 0 0 1],[],a,b,c,d,erg,BRF,RFV,TAZ,iS);
%After changing:
 if SOP
 options=optimset('Display','iter','LargeScale','on','Diagnostics','on',...
                      'TolFun',10^-30,'TolX',10^-20);
 else
 options=optimset('Display','off','LargeScale','on','TolFun',10^-30,'TolX',10^-20);
 end
 [q,z1,z2]=fsolve('zust_aug2',AW',options,a,b,c,d,erg,BRF,RFV,TAZ,iS,SDF);
 
 for j=1:TAZ
     erg(enr)=q(j);
     enr=enr+1;
 end
end
