what is wrong with my 'While' function
2 views (last 30 days)
Show older comments
i want to increase (n) until TT=s
v=0.5;
d=0.1;
density=1000;
w=0.446;
t=0.2;
rr=30.494*v^5-86.479*v^4+89.045*v^3-34.023*v^2+6.6418*v;
va=v*(1-w);
s=rr/(1-t);
n=1;
while n>1;
n=n+1;
j=va/(n*d);
kt=0.392*(1-(j/0.95))^0.8;
TT=kt*density*n^2*d^4;
if TT=s;
break
end
end
This is my program.... when i run it i received this error message '>> T=T Undefined function or variable 'T'.'
0 Comments
Accepted Answer
Ilham Hardy
on 30 May 2013
Edited: Ilham Hardy
on 30 May 2013
I ran your code and found:
- Matlab is about precision, in terms that your while loops only break if the TT value is exactly the same as s. So the script will executes until n<12000 instead of stopping at the desired vessel speed. To circumvent the problem, check your script well! and use no exact value but a tolerance as shown by Azzi above.
- DO NOT name your variable i or j
- the variable n is usually expressed in rps instead of rpm.
See the code below
v=0.5;
d=0.1;
density=1000;
w=0.446;
t=0.2;
rr=30.494*v^5-86.479*v^4+89.045*v^3-34.023*v^2+6.6418*v;
va=v*(1-w);
s=rr/(1-t);
n=1;
while n<12000;
n=n+1;
j_=va/(n*d);
kt=(0.392*(1-(j_/0.95)))^0.8;
TT=kt*density*n^2*d^4;
if TT>=(s-0.5) && TT<=(s+0.5) ; %example
break
end
end
0 Comments
More Answers (4)
Azzi Abdelmalek
on 29 May 2013
Edited: Azzi Abdelmalek
on 29 May 2013
n=1;
while n>1;
You will never be in the loop
Also, there is no variable T in your code
To test if TT is equal to s use
if TT==s
% or
if abs(TT-s)<=tolerence %or if abs(TT-s)>=tolerence
0 Comments
Matt J
on 29 May 2013
The error message is not being triggered by any of the lines you've shown. The command T=T appears nowhere in your posted code.
I suspect you meant to type this
>>TT,
but accidentally typed this
>>T=T,
0 Comments
Image Analyst
on 29 May 2013
Lots and lots wrong with that code. First of all the line TT=s. It would normally be TT==s, except for the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F so let's get rid of that line totally - you don't even need it because we'll put the stopping condition on the while line, like I'll show you later.
Next, kt is complex number. Is that what you expected? So now TT is complex. That combined with my previous comments suggests something like
n=1;
TT = -inf;
while abs(TT) < s
n=n+1;
j=va/(n*d);
kt=0.392*(1-(j/0.95))^0.8;
TT = kt*density*n^2*d^4
end
but I think that's not what you want. Another thing to do with while loops is to put in a failsafe to prevent runaway infinite loops. Like a check on n, where you'll break if it's greater than a million, or whatever you think is the greatest number n will ever possibly get to.
while abs(TT) < s && n < 1000000
So fix all those things, post your new code, and then let's continue if you're still having problems.
11 Comments
Image Analyst
on 30 May 2013
Edited: Image Analyst
on 30 May 2013
j = 1.385 the first time into the loop. So, if I plug that in for j to the command line, look what that does to kt:
K>> kt=0.392*(1-(1.385/0.95))^0.8
kt =
-0.1698 + 0.1233i
kt is complex!
Ilham Hardy
on 30 May 2013
Yes, you are correct for n = 2, but when n>2, parameter j becomes smaller than 1, which makes kt no longer complex.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!