is for loop or while loop better?

9 vues (au cours des 30 derniers jours)
Anastasia Zistatsis
Anastasia Zistatsis le 22 Jan 2021
I'm completing a homework question, and would like advice before I continue my code.
I will paste the problem below. I chose to use a while loop to solve the problem, but would a for loop me more applicable? If so, why is that?
Here is my code:
while (2500 <= Re) && (Re <= 1000000)
f = (1/((sqrt(f)*(4*log(Re*sqrt(f))-0.4))))^2; %von Karman equation
disp(f);
end
%user inputs values here
For fluid flow in pipes, friction is described by a dimensionless number, the Fanning friction factor f. The Fanning friction factor is dependent on a number of parameters related to the size of the pipe and the fluid, which can all be represented by another dimensionless quantitiy, the Reynolds number R e. A formula that predicts f given R e is the von Karman equation:
Typical values for the Reynolds number for turbulent flow are 10,000 to 500,000 and for the Fanning Friction factor are 0.001 to 0.01. Develop a function that uses FalsePosition (es=0.000001 and maxit=200) to solve for f given a user-supplied value of Re between 2,500 and 1,000,000.
  1 commentaire
dpb
dpb le 22 Jan 2021
Well, in the code given, there's no purpose in the while at all; nothing inside the loop changes the Re number to terminate the loop if it once enters it.
There could be a reason for a while in the iteration loop to solve for the friction factor, but it would be more useful for it to be comparing the convergence condition; the Re number is presumed known and fixed to solve for f
A general consideration in choosing one over the other is that for loops are best for counting and limits whereas while is good as noted above when something calculated changes and that calculation is inside the loop (a convergence as here is a prime example).
In this case you have both kinds of tests because the iteration limit is counted so you could choose to wrap the iteration inside that kind of a structure; I'd probably go the other way and build the outer loop on the convergence and just count iterations internally and break/error if exceed the maximum.
In the end, it's user's choice but those are some guiding principles.

Connectez-vous pour commenter.

Réponse acceptée

Hossein Malekahmadi
Hossein Malekahmadi le 22 Jan 2021
Modifié(e) : Hossein Malekahmadi le 22 Jan 2021
Hi sir, I also studing water engineering, about "for loop" and "while loop" i prefer while specially about your homework question, my reason for that is you can do alot of things with while loop that you cant do with for loop specially in your question it will be alot easiler to write code for colebrook equation using while loop, i did this coding for my university as well that i glad share it with you, i hope it help you as well. "Re" is reynolds number, "e" is roughtness of pipe (depend on your pipe material), "D" is your pipe Diameter. you can also check moody diagram as well.
% your input should be Re=[...], e=[...], D=[...].
if Re<=2000 && Re>0 % laminar flow
f1=64/Re; % because flow is too slow the roughness of pipe is ineffective, and only reynolds number effecting friction
disp(['Coef Of Friction is ' num2str(f1)]);
elseif Re>2000 % turbulence flow
f1=0.008; % minimum possible friction
f2=0.08; % maximum possible friction
% at this point i used linear interpolation for calculating friction.
% in this method you have to choose minimum and maximum
% values for linear interpolation, and the best choice is
% maximum and minimum values of colebrook friction.
F1=(1/sqrt(f1))+(0.869*log(((e/D)/3.7)+(2.523/(Re*sqrt(f1))))); % minimum value of friction
F2=(1/sqrt(f2))+(0.869*log(((e/D)/3.7)+(2.523/(Re*sqrt(f2))))); % maximum value of friction
f3=f2-(F2*(f2-f1)/(F2-F1)); % this is the start of linear interpolation
while abs(f1-f2)>0.0000001 % i used while loop here for reaching the 0.0000001 error.
F1=(1/sqrt(f1))+(0.869*log(((e/D)/3.7)+(2.523/(Re*sqrt(f1)))));
F2=(1/sqrt(f2))+(0.869*log(((e/D)/3.7)+(2.523/(Re*sqrt(f2)))));
F3=(1/sqrt(f3))+(0.869*log(((e/D)/3.7)+(2.523/(Re*sqrt(f3)))));
if F3<0
f2=f3;
else
f1=f3;
end
f3=(f1+f2)/2;
end
disp(['Coef Of Friction is ' num2str(f1)]);
else
disp('Input Reynolds number is invalid');
end

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by