Help with iteration problem

30 ビュー (過去 30 日間)
Vyom Desai
Vyom Desai 2023 年 1 月 23 日
コメント済み: Vyom Desai 2023 年 1 月 23 日
Background Info: The problem is that I have 2 equations of pressure (pressure loss and pressure gain) that need to equal each other where at that point outputs the certain mass flow rate. As some variables depend on temperature, Ive asked the user to enter a certain temperature to be used in the calculations. Ive asked to also enter a mass flow rate just to get an intial idea of the pressure difference.
I need to run an iteration where both these values become the same so that I can work out the mass flow rate. Below is the code:
%Calls functions 'calculateDP' and 'calculatePD' to work out total Hydraulic Resistance
pipeHP = calculateDP(T,mdot,rho,mu,dpipe,Lpipe);
motorHP = calculatePD(T,mdot,rho,mu,dmotor,Lmotor);
totalHP = pipeHP + motorHP;
disp("Hydrualic Resistance of pipe: " + totalHP + "Pa") %Total pressure loss across system
%Calls 'pressuregain' function to work out pressure gain from fan
fangainpressure = calculatePG(mdot,rho,g);
disp("Pressure gain by fan: " + fangainpressure + "Pa")
%Loop to find mdot when pressure gain = pressure loss
while totalHP ~= fangainpressure
mdotpipe = (pipeHP^(4/7)*pi*rho^(4/7))/(1.395*Lpipe^(4/7)*mu^(1/7)*dpipe^(4/7)); %%%%%CHECK WHAT PRESSURE TO USE - NEED TO DO HAND CALCS HERE TO CONFIRM%%%%
mdotmotor = (motorHP^(4/7)*pi*rho^(4/7))/(1.395*Lmotor^(4/7)*mu^(1/7)*dmotor^(4/7)); %%%%%CHECK WHAT PRESSURE TO USE - NEED TO DO HAND CALCS HERE TO CONFIRM%%%%
totalmdot = mdotpipe+mdotmotor
end
disp("The mass flow rate across system is " + totalmdot + "kg/s")
I am unsure on how to code the while or for loop so that until the pressures are the same it keeps on repeating and then outputs the specific mass flow rate when pressures are equal.

回答 (1 件)

Image Analyst
Image Analyst 2023 年 1 月 23 日
See the FAQ:
Use ismembertol or abs since those floating point numbers will never be exactly equal.
someTolerance = 0.1; % Whatever....
loopCounter = 1;
% Set up a failsafe
maxIterations = 100; % Way more than you think it would ever need.
while abs(totalHP - fangainpressure) > someTolerance
while totalHP ~= fangainpressure
mdotpipe = (pipeHP^(4/7)*pi*rho^(4/7))/(1.395*Lpipe^(4/7)*mu^(1/7)*dpipe^(4/7)); %%%%%CHECK WHAT PRESSURE TO USE - NEED TO DO HAND CALCS HERE TO CONFIRM%%%%
mdotmotor = (motorHP^(4/7)*pi*rho^(4/7))/(1.395*Lmotor^(4/7)*mu^(1/7)*dmotor^(4/7)); %%%%%CHECK WHAT PRESSURE TO USE - NEED TO DO HAND CALCS HERE TO CONFIRM%%%%
totalmdot = mdotpipe+mdotmotor
% Somehow update at least one of totalHP or fangainpressure.
% Missing code -- was never posted.
% Then compute and plot the difference.
theDifference(loopCounter) = abs(totalHP - fangainpressure);
loopCounter = loopCounter + 1;
% Plot how the difference is changing
plot(loopCounter, 'b.-', 'LineWidth', 2)
hold on;
end
% Alert user if we exited normally, or if the failsafe kicked us out to avoid an infinite loop.
if loopCounter < maxIterations
% Then the loop found the condition and exited early, which means normally.
fprintf('Loop exited normally after %d iterations.\n', loopCounter);
else
% Then the loop never found the condition and exited when the number of iterations
% hit the maximum number of iterations allowed, which means abnormally.
fprintf('Loop exited abnormally after iterating the maximimum number of iterations (%d) without obtaining the exit criteria.\n', maxIterations);
end
fprintf('All done after %d iterations.\n', loopCounter)
  1 件のコメント
Vyom Desai
Vyom Desai 2023 年 1 月 23 日
thank you very much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by