# Use ode45 when I have a constant that varies over time.

8 views (last 30 days)
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.
##### 2 CommentsShowHide 1 older comment
Nicola De Noni on 16 May 2022
Edited: Nicola De Noni on 16 May 2022
Absolutely! T_in is the inlet temperature of a chemical reactor jacket and T_out is the outlet temperature of the reactor jacket. I’m studying the heat transfer in a water-cooled tank. T is the temperature inside the vessel.
I would like to plot the temperature inside the reactor vs time (by knowing the inlet and outlet temperature of the cooling fluid) in order to understand the heat transfer.

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)
Nicola De Noni on 16 May 2022
You're right, i'm sorry! Anyway, thanks a lot!

Nicola De Noni on 20 Jul 2022
Edited: Nicola De Noni on 20 Jul 2022
Hi everyone!
I'm using the script created by @Torsten but I need to modify it because I have some experimental data instead of function. I will explain it better after posting the script.
As you can see there are two variables (T_in and T_out) that are used in the ODE like and interpolation of data. Due to this approximation, Matlab shows me a linear variation of temperature inside the reactor. In order to be more accurate I want to consider all the experimental data instead of a function. How can I do it?
Thanks!
In the first plot you can see the results of the ODE in blue. Probably this is a line due to the use of
T_in = @(t)interp1(timespan,T_in,t);
T_out = @(t)interp1(timespan,T_out,t); In the second plot there is the temperature profile of the outlet cooling fluid. clear all; clc; close all
T_out = (xlsread('experimentaldata.xlsx',1,'E1651:E1682'))+273.15; % Experimental data of outlet cooling water
n=length(T_out);
timeStart = 0; % Initial time [s]
timeEnd = 15.5*60; % Final time [s]
timespan = linspace(timeStart,timeEnd,n); % Time vector
xmin_in = 17 + 273.15; % Minimum inlet temperature
xmax_in = 17 + 273.15; % Maximum inlet temperature
T_in = 17 + 273.15; % Constant temperature of inlet cooling water
M_mix = 11379; % Weigth [Kg]
cp_mix = 2000; % Specific heat [J/KgK]
U = 102.408898670153; % Thermal coefficient [W/m2K]
A = 25.2; % Area
%% I want to change the vector T_in and T_out in order to use the single experimental data
% instead of a linear function.
T_in = @(t)interp1(timespan,T_in,t);
T_out = @(t)interp1(timespan,T_out,t);
% Temperature of fluid to be cooled
f = @(t,T) (-U*A*((T-T_in(t))-(T-T_out(t)))/log((T-T_in(t))/(T-T_out(t))))/(M_mix*cp_mix);
% Initial condition: Initial temperature of fluid to be cooled
initT = 121.2307587+273.15; % [K]
[timeOut, T] = ode45(f, timespan, initT);
##### 2 CommentsShowHide 1 older comment
Nicola De Noni on 20 Jul 2022
Thank you @Torsten!
Have a nice day.