Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Curious
Posts:
2,359
Registered:
12/6/04


Re: Help with while loop
Posted:
Sep 12, 2013 4:54 PM


"Yutthana Jeenjiu" <jimmyj79@iinet.net.au> wrote in message <l0so6n$g3n$1@newscl01ah.mathworks.com>... > Please help with trying to make the while loop work > > e =0.0015; > D =50; > V =2; > Dm =0.05; > v =1.01*10^6; > Re = V*Dm/v; > N =1000; > f = linspace(0.008,0.1,N); > > i = 0; > f_abs = 1; > FF = 0.008; > > while FF(end) < f_abs; > i = i + 1; > FF = log(e/D/3.7 + 2.51*Re*sqrt (f(i)))*sqrt(f(i))*2; > end > > My aim is to iterate through f until the equation or FF is equal to 1 or less than .001 away from 1. > Please note i am a beginner user and have no prior programming knowledge
% I don't have MATLAB installed on this computer, so I can't try to run your code. % It would help if you said WHY you think the loop is not working % (error message, endless loop, etc.).
% Here are some things I can point out:
while FF(end) < f_abs; % should simply be: while FF < f_abs;
FF = log(e/D/3.7 + 2.51*Re*sqrt (f(i)))*sqrt(f(i))*2; % is not correct, the *2 at the end should give you an error. % Perhaps you mean: FF = 2*log(e/D/3.7 + 2.51*Re*sqrt (f(i)))*sqrt(f(i));
% The code will crash if i becomes greater than length(f) % (assuming the while loop still doesn't terminate). % One way around this would be to rewrite the loop as:
% N =1000; % f = linspace(0.008,0.1,N); f = 0.008;
% i = 0; f_abs = 1; FF = 0.008;
while FF < f_abs % i = i + 1; % FF = log(e/D/3.7 + 2.51*Re*sqrt (f(i)))*sqrt(f(i))*2; FF = 2*log(e/D/3.7 + 2.51*Re*sqrt (f))*sqrt(f); f = f + 0.1; end
% This may still be an endless loop, but it shouldn't crash. % Do you perhaps mean: while abs(FF) < f_abs % instead?
> > Thanks Jimmy



