Error in ode45 (line 107)

Hello, I'm trying to model the biomass accumulation over time of a microorganism that depends on temperature using a differential equation that I want to solve with ode45 function. But I got this error message:
Error using odearguments
@(TIME,XI)DXDT(TIME,XI,TEMP,PARAM,PARAM_PH,PH) returns a vector of length 29613, but the length of initial conditions vector is 1. The vector returned by
@(TIME,XI)DXDT(TIME,XI,TEMP,PARAM,PARAM_PH,PH) and the initial conditions vector must have the same number of elements.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
After checking, I figured out that the ode45 function doesn't take my temperature vector but only one cell of it (knowing that the size of my temperature and time vectors is the same).
Can you help me out to solve this issue please?
Here is my code for ode45:
tspan = Time; %Time vector with the same length as experimental data
%C0 is the initial biomass concentration
%the vector param contains my parameters [tmin,topt,tmax,mumax]
[t,xi] = ode45(@(t,xi) dxdt(t,xi,temperature,param),tspan,C0, odeset('RelTol',1E-3));
Here is my dfunction to calculate the ifferential equation:
function dC=dxdt(t,C,Temp,param,param_ph,pH)
dC=C.*phi(param,Temp);
end
Thanks in advance for you help :)

3 Comments

Cris LaPierre
Cris LaPierre on 15 May 2024
Edited: Cris LaPierre on 15 May 2024
Your ode function must return the result for a single time step. The error is saying your function is returning too many outputs. The outputs should be a column vector the same length as your initial conditions.
Your odefxn expects the following inputs
  • Current time, t
  • Previous timestep output, C (this is the value of dC in the previous time step)
  • Additional input, Temp (undefined)
  • Additional input, param (undefined)
  • Additional input, param_ph (undefined)
  • Additional input, pH (undefined)
We don't really have enough information to answer your question yet (what is phi?). Could you share a working example that throws the error message?
Yob
Yob on 16 May 2024
Thank you for your unswer, and sorry that i didn't give a better explanation of my problem.
Here is my function to calculate the differential equation where phi is another function that calculates the growth rate in function of the temperature:
% Temp is a vecrtor containing the temperature values, and param is another vector containing the growth parameters tmin, tmax and topt
function dC=dxdt(t,C,Temp,param)
dC=C.*phi(param,Temp);
end
And now I could know why I receive that error message: my function should return a vector of length 29613 since my tspan and temperature vectors have the same length "29613". However, the problem is that my function doesn't take the "Temp" input as a vector but only as one value.
Thanks in advance.
Yob
Yob on 16 May 2024
Anyway, I thank you for your help, I fixed the issue and my model works.

Sign in to comment.

Answers (0)

Asked:

Yob
on 15 May 2024

Commented:

Yob
on 16 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!