% ***************************************************************************
% COPYRIGHT BY T.E. MARLIN 1995
% FILNAME : DMC_DIFF.M
% Version 2.0
% LAST UPDATED : January 1995
% DISCRIPTION OF FILE :
% THIS M-FILE IS ACCESSED FROM THE DMC DESIGN MENU AND CALCULATES
% COEFFICIENTS FOR THE DMC MODEL IN DIFFERENCE FORM - USED TO GENERATE MATRIX
% ***************************************************************************

% ***************************************************************************
% ********  EVALUATION OF CONSTANTS FOR THE DIFFERENCE EQUATIONS  ***********
% *****************  SECOND-ORDER LEAD-LAG PLUS DEADTIME  *******************
% *****************************  PROCESS  ***********************************
% ***************************************************************************

     gamma_m = round(DT_m./delt);

	tau12_m=tau1_m-tau2_m;
	tauA1_m=tauA_m-tau1_m;
	tau2A_m=tau2_m-tauA_m;
	exp1_m=zeros(2,2);
	exp2_m=zeros(2,2);
	a1_m=zeros(2,2);
	a2_m=zeros(2,2);
	b1_m=zeros(2,2);
	b2_m=zeros(2,2);

for x=1:2
	for y=1:2
	if Kp_m(x,y) ~= 0         
	 if (tau12_m(x,y)==0) & (tau1_m(x,y)==0)
	    tau1_m(x,y)=1e-3;     
	 elseif (tau12_m(x,y)==0) & (tau1_m(x,y)~=0)
	    tau1_m(x,y)=1.01*tau1_m(x,y);
	 end

	tau12_m(x,y)=tau1_m(x,y)-tau2_m(x,y);
	tauA1_m(x,y)=tauA_m(x,y)-tau1_m(x,y);
	tau2A_m(x,y)=tau2_m(x,y)-tauA_m(x,y);

	 if tau1_m(x,y)>0
	    exp1_m(x,y)=exp(-delt/tau1_m(x,y));
	 end                                     % IF
	
	 if tau2_m(x,y)>0
	    exp2_m(x,y)=exp(-delt/tau2_m(x,y));
	 end                                     % IF
	    a1_m(x,y)=-exp1_m(x,y)-exp2_m(x,y);
	    a2_m(x,y)=exp1_m(x,y)*exp2_m(x,y);
	    b1_m(x,y)=Kp_m(x,y)*(1+tauA1_m(x,y)/tau12_m(x,y)*exp1_m(x,y)+...
	    tau2A_m(x,y)/tau12_m(x,y)*exp2_m(x,y));
	    b2_m(x,y)=Kp_m(x,y)*(a2_m(x,y)+tauA1_m(x,y)/tau12_m(x,y)*...
	    exp2_m(x,y)+tau2A_m(x,y)/tau12_m(x,y)*exp1_m(x,y));
	end     % Kp CHECK
  end           % y LOOP
end             % x LOOP

% ***************************************************************************
