How to do an iteration with while loop

4 次查看(过去 30 天)
SITI AISHAH
SITI AISHAH 2019-3-28
回答: ag 2024-11-13,18:03
May somebody help me out with this. I want to do the iteration until Tf-T_average = 0. I want to put T_f as new Tf, however i do not know how. Attached is the iteration that I want to do and below is my Matlab code. I had try while and end code
%Iteration-----------------------------------------------------------------%
Tf = 140 ; %Assumed temperature (°F)
Viscousity = V_1*exp(V_2/(Tf + 95)); % Viscousity (reyn)
S = (C_ratio)^2*((Viscousity*(N/60))/P); %Sommerfeld Number
delta_T = (TRV_2 + (TRV_3*S)+(TRV_4*(S^2)))*(P)/9.7; % Temperature Rise (°F)
T_average = Temp_in + (delta_T/2); % Average temperature
T_f = (Tf + T_average)/2; % Average assumed temperature (°F)
delta_T2 = Tf -T_average
while delta_T2 > 0
delta_T2 = Tf -T_average
end

回答(1 个)

ag
ag 2024-11-13,18:03
Hi Siti
To perform an iterative process where you update Tf until the difference between Tf and T_average is zero, you need to set up a loop that recalculates Tf and T_average in each iteration.
Below is the modified version of your code:
Tf = 140 ; %Assumed temperature (°F)
while true
Viscousity = V_1*exp(V_2/(Tf + 95)); % Viscousity (reyn)
S = (C_ratio)^2*((Viscousity*(N/60))/P); %Sommerfeld Number
delta_T = (TRV_2 + (TRV_3*S)+(TRV_4*(S^2)))*(P)/9.7; % Temperature Rise (°F)
T_average = Temp_in + (delta_T/2); % Average temperature
T_f = (Tf + T_average)/2; % Average assumed temperature (°F)
delta_T2 = Tf -T_average
% update the required variables for each iteration as per the need
% for eg Tf = T_f
if delta_T2 <= 0
break
end
end
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by