%  *** EXAMPLE 3.1 STIRRED TANK MIXER **
%  ***      from  MARLIN 1994   ********
%  COPYRIGHT 1994 by McMASTER UNIVERSITY
%  WRITTEN BY : T. MARLN
%  FILENAME: EXAM3_1.M
%  LAST UPDATED: JANUARY 14, 1994
%  VERSION 1.0
%  DECRIPTION OF FILE:
%  this MATLAB M-file calculates the transient response 
%  for the stirred tnk mixing dynamics, example 3_1 
%  ******************************************************************

   clear ;                   % clear work space
   clf   ;                   % clear previous graphs 
   axis ('auto');            % auto scale
   clc;                      %  clear screen

% ********************************************************************
%    PROCESS DATA
% ********************************************************************

v = 2.1;              % volume of tank
f = 0.085;            % flow rate
cainit = 0.925;       % initial tank concentration

deltaca0=0.925  ;     % step introduced at time=10
percent = 90  ;       % percent of change required for processing to resume
yprime = cainit + (percent/100) * deltaca0;  % switch concentration
% *********************************************************************
%   SIMULATION DATA
% *********************************************************************

%  for the analytical solution
tstart=0.    ;
tend=120.    ;
tdist=10     ;                       % time to introduce the step
delt=0.5   ;                        % time between evaluations
time=0.     ;
n = round ((tend-tstart)/delt);      %  number of time steps

% ******************************************************************
%   parameters for the linearized dynamic model

    tau = v/f  ;  % time constant
    kp  = 1.0  ;  % gain (=1.0 because mixing)
% ******************************************************************
%  initial conditions
ca = cainit   ;  % yn is the linearized analytical approximation
ca0 = cainit ;  % initial manipulated variable in deviation variables

%  size some matrices which improves speed
YL =zeros(1,n);
YSW = zeros(1,n);
MV =zeros(1,n);
T  =zeros(1,n);
% *****************************************************************
%   parameters for the non-linear model
% *****************************************************************
%      NONE
% *****************************
% SIMULATION
% ************************************************************************

for cnt=1:1:n
%
    time = time + delt;
%  ********************************************************************
    ca0 = cainit;
                     % introduce the step at proper time
    if time >= tdist
          ca0 = ca0+deltaca0; %  this in deviation variables for the linearized
    end
%  ********************************************************************
%  linear analytical process  model    

    ca = cainit;
    if time >= tdist
        ca = cainit + kp * deltaca0*( 1 - exp (-(time-tdist)/tau) );
    end
% 
%  *******************************************************************
%  store results for plotting

    YL(1,cnt) = ca     ;             %  this plots to compare with tn
    YSW(1,cnt) = yprime      ;
    MV(1,cnt)= ca0     ;             % this is the same as mvn+deltafc
    T(1,cnt) = time   ;
%  **********************************************************************

end    % for cnt loop

% *********************************************************************
%    plot results

subplot (211), plot (T(1,:),YL(1,:), T(1,:), YSW(1,:),'--')
axis([0 tend 0.75 2 ])
xlabel ('time')
ylabel ('tank concentration')
title (' Example 3.1, solid is the CA, dashed is 90% value')
%
%
%axis ([0 tend 0 2 ])
subplot (212), stairs (T(1,:),MV(1,:))
xlabel('time')
ylabel('inlet concentration')

figure (1) 
pause

close all

% *********************** END *******************************************