MATLAB: Please good people out there, i need help with how to vary a parameter in this code. In the case i want to vary TC from 180 to 220. How do i factor that into this code? Thank you very much for your help!!

constant varyparameter varytemperature vary

FUNCTION FILE (First File)
function [rate_out] = BATCOM(t,x)
TC = 190 ; %Reactor temperature, C
R = 8.31434; %Gas constant, kJ/kmol K
%Arrhenius constant, 1/s
A1= 5.78052E+10 ;
A2 = 3.92317E+12;
A3 = 1.64254E+4 ;
A4 = 6.264E+8 ;
%Activation energy, kJ/kmol
Ea1 = 124670 ;
Ea2 = 150386 ;
Ea3 = 77954 ;
Ea4 = 111528 ;
%The concentration of the species are first assigned in the vector format
CA=x(1);
CB=x(2);
CC=x(3);
CD=x(4);
%Reactor temperature, Kelvin
TK=R*(TC+273);
%Reaction rate constants, 1/s (k=Ae^(Ea/T);NB..R has been factored at TK)
k1= A1*exp(-Ea1/TK);
k2=A2*exp(-Ea2/TK) ;
k3=A3*exp(-Ea3/TK) ;
k4=A4*exp(-Ea4/TK) ;
k5=2.16667E-04 ;
%Reaction rates, kmoles/m3 s}
dCAdt =-(k1+k2)*CA;
dCBdt=k1*CA-k3*CB;
dCCdt=k2*CA-k4*CC;
dCDdt=k3*CB-k5*CD;
%Mass balances
rA=dCAdt;
rB=dCBdt;
rC=dCCdt;
rD=dCDdt;
% The output of the function reaction rates are saved in vector format
rate_out=[rA; rB; rC; rD ];
end
RUN FILE(2nd file)
% Define initial concentrations
C0= [1 0 0 0 ];
% Time span
tspan=[0 1000];
% Run ODE solver
[t,x]=ode15s(@BATCOM,tspan, C0);
%Graph Plot
plot(t,x);
xlabel('Time[s]');
ylabel('Concentration[kmol/m3]');
legend('CA','CB','CC','CD');

Best Answer

Daniel - what are you incrementing TC by? 180, 181, 182, ..., 220? Or something else? Are you expecting to have a different plot for each value of TC? If so, then I suppose that you could nest the BATCOM within the main function that sets the value of TC. For example,
function myMainFunction
close all;
figure;
hold on; % ensures that all plots are retained on the axes
for TC = 180:10:220
% Define initial concentrations
C0= [1 0 0 0 ];
% Time span
tspan=[0 1000];
% Run ODE solver
[t,x]=ode15s(@BATCOM,tspan, C0);
%Graph Plot
plot(t,x);
drawnow;
end
xlabel('Time[s]');
ylabel('Concentration[kmol/m3]');
legend('CA','CB','CC','CD');
function [rate_out] = BATCOM(t,x)
% BATCOM code goes here (remove initialization of TC)
R = 8.31434; %Gas constant, kJ/kmol K
%Arrhenius constant, 1/s
A1= 5.78052E+10 ;
A2 = 3.92317E+12;
% etc.
end
end
Note how we iterate from 180 to 220 (with steps of ten). Since BATCOM is nested within myMainFunction, then it has access to the TC variable. See Nested Functions for more details.