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

5 Ansichten (letzte 30 Tage)
Nicola De Noni
Nicola De Noni am 16 Mai 2022
Kommentiert: Nicola De Noni am 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 Kommentare
Torsten
Torsten am 16 Mai 2022
Could you explain in short what temperatures T_in, T_out and T are (so the background of your question)?
Nicola De Noni
Nicola De Noni am 16 Mai 2022
Bearbeitet: Nicola De Noni am 16 Mai 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 16 Mai 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 Kommentare
Torsten
Torsten am 16 Mai 2022
Bearbeitet: Torsten am 16 Mai 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
Nicola De Noni
Nicola De Noni am 16 Mai 2022
You're right, i'm sorry! Anyway, thanks a lot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Nicola De Noni
Nicola De Noni am 20 Jul. 2022
Bearbeitet: Nicola De Noni am 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 Kommentare
Torsten
Torsten am 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.
Nicola De Noni
Nicola De Noni am 20 Jul. 2022
Thank you @Torsten!
Have a nice day.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Model Predictive Control Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by