dTdt=−k(T−Ta) k=0.4,Ta=25∘, initial condition T(0)=T0=75∘C .how to solve using ode45 and report the temperature obtained at times t=1,2,3 and 4 . report T(1.0)
Show older comments
ORIGINAL:
dTdt=−k(T−Ta)
k=0.4,Ta=25∘C, k=0.4,Ta=25° and the initial condition T(0)=T0=75∘
The true solution of the ODE is given by:
T(t)=Ta+(T0−Ta)e−kt

The true solution of the ODE is given by:
9 Comments
Ramesh
on 13 Mar 2024
Dyuman Joshi
on 13 Mar 2024
How is your question related to MATLAB?
Chuguang Pan
on 13 Mar 2024
Sam Chak
on 13 Mar 2024
The Law of Cooling is shivering COOL 🥶! However, two of your previous questions are still open. Would you like to follow up and provide feedback? This will allow others to update their answers or submit new ones, so that you can evaluate and close them satisfactorily.
Question 1 (opened on 2 Feb 2024):
Question 2 (opened on 5 Feb 2024):
Since an analytical solution exists for a first-order linear system and its mathematical structure is simple enough for display, it would be nice to straightforwardly use another popular command from the Symbolic Math Toolbox.
Edit: The code is edited to reflect on the true solution is corrected by @Chuguang Pan. However, @Ramesh should verify it mathematically.
k = 0.4;
Ta = 25;
T0 = 75;
e = exp(1); % Numeric Representation of e
% T = @(t) Ta + (T0 -Ta)*e - k*t; % true solution supplied by the OP
T = @(t) Ta + (T0 -Ta)*e.^(- k*t);% true solution corrected by Chuguang Pan
t = 0:0.1:20; % simulate from time 0 to 20
plot(t, T(t)), grid on, xlabel t, ylabel T(t)
title('Temperature decay based on the Analytical Solution')
For the numeric representation of e, please see https://www.mathworks.com/help/matlab/ref/exp.html#bt_a2dn-1.
Ramesh
on 13 Mar 2024
Sam Chak
on 13 Mar 2024
I understand that you are unable to obtain the answer, which might be due to a possible error in entering the equation in the MATLAB code when using the ode45() command. To help troubleshoot the issue, please click on the indentation icon
and copy/paste the MATLAB code into the grey field. This will allow us to assist you further.
Chuguang Pan
on 13 Mar 2024
I think the true solution of T should be
, so the formula of T in your code should be:
, so the formula of T in your code should be:T=@(t) Ta + (T0-Ta)*exp(-k*t);
Sam Chak
on 13 Mar 2024
Answers (1)
Hi Ramesh,
You can follow the following steps to solve a differential equation using "ode45" function
1) Define the ODE function:Create a function that represents the right-hand side of the ODE.
2) Solve the ODE using ode45: "ode45" function is used for solving initial value problems for ordinary differential equations. Give the function handle, time span and initial conditions as input to the function.
3) Extract the Temperatures at desired times: After solving the ODE, you can extract the temperature values at the desired times using interpolation
Here is the code implementation for the same:
% Define the parameters
k = 0.4;
Ta = 25;
T0 = 75;
% Define the ODE function
odefun = @(t, T) -k * (T - Ta);
% Time span (from t=0 to t=4)
tspan = [0 4];
% Initial condition
T_initial = T0;
% Solve the ODE
[t, T] = ode45(odefun, tspan, T_initial);
% Interpolate or find the closest values to t=1, 2, 3, and 4
T1 = interp1(t, T, 1.0); % Interpolating for T at t=1.0
% Display the result for T(1.0)
fprintf('The temperature at t=1.0 is approximately %.2f°C\n', T1);
You can refer to the following MathWorks documentation to understand more about "ode45" function.
I hope this helps.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!