function K=kmatrix(Z,S)
%KMATRIX  weighting factors for the optimization equation system of (BOD)
%         K=kmatrix(Z,S) whereat S=number of columns; Z=number of rows
%  BOD: Discontinuous Magnitude Optrimum - compare chapter 3 in:
%                     "Entwurf digitaler Regler für elektrische Antriebe",
%                     by G.-H. Geitner, 
%                     publisher: VDE-Verlag GmbH Berlin und Offenbach 1995
%                     (equation 3.11 and table 3.1 on page 69); 

% KEY WORDS: Discontinuous Magnitude Optrimum, weighting factors

% Copyright (c) 1997-2023. All Rights Reserved. 
%                    Dr. Gert-Helge Geitner, TU Dresden,
%                    https://wwwpub.zih.tu-dresden.de/~geitner/ae_1_0e.htm

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
