%  **   EXAMPLE 5.2   SERIES OF FIRST ORDER WITH DEAD TIME **
%                           from  MARLIN 1994
%  COPYRIGHT  1994  by McMASTER UNIVERSITY
%  WRITTEN BY:  T. MARLIN
%  FILENAME:  EXAM5_2.M
%  LAST UPDATED: November 1994
%  VERSION 2.0
%  DESCRIPTION OF FILE:
%  THE TRANSIENT RESPONSE OF FOUR SERIES SYSTEMS, EACH WITH
%  FIRST ORDER WITH DEAD TIME DYNAMICS IS DETERMINED
%  FOR A STEP INPUT.  
%  ***********************************************************

clear;                      % CLEAR WORKSPACE
clf;                        % CLEAR GRAPH
axis ('auto')               % AUTO SCALE
clc;                        % CLEARS SCREEN


%  ***********************************************************
%  SIMULATION DATA AND INITIAL CLCULATIONS
%  ***********************************************************
% 
tstart=0.    ;                % time simulation begins
tend=20.    ;                 % time simulation ends
delt = 0.1 ;                  % evaluate every delt
%
n=round(tend/delt)  ;
%
initstep = 22   ;             % (time step introduced)/delt + 2 because
                              % must be greater than dead_time/delt+1

for cnt = initstep:n          % INITIALIZE RESULT AND SIZE MATRIX
   y(1,cnt) = 1.0 ;
end
T = zeros(1,n);
case = 1 ;                    % CAN BE EXPANDED FOR ADDITIONAL
                              % CASES WITH ADDITIONAL LOOP

%  ************************************************************
%    DATA FOR EACH SYSTEM
%  ************************************************************

nstage = 4;                 % NUMBER OF SERIES SYSTEMS
for nn=2:nstage+1           % VALUES BEFORE STEP ARE ZERO
    for cnt =1:n
        y(nn,cnt) = 0.0 ;
    end
end
                             % first sub=case, second=stage
                             % MODEL PARAMETERS
     tau(1,1) =  2 ;
     tau(1,2) =  2 ;
     tau(1,3) =  0  ;
     tau(1,4) =  0  ;
     theta(1,1) = 0 ;
     theta(1,2) = 2  ;
     theta(1,3) = 1   ;
     theta(1,4) = 1   ;
     gain(1,1)  = 1.0 ;
     gain(1,2)  = 1.0 ;
     gain(1,3)  = 1.0 ;
     gain(1,4)  = 1.0 ;

%  **********************************************************
%  PRECALCULATE SOME CONSTANTS USED IN DYNAMIC MODELS
%
%  integration using equation (F-7) for Appendix F
%  **********************************************************
case=1;                    %
   for stage =1:nstage

     if tau(case,stage) > .01
        alpha(case,stage)  = exp (-delt/(tau (case,stage)))  ;
     else
        alpha(case,stage)  = 0.0;
     end
     beta (case,stage)  = gain(case,stage)*(1 - alpha(case,stage ) )  ;
     gamma(case,stage) = round(theta(case,stage)/delt)  ;

   end
end

%  *************************************************************
%   SIMULATION IS HERE
%  *************************************************************

case=1 ;
for cnt=initstep:n

      for nn=2:nstage+1
         if alpha (case,nn-1) < .01
              y(nn,cnt) = y(nn-1,cnt-gamma(case,nn-1));

          else
              y(nn,cnt) = alpha(case,nn-1)*y(nn,cnt-1) + ...
                       beta(case,nn-1)*y(nn-1,cnt-gamma(case,nn-1)-1); 
         end  % if

       end   % for

    T(1,cnt) = cnt*delt- 2*delt  ;   % 2 is correction for initstep
                                     %which is 20 correctly, but was
                                     %22 to prevent accessing array < 0

end   % cnt
%
% *****************************************************************
%  PLOT RESULTS
%  ****************************************************************
%  EACH OUTPUT IS OFFSET BY A BIAS SO THAT EACH CAN BE SEEN CLEARLY
%  ****************************************************************


plot (T(1,:),y(2,:), T(1,:), y(3,:)+1,T(1,:),y(4,:)+2, ...
     T(1,:),y(5,:)+3)
xlabel ('time')
ylabel ('output responses, 1 to 4 from bottom')
title('Example 5.2, Case 2,  Response of fourth order system to step at t=2')
figure (1)
pause

close all
%  *******************   END   ************************************