function [q,varargout]=bod_esg(p,a,b,c,d,EZW,QQ,BMEA,ZVe,NVe,NV,TOL,SV)
%BOD_ESG  auxiliary file for bod_es.m - optimize structure in z-domain
%         [q,varargout]    = bod_esg(p,a,b,c,d,EZW,QQ,BMEA,ZVe,NVe,NV,TOL,SV) 
%         obsolete call: q = bod_esg(p,a,b,c,d,EZW,QQ,BMEA,ZVe,NVe,NV)
%         for definition of plant and control structure see file:
%         strucxxx.m (example: struc000.m)
%	  Here: computation of BOD basic optimization equation system

%  KEY WORDS: Digital Amount Optimum, optimization in z-domain
%             auxiliary file for bod_es

%  Copyright (c) 2011 Dr. Gert-Helge Geitner; TU Dresden, Fak. ET,
%                     Elektrotechnisches Institut (ETI); Mommsenstr. 13;
%                     D-01062 Dresden, Germany;
%                     Tel./Fax ++49-351-463-32917/33655
%                     Gert-Helge.Geitner@tu-dresden.de

% perparation of optional Jacobian computation
 a_o=a; b_o=b; c_o=c; d_o=d;
% unpack of transfered packed fsolve arguments
 BRF=EZW(1,1); MAU=EZW(1,2); TAZ=EZW(1,3); iS=EZW(1,4);
 knr=EZW(1,5);                     % number of already known results
 for i=1:knr KZV(i)=EZW(2,i); end; % KZV equal to erg
 [i,j]=size(BMEA);j=j/2;BME(:,1:j)=BMEA(:,1:j);BMA(:,1:j)=BMEA(:,j+1:2*j);

q=zeros(TAZ,1);         % preparation of fsolve q(ii) computation
KZV(MAU)=0;	  % "remaining" coefficients firstly fill up by zeros
for i=1:TAZ
    knr=knr+1; KZV(knr)=p(i); % store intermediate fsolve results
end

% unpack name vector of controler parameters and assign numerical values:
 i=0; n0=0; k=length(NV);
 while i<k n0=n0+1; i=i+1; j=1; vtext=0;   %increase "i" by "2" because of: "; "
       while NV(i)~=';' vtext(j)=NV(i); i=i+1; j=j+1; end
       eval([vtext '=KZV(' num2str(n0) ');']); i=i+1; end
if n0~=MAU
  error('Inconsistent vectors NV of controller parameter names and matrix BM!'),
end

% unpack numerator vector of particular blocks each:
 i=0; n1=0; k=length(ZVe);
 while i<k n1=n1+1; i=i+1; j=1; vtext=0;
       while ZVe(i)~=';'  vtext(j)=ZVe(i); i=i+1;  j=j+1; end
       eval(['rz' num2str(n1) '=' vtext ';']); end

% unpack denominator vector of particular blocks each:
 i=0; n=0; k=length(NVe);
 while i<k n=n+1; i=i+1; j=1; vtext=0;
       while NVe(i)~=';'  vtext(j)=NVe(i); i=i+1;  j=j+1; end
       eval(['rn' num2str(n) '=' vtext ';']); end
if n~=n1 error('Unequal number of controller numerator/denominator blocks!'),end

% generate unconnected diagonal system:
for i=1:n
    RZ=eval(['rz' num2str(i)]); RN=eval(['rn' num2str(i)]); 
    zl=length(RZ); nl=length(RN); if nl<zl RN(zl)=0; end    % because of tf2ss
    [at,bt,ct,dt]=tf2ss(RZ,RN);
    [a,b,c,d]=append(a,b,c,d,at,bt,ct,dt); end

% compute connected system:
  [ac,bc,cc,dc]=connect(a,b,c,d,QQ,BME(iS,BRF),BMA(iS,BRF));
  [acm,bcm,ccm,dcm]=minreal(ac,bc,cc,dc,TOL);  %===>unconditional necessary!!!
  [a,b]=ss2tf(acm,bcm,ccm,dcm);

% check of computability:
  oz=length(a); on=length(b);   % order of numerator and denominator
  o=max(oz,on);     % protection against denominator polynomial approximations
  if oz>on b(oz)=0; elseif on>oz a(on)=0; end
  Z=TAZ;			% number of rows for Matrix K
  S=o-1;			% number of columns for Matrix K
  if TAZ>=o disp('Warning: may be order < number of unknown!')
     if TAZ>o a(TAZ+1)=0; b(TAZ+1)=0; o=TAZ+1; end
  end

%Computation of matrix K: columns=S; rows=Z
for i=1:Z
 for j=1:S
  if     i==1         K(i,j)=j^2;
  else   if j==1      K(i,j)=0;
	 elseif  j==2 K(i,j)=K(i,j-1)+K(i-1,j-1);
	 elseif  j==3 K(i,j)=K(i,j-1)+K(i,j-2)+K(i-1,j-1)+K(i-1,j-2);
	 else         K(i,j)=K(i,j-1)+K(i,j-2)-K(i,j-3)+K(i-1,j-1)+K(i-1,j-2);
end; end; end; end

%Digital Amount Optimum (BOD):
for ii=1:TAZ
    for i=ii:(o-1), x=0;
        for j=1:(o-i)
            x = x + a(j) * a(j+i) - b(j) * b(j+i);  end
        q(ii) = q(ii) + x * K(ii,i);  end; end
if SV(1)==0
varargout=[];
else  %compute Jacobian (== gradients)
J=bod_esp(p,a_o,b_o,c_o,d_o,EZW,QQ,BMEA,ZVe,NVe,NV,TOL);
varargout={J}; %cell array definition necessary
end
