# i cant seem to find the right value for k and it also says that eqn must be data of sym. how do i do it?

1 view (last 30 days)
Christella Bañaga on 29 Oct 2021
Answered: arushi on 12 Apr 2024
%Setup the variables U(t) as the function of Temperature over a period of time t, and k that will be used in the program.
%Find also the derivative of P(t) and set as dP
syms t;
syms k;
syms U(t);
dU = diff(U,t);
%Initial Conditions
cond1 = U(0)==165;
cond2 = U(10)==150;
Ta = 35 %Ambient Temperature
Ufinal = 70 %Final Temperature
%Set the differential equation model as eqn1;
eqn1 = diff(U,t)-k*(U-Ta)==0;
%Find k1 and k2, by solving the initial value problem eqn1 using cond1 and cond2, respectively.
Usol1(t,k) = dsolve(eqn1,cond1);
Usol2(t,k) = dsolve(eqn1,cond2);
%Solve for k by equating k1 and k2 at t=0. Save results as k.
Sol(k) = Usol1(0,k)-Usol2(0,k);
%Solve the eqn1 using the acquired value of k and using Initial value cond1.
eqn1 = Sol(k)==0;
%Let the Usoln be equal to Ufinal. Solve the equation and save your answer as tfinal
r = solve(eqn1);
Usoln(t) = Usol1(t,r);
eqn1 = Usoln(t)==Ufinal;
tfinal = subs(solve(eqn1));
% Plot the equation: Use the Title=Cake Temperature, XValue=Time (in Minutes), YValue=Temperature (F)
Title = "Cake Temperature";
XValue = "Time (in Minutes)";
YValue = "Temperature (F)";
%Use the domain (0, tfinal+5) with 0.2 gaps from each point
x=0:0.2:tfinal+20;
y = Usoln(x);
plot(x,y);
hold on;
title(Title);
xlabel(XValue);
ylabel(YValue);
plot(0,Usoln(0),'r*');
plot(tfinal,Usoln(tfinal),'r*');
hold off;

arushi on 12 Apr 2024
Hi Christella,
The primary issue in the code is the approach to finding (k) using the initial conditions and then using it to solve for (tfinal). Solving for (k) by equating two solutions of the differential equation at (t=0) does not mathematically make sense as it is trying to use the same differential equation with two different initial conditions separately, and then somehow equate these solutions to solve for (k). The correct approach is to use the information from both initial conditions together to solve for (k) in a single step, and then use this (k) to find when the temperature reaches (Ufinal).
Here's the modified code :
syms t k U(t);
% Initial Conditions
cond1 = U(0) == 165;
cond2 = U(10) == 150;
Ta = 35; % Ambient Temperature
Ufinal = 70; % Final Temperature
% Set the differential equation model
eqn = diff(U, t) == -k*(U - Ta);
% Solve the differential equation with the first initial condition to find a general solution
Usol = dsolve(eqn, cond1);
% Use the second condition to find k
kSol = solve(subs(Usol, t, 10) == 150, k);
% Substitute k back into the solution
UsolSub = subs(Usol, k, kSol);
% Solve for the time when the temperature is Ufinal
tfinal = solve(UsolSub == Ufinal, t);
% Prepare for plotting
fplot(subs(UsolSub, t), [0, double(tfinal) + 5], 'LineWidth', 2);
hold on;
title('Cake Temperature');
xlabel('Time (in Minutes)');
ylabel('Temperature (F)');
grid on;
plot(0, 165, 'ro'); % Initial condition point
plot(10, 150, 'ro'); % Second condition point
plot(double(tfinal), Ufinal, 'rx'); % Final condition point
legend('Temperature', 'Initial Condition', 'Condition at t=10', 'Ufinal', 'Location', 'Best');
hold off;
Hope this helps.