Random exponential function between two values

Hello everyone! I’m trying to create a random vector with a maximum value of xmax_out = 65 and a minimum value of xmin_out = 16. This vector. However, I would like this vector to have an exponential shape, as shown, because I want to simulate the temperature that decreases in contact with a hot fluid to be cooled.
As you can see: I would like the temperature at time 0 (therefore origin of the x axis) to be equal xmax_out while at time t the temperature reaches the xmin_out value. In the project the cooling time is about 50 min.
I tried to use the following code to generate a decreasing random vector, but unfortunately it is linear.
% Temperature range
n = 50; % Length of time [min]
xmin_out = 16 + 273.15; % Minimum outlet temperature [K]
xmax_out = 65 + 273.15; % Maximum outlet temperature [K]
% Random Vector of temperature:
T_out = sort((xmax_out-xmin_out)*rand(n,1)+xmin_out, "descend");
Thanks!

 Accepted Answer

Are you looking for an exponential decay like this?
t = linspace(0, 3000, 30001);
tmin = 50;
xmin = 16;
xmax = 65;
k = -log(xmin/65)/(60*tmin);
T = xmax*exp(-k*t);
plot(t/60, T, 'linewidth', 1.5)
grid on
xlabel('Time, t [min]')
ylabel('Temperature, T')

6 Comments

Yes! The course is that in the figure. But I wanted a function where the maximum and minimum temperature values were present inside. Because xmax_out and xmin_out are inputs and could change. I would like a function like this:
T = xmax_out*exp(-k*xmin_out*t)
I hope you caught the problem.
Sounds like you want to express the Temperature as a function of the input parameters {tmin, xmin, xmax}.
I have revised my Answer. Please check if it is acceptable.
That's is what I want! Thanks a lot @Sam Chak!
I'm sorry @Sam Chak, in my project the temperature is in Kelvin and your code doesn't work.
t = linspace(0, 3000, 3000); % 3000 seconds == 50 min
tmin = 50;
xmin = 20+273.15; % Temperature [Kelvin]
xmax = 45+273.15; % Temperature [Kelvin]
k = -log((xmin)/(xmax))/(60*tmin);
Temp = (xmax)*exp(-k*t);
plot(t/60, Temp, 'linewidth', 1.5)
grid on
xlabel('Time, t [min]')
ylabel('Temperature, T')
In this way the plot represents a line (not an exponential shape) between the minimum temperature and the maximum one. Could you help me?
The shape comes from an exponential, but it's not visible in the interval [0:50].
If you plot for
t = linspace(0,3000*10,3000)
, you'll see the exponential behaviour.
Since the absolute Kelvin is just an additive reference, you can do this way:
t = linspace(0, 36000); % 3000 seconds == 50 min; plot up to 600 min
tmin = 50;
xmin = 20; % Temperature [Kelvin]
xmax = 45; % Temperature [Kelvin]
k = -log((xmin)/(xmax))/(60*tmin);
Temp = (xmax)*exp(-k*t) + 273.15;
plot(t/60, Temp, 'linewidth', 1.5)
grid on
xlabel('Time, t [min]')
ylabel('Temperature, T [K]')
It actually behaves exponentially. Just that the 50 min duration is relatively short to see the effect.

Sign in to comment.

More Answers (0)

Asked:

on 18 May 2022

Commented:

on 18 May 2022

Community Treasure Hunt

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

Start Hunting!