# ODE 45 and function combination

1 view (last 30 days)
Matthijs neerhof on 22 Oct 2020
Commented: Star Strider on 22 Oct 2020
For my study I recieved an assignment to calculate the most optimal way of isolating a house. This is done using matlab.
The assignment is included underneath.
Me and my group are getting several errors, and really dont know what is going wrong.
We would really appreciate it if someone could help us out.
The script
% define constants
A = 1;
B = 2;
Cint = 1.0*10^4;
Cwall = 5.0*10^2;
R1 = 0.1;
R2 = 0.08;
Rwin = 0.1;
QHVAC = 0;
% time dependent window
tspan = [0:1:1440]; % calculate from t=0 up to t=1440
% can define the time interval steps
y0 = [18 20]; % initial conditions
Twall = y0(1);
Tint = y0(2);
odefunc = @(t,dydt) odeFunction(t, Twall, Tint, Tamb, Cwall, Cint, R1, R2, Rwin, QHVAC);
% integrate the system of differential equations from tspan(1) to
% tspan(2) with initial conditions y0
[t,dydt] = ode45(odefunc, tspan, y0);
% plot the results
plot(t,dydt)
The function
function [dydt] = odeFunction(t, Twall, Tint, Tamb, Cwall, Cint, R1, R2, Rwin, QHVAC)
% set of ordinary differential equations
% input: A - constant
% B - constant
% t - the time variable
% y - the state variable
%output: dydt - the set of equations
% initialize the set of equations
dydt = zeros(2,1);
% define the set of equations
dydt(1) = ((Tamb-Twall)/(Cwall *R2)) + ((Tint-Twall)/(Cwall*R1)) %Twall %y(2)+A;
dydt(2) = ((Twall-Tint))/(Cint *R1) + ((Tamb-Tint)/(Cint* Rwin)) + (QHVAC/Cint) %Tint %(A/B)*y(1);
end
The errors
Unable to perform assignment because the left and right sides have a different number of elements.
Error in odeFunction (line 15)
dydt(1) = ((Tamb-Twall)/(Cwall *R2)) + ((Tint-Twall)/(Cwall*R1)) %Twall %y(2)+A;
Error in odeScript>@(t,dydt)odeFunction(t,Twall,Tint,Tamb,Cwall,Cint,R1,R2,Rwin,QHVAC) (line 26)
odefunc = @(t,dydt) odeFunction(t, Twall, Tint, Tamb, Cwall, Cint, R1, R2, Rwin, QHVAC);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in odeScript (line 31)
[t,dydt] = ode45(odefunc, tspan, y0);
>>

Matthijs neerhof on 22 Oct 2020
I assume you mean y0 between the brackets at the top line of the function?
Ameer Hamza on 22 Oct 2020
No, I mean that inside the following function, you haven't used the state variable 'y' or time 't' anywhere
function [dydt] = odeFunction(t, Twall, Tint, Tamb, Cwall, Cint, R1, R2, Rwin, QHVAC)
% set of ordinary differential equations
% input: A - constant
% B - constant
% t - the time variable
% y - the state variable
%output: dydt - the set of equations
% initialize the set of equations
dydt = zeros(2,1);
% define the set of equations
dydt(1) = ((Tamb-Twall)/(Cwall *R2)) + ((Tint-Twall)/(Cwall*R1)) %Twall %y(2)+A;
dydt(2) = ((Twall-Tint))/(Cint *R1) + ((Tamb-Tint)/(Cint* Rwin)) + (QHVAC/Cint) %Tint %(A/B)*y(1);
end
Considering above, the line
odefunc = @(t,dydt) odeFunction(t, Twall, Tint, Tamb, Cwall, Cint, R1, R2, Rwin, QHVAC);
seems incorrect. Can you show your ODE in mathematical form?
Matthijs neerhof on 22 Oct 2020
What do you mean with mathematical form?

Star Strider on 22 Oct 2020
I cannot run your code, since I do not have the ‘Tamb’ data.
Nevertheless ‘Tamb’ is likely the problem, since it appears to be an array of some sort, and that is throwing the error. If it is time-dependent, it will be necessary to interpolate it using the approach in ODE with Time-Dependent Terms in the ode45 (and related functions) documentation.

Matthijs neerhof on 22 Oct 2020
Thanks!
I am again getting a graph with no results. The results should be as the file I added. I hope you can help us one last time.
Matthijs neerhof on 22 Oct 2020
I now also found a way to share the .dat file.
If I am correct it is in the zip-file I attatched.
Star Strider on 22 Oct 2020
I get this plot:
It is not exactly the one in the assignment, however you will need to sort that, since it most likely depends on where the 2 values of ‘Twall’ belong, since I am not certain where ‘Twall(1)’ and ‘Twall(2)’ go in the derivatives. I assigned them as I did simply to get the code to run without throwing errors.
The ‘odeFunction’ file did not change from my previous Comment. The calls to it and the plot call are:
odefunc = @(t,Twall) odeFunction(t, Twall, Tint, Tamb, Cwall, Cint, R1, R2, Rwin, QHVAC);
% integrate the system of differential equations from tspan(1) to
% tspan(2) with initial conditions y0
[t,y] = ode45(odefunc, tspan, y0);
% plot the results
figure
plot(t,y)
grid
xlabel('time (in minutes)')
ylabel('Temperature (celsius)')
as previously (with only the axis titles added).

### Community Treasure Hunt

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

Start Hunting!

Translated by