% programa 5.3, W.L.Luyben, pag. 120
% "three-isothermal cstr (openloop)" 
% modif. E.Quintero.M 18nov'98
%------------------------------------------------------
% disturbance is a step change in feed concentration
% at time equal zero from 0.8 to 1.8 kg-moles of A/
% cubic meter
% time is in minutes
clear
format short e
% initial conditions
time=0; ca1=0.4; ca2=0.2; 
ca3=0.1; cao=1.8; xx=[];
% parameter values
tau=2; k=0.5; 
delta=0.1; tprint=0;
while(time<=3)
   % evaluate derivatives
   ca1dot=(cao-ca1)/tau-k*ca1;
   ca2dot=(ca1-ca2)/tau-k*ca2;
   ca3dot=(ca2-ca3)/tau-k*ca3;
   if(time>=tprint)
      xx=[xx;time,ca1,ca2,ca3];
      tprint=tprint+0.1;
   end
   ca1=ca1+ca1dot*delta;
   ca2=ca2+ca2dot*delta;
   ca3=ca3+ca3dot*delta;
   time=time+delta;
end
xx
subplot(2,1,1),plot(xx(:,1),xx(:,2)),ylabel('Ca1'),grid
subplot(2,1,2),plot(xx(:,1),xx(:,4)),ylabel('Ca3'),grid
