MATLAB: Ode45 code not producing correct results

MATLABode45

clear all; close all
clc
%SEMI-BATCH CODE CONCENTRATION AS A FUNCTION OF TIME
%time span specification
timeStart = 0; timeEnd = 200;%s
timeSpan = [timeStart, timeEnd];
%initial condition specification
initCA = 0.2; initCB = 0.1; initCC = 0; initCD = 0; %mol/m^3
initCon= [initCA, initCB, initCC, initCD];
%ODE solver CSTR
[time, Csemi] = ode45(@diffsemi, timeSpan, initCon,[]);
%plot CSTR
figure (1)
plot(time, Csemi, 'x')
grid on
xlabel('time [s]')
ylabel('concentration [mol/m^3]')
legend('c_A','c_B','c_C','c_D')
function dcdt = diffsemi(t, C)
cA0 = 0.2; %mol/L

cB0 = 0.1; %mol/L
V0 = 0;
Q0A = 1.5*10*-3;
Q0B = 1.5*10*-3;
Q0 = Q0A + Q0B;
V = Q0*t + V0;
k1 = 100; %L/(mol*s)

k2 = 0.15; %L/(mol*s)
%Rate equations
rA = -(k1*C(1)^2*C(2))-(k2*C(1)*C(2));
rB = rA;
rC = (k1*C(1)^2*C(2));
rD = (k2*C(1)*C(2));
% differential equations
dcdt = [ Q0A*(cA0/V) - Q0*(C(1)/V) - (-rA);
Q0B*(cB0/V) - Q0*(C(2)/V) - (-rB);
-Q0*(C(3)/V) + rC;
-Q0*(C(4)/V) + rD];
end
So I have this problem that I'm trying to solve using ode45. There appears to be something with wrong with the function I believe cause the graph that I get looks like this.

Best Answer

The initial value for V is zero, so, because you divide by V in dcdt you get NaNs everywhere. You need to revisit how you calculate V.