ODE45 - must return a column vector.

I have following code:
B = readtable('t13.xlsx');
t13 = table2array(B);
c = readtable('ambient.xlsx');
ambient = table2array(c);
d = readtable('solar.xlsx');
solar = table2array(d);
tspan = [0 50];
y0 = 20;
[t,y14] = ode45(@(t,y14) odefcn(t,y14,t13,solar,ambient), tspan, y0);
function dy14dt = odefcn(t,y14,t13,solar,ambient)
dy14dt = zeros(50,1)
dy14dt = [-1.3461*y14 - 1.3556*t13 + 4.3505*solar -1.3556*ambient];
%dy14dt= dy14dt(:);
end
I am getting an error as follows:
Error using odearguments
@(T,Y14)ODEFCN(T,Y14,T13,SOLAR,AMBIENT) must return a column vector.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in validation (line 11)
[t,y14] = ode45(@(t,y14) odefcn(t,y14,t13,solar,ambient), [0 41.5], y0);
Based on one of the previous post, I tried putting this line: dy14dt= dy14dt(:); but it did not work. Kindly help me to solve this error. Thanks!

 Accepted Answer

B = readtable('t13.xlsx');
t13 = table2array(B);
c = readtable('ambient.xlsx');
ambient = table2array(c);
d = readtable('solar.xlsx');
solar = table2array(d);
tspan = [0 50];
y0 = 20;
[t,y14] = ode45(@(t,y14) odefcn(t,y14,t13,solar,ambient), tspan, y0);
plot(t,y14)
function dy14dt = odefcn(t,y14,t13,solar,ambient)
solart = interp1(t13,solar,t);
ambientt = interp1(t13,ambient,t);
dy14dt = -1.3461*y14 - 1.3556*t + 4.3505*solart -1.3556*ambientt;
end

9 Comments

Hi Torsten,
Thank you very much for the answer. The code suggested by you does work without error but the results are different from expected. Can you please tell me why solart and ambientt are added, it will help me in understanding. Thank you!
What is your ode modeling? It would be helpful if you could describe that, preferably both in words and the mathematical equations.
y14 is the value of temperature and solar, ambient and t13 are the values that influence this temperature.
dy14/dt = -1.3461*y14 - 1.3556*t13 + 4.3505*solar -1.3556*ambient
Torsten
Torsten on 27 Jan 2023
Edited: Torsten on 27 Jan 2023
You have arrays t13, solar and ambient. I suspect that solar and ambient are factors that influence y14 at times t13. Thus, when the solver tells you to return dy14/dt at time t, you must compute
dy14/dt = -1.3461*y14 - 1.3556*t + 4.3505*solar(t) - 1.3556*ambient(t)
where solar(t), ambient(t) are taken as approximate values for "solar" and "ambient" from your arrays at time t.
"Approximate values" means that you interpolate their values from the graphs (t13,solar) and (t13,ambient).
Is my understanding correct ?
Hello Torsten,
Thank you for your reply.
Actually, solar, ambient and t13, all three factors are indepedent and influence y14.
t13 is not a time value but a temperature value.
Torsten
Torsten on 28 Jan 2023
Edited: Torsten on 28 Jan 2023
So at time t, you have calculated a temperature y14 and you must supply t13, solar and ambient for the equation
dy14dt = [-1.3461*y14 - 1.3556*t13 + 4.3505*solar -1.3556*ambient];
to calculate the time derivative of y14.
What are the values from these arrays t13, solar and ambient do you have to use for this ?
Is there a time vector "T" as independent variable in the background such that t13(i), solar(i) and ambient(i) are the values measured at time T(i) ?
Yes, there is a time vector 'T' as independent variable in background and t13(i), solar(i) and ambient(i) are the values measured at time T(i).
Then insert this time vector T and use
B = readtable('t13.xlsx');
t13 = table2array(B);
c = readtable('ambient.xlsx');
ambient = table2array(c);
d = readtable('solar.xlsx');
solar = table2array(d);
T = ??
tspan = [0 50];
y0 = 20;
[t,y14] = ode45(@(t,y14) odefcn(t,y14,T,t13,solar,ambient), tspan, y0);
plot(t,y14)
function dy14dt = odefcn(t,y14,T,t13,solar,ambient)
t13t = interp1(T,t13,t);
solart = interp1(T,solar,t);
ambientt = interp1(T,ambient,t);
dy14dt = -1.3461*y14 - 1.3556*t13t + 4.3505*solart -1.3556*ambientt;
end
It worked. Thank you!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!