MATLAB: ODE solving lack of span inputs

functionshandlesode45span

I have this piece of code and I dont know what is the problem, it says the tspan argument must have at least two arguments.
%Initial conditions
tspan=[0, 168169];
c0=[0.5,0,0];
%Solving for the differential equation for the volume
[t,lnV]=ode45(@cstP_V,tspan,c0);
%Since written in terms of concentration of A, extraxt last value (at time
%t) of the first column
V=exp(lnV(end,1))
%Function is defined
function dlnV=cstP_V(t,C) %Time and initial concentration constants
k=0.0023; %Reaction rate constant
function dC=cstP_C(t,C) %Embeded function determining the rate of change of concentration of compounds
%Assigning inital concentrations
CA=C(1);
CB=C(2);
CC=C(3);
%Defining rate laws
r=k*CA^2;
CA=-r;
CB=r;
CC=r;
%Assigning the rates of change of each compound to the output
dC(1,:)=CA;
dC(2,:)=CB;
dC(3,:)=CC;
end
dCA=cstP_C(t,C); %obtaining rate of change in concentration with initial conditions from main function
[~,C1]=ode45(@cstP_C,[0 1],C); %solving for concentration values (tspan values had to be respecified for after already calling function)
ca=C1(end,1); %determining ca at time t
dlnV=-1*(ca^(-1)*dCA+(k*ca)); %specifying the rate of change in ln(V) from derived relation
end

Best Answer

OK, you have 2 coupled ODEs. If I expand the D/Dt(log(V(t))) to 1/V(t)*D/Dt(V(t) I'd get something like this:
function dVdtdcadt = odeperhaps(Vca,t)
k = 12; % perhaps some correction...
V = Vca(1);
ca = Vca(2);
dcadt = k*ca^2;
dVdt = -k*ca*V - dcadt*V/ca;
dVdtdcadt = [dVdt;dcadt];
end
These coupled ODEs you then integrate together. You might want to set the ode-options that ensure the solutions are kept positive - check the help and documentation for ode45 and odeset, it should be the 'NonNegative' field that should be used. That ought to make the V and ca in the denominators safe...
Perhaps I misstyped something.
HTH