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?
조회 수: 2 (최근 30일)
이전 댓글 표시
%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;
댓글 수: 0
답변 (1개)
arushi
2024년 4월 12일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!