Use ode45 when I have a constant that varies over time.
3 views (last 30 days)
Show older comments
Nicola De Noni
on 16 May 2022
Commented: Nicola De Noni
on 20 Jul 2022
Hi everyone, I’m trying to solve this differential equation (dT/dt):
dTdt = ((U*A)/(M_mix*cp_mix))*(T_in-T_out)/(log(T_in-T)/(T_out-T));
Where I want to see the temperature profile T as the weather changes. Unfortunately, T_in and T_out are two temperature vectors whose values vary over time. How can I solve this differential equation? This is my code:
function f = CalcoloSperimentale(t,T)
%% Parameters
n = 10; % length of time vector
M_mix = linspace(11379,11379,n); % Weigth [Kg]
cp_mix = linspace(2000,2000,n); % Cp
U = linspace(53,53,n); % Thermal coefficient [W/m2K]
A = linspace(25.2,25.2,n); % Area
xmin_in = 15; % Minimum inlet temperature
xmax_in = 16; % Maximum inlet temperature
T_in = xmin_in+rand(1,n)*(xmax_in-xmin_in);
xmin_out = 20; % Minimum outlet temperature
xmax_out = 40; % Maximum outlet temperature
T_out = linspace(xmax_out,xmin_out,n);
% Differential equation
dTdt(i) = ((U(i)*A(i))/(M_mix(i)*cp_mix(i)))*(T_in(i)-T_out(i))/(log(T_in(i)-T(i))/(T_out(i)-T(i)));
f = dTdt;
end
And the second script is:
n = 10;
timeStart = 0;
timeEnd = 5280;
timespan = linspace(timeStart,timeEnd,n);
% Initial condition
initT = 98.5+273.15;
[timeOut, T] = ode45(@CalcoloSperimentale, timespan, initT);
% Plotting data
plot(timeOut, T)
In order to solve this differential equation i should have to consider also the final condition of temperature, but i don't know hot to implement it.
Thanks for the answers!
2 Comments
Torsten
on 16 May 2022
Could you explain in short what temperatures T_in, T_out and T are (so the background of your question)?
Accepted Answer
Torsten
on 16 May 2022
n=10;
xmin_in = 15; % Minimum inlet temperature
xmax_in = 16; % Maximum inlet temperature
T_in = xmin_in+rand(1,n)*(xmax_in-xmin_in);
xmin_out = 20; % Minimum outlet temperature
xmax_out = 40; % Maximum outlet temperature
T_out = linspace(xmax_out,xmin_out,n);
M_mix = 11379; % Weigth [Kg]
cp_mix = 2000; % Cp
U = 53; % Thermal coefficient [W/m2K]
A = 25.2; % Area
timeStart = 0;
timeEnd = 5280;
timespan = linspace(timeStart,timeEnd,n);
T_in = @(t)interp1(timespan,T_in,t);
T_out = @(t)interp1(timespan,T_out,t);
f = @(t,T) U*A/(M_mix*cp_mix)*(T_in(t)-T_out(t))/log((T_in(t)-T)/(T_out(t)-T));
% Initial condition
initT = 98.5+273.15;
[timeOut, T] = ode45(f, timespan, initT);
% Plotting data
plot(timeOut, T)
5 Comments
Torsten
on 16 May 2022
Edited: Torsten
on 16 May 2022
One problem surely is that you use Celsius temperature for the temperature in the cooling vessel and Kelvin temperature in the reactor ...
If you want to set the outlet temperature to 78+273.15, use
xmin_out = 78+273.15; % Minimum outlet temperature
xmax_out = xmin_out; % Maximum outlet temperature
More Answers (1)
Nicola De Noni
on 20 Jul 2022
Edited: Nicola De Noni
on 20 Jul 2022
2 Comments
Torsten
on 20 Jul 2022
For your above code to work, don't you have to use
T_in = ones(size(T_out))*(17 + 273.15); % Constant temperature of inlet cooling water
instead of
T_in = 17 + 273.15; % Constant temperature of inlet cooling water
?
In order to be more accurate I want to consider all the experimental data instead of a function.
The interpolation function considers all experimental data that are included in the arrays T_in and T_out. You could try a different interpolation method, e.g.
T_out = @(t) interp1(timespan,T_out,t,'spline')
and/or tighten the tolerances for ODE45:
options = odeset('RelTol',1e-8,'AbsTol',1e-8)
[timeOut, T] = ode45(f, timespan, initT,options);
to see whether the graph for T changes.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!