% ***************************************************************************
% COPYRIGHT BY T.E. MARLIN 1995
% FILNAME : DMC_COEF.M
% Version 2.0
% LAST UPDATED : January 1995
% DISCRIPTION OF FILE :
% THIS M-FILE IS ACCESSED FROM THE MAIN SIMULATION AND CALCULATES
% COEFFICIENTS FOR THE DMC MODEL MATRIX, KNOWN AS "A" IN CUTLERS NOTATION
% ***************************************************************************

%  ******************************************************
%  initialize some variables
%  ******************************************************

  cv_m  = zeros(nout)    ;
  cv1_m = zeros (nout)   ;
  cv2_m = zeros(nout)    ;
  MV1_m = zeros (nin)   ;
  MV2_m = zeros (nin)   ;
% ***************************************************************************
% ********          EVALUATION OF DMC COEFFICIENTS                ***********
% *****************  BASED ON DIFFERENT EQUATION PARAMETERS *****************
% ****************** EVALUATED FROM DMC MODEL      **************************
% ***************************************************************************

%   FIRST, EVALUATE THE "A" MATRIX OF STEP WEIGHTS
%          VECTOR OF STEP WEIGHTS

%    initialize inputs for dead time calculations    
    
    SStime_m = max (max (DT_m))  ;    %  all inputs are 0.0 in past
                                       %  (1 is for the hold in discrete model)
    SSstep_m = SStime_m/delt +1 ;          %  and 1.0 in current and future
    
    for iin=1:nin
      for istep=1:SSstep_m-1
           INSTEP(iin,istep) = 0.0 ;
      end

      for istep = SSstep_m : SSstep_m + NN
          INSTEP(iin,istep)  = 1.0 ;
      end
    end           % form the input data

%  calculate the step responses from the difference equation

OUTPUT11 = zeros (1,NN) ; OUTPUT12 = zeros (1,NN);
OUTPUT21 = zeros (1,NN) ; OUTPUT22 = zeros (1,NN);

for ncount=  SSstep_m+1 : SSstep_m + NN             % time loop  

% UPDATES THE EFFECTS OF THE MANIPULATED VARIABLES ON CONTROLLED VARIABLES

MV1_m=[INSTEP(1, ncount-1-gamma_m(1,1)) INSTEP(2, ncount-1-gamma_m(1,2)); ...
     INSTEP(1, ncount-1-gamma_m(2,1)) INSTEP(2, ncount-1-gamma_m(2,2))];

% for the first iteration, the argument of one INSTEP(arg) is zero
% always by the calculation of SSstep_m -- prevent this illegal argument

if ncount == SSstep_m+1
    MV2_m = [ 0 0 ; 0 0] ;
else
    MV2_m=[INSTEP(1,ncount-2-gamma_m(1,1)) INSTEP(2,ncount-2-gamma_m(1,2)); ...
     INSTEP(1,ncount-2-gamma_m(2,1)) INSTEP(2,ncount-2-gamma_m(2,2))];
    xxx=min(ncount*ones(2,2)-gamma_m)-2;
       if xxx <= 0
    disp('argument error in program DMC_COEF.M, do not continue')
    pause;
end

end  %ncount

cv_m=-a1_m.*cv1_m-a2_m.*cv2_m+b1_m.*MV1_m+b2_m.*MV2_m;

cv2_m=cv1_m;        % holds and updates the n-2 value
cv1_m=cv_m;         % holds and updates the n-1 value

OUTPUT11(ncount-SSstep_m) =  (cv_m(1,1)) ;
OUTPUT12(ncount-SSstep_m) =  (cv_m(1,2)) ;
OUTPUT21(ncount-SSstep_m) =  (cv_m(2,1)) ;
OUTPUT22(ncount-SSstep_m) =  (cv_m(2,2)) ;
     
end % time LOOP

%  ***************************************************************
%  ******   now, for the "A" matrix   ****************************

A11 = zeros(NN,MM);
A12 = zeros(NN,MM);
A21 = zeros(NN,MM);
A22 = zeros(NN,MM);

for iNN=1:NN
    for iMM=1:MM
       if (iNN-iMM+1) >= 1
         A11(iNN,iMM) = OUTPUT11(iNN-iMM+1);
         A12(iNN,iMM) = OUTPUT12(iNN-iMM+1);
         A21(iNN,iMM) = OUTPUT21(iNN-iMM+1);
         A22(iNN,iMM) = OUTPUT22(iNN-iMM+1);
       end  % if
    end
end


