MATLAB: Find Optimum Temperature where Concentration of product vs Time is high.

ode45optimum temperatureoptimum timeplotting concentration vs timetwo ode solver

I know how to solve ODE with const Temperature, but with temperature range I find it difficult to solve.
function dYdt = odereal2(t,Yf);
CA = Yf(1);
CB = Yf(2);
T = 335.32; %just assumed the constant temperature.
k1 = 4000*exp(-2500/T);
k2 = 620000*exp(-5000/T);
dCAdt = -k1*CA^2;
dCBdt = k1*CA^2-k2*CB;
dYdt = [dCAdt; dCBdt;];
This is my function that includes Derivative equation.
tspan = [0 3];
C0 = [1.; 0];
[t,C]=ode45(@odereal2,tspan,C0);
for i=1:size(C,2)
plot(t,C,'pr') %from that plot we can infer the maximum yield of product B
axis([0 3 0 1])
title(' Plot of concentration(C) profiles at optimal residence time' );
xlabel(' Time (hr)');
ylabel(' Concentration (C)');
pause
end
And here is my ODE solver code. I just want to find the optimum temperature when concentration of product B reaches the maximum.
Kindly help me in this problem. Thanks in advance

Best Answer

Does this help:
tspan = 0:0.1:3;
C0 = [1.; 0];
T = 330:5:360;
for i = 1:numel(T)
[t,C]=ode45(@odereal2,tspan,C0,[],T(i));
CA(:,i) = C(:,1);
CB(:,i) = C(:,2);
end
plot(t,CA,'pr-',t,CB,'bo-') %from that plot we can infer the maximum yield of product B
axis([0 3 0 1])
title(' Plot of concentration(C) profiles at optimal residence time' );
xlabel(' Time (hr)');
ylabel(' Concentration (C)');
function dYdt = odereal2(~,Yf,T)
CA = Yf(1);
CB = Yf(2);
% T = 335.32; %just assumed the constant temperature.
k1 = 4000*exp(-2500/T);
k2 = 620000*exp(-5000/T);
dCAdt = -k1*CA^2;
dCBdt = k1*CA^2-k2*CB;
dYdt = [dCAdt; dCBdt;];
end