what is wrong with my 'While' function

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'.'

 采纳的回答

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

更多回答(4 个)

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
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,
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 个评论

this is my new code and i still have the same error message when i run it
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;
break
end
end
The error message is ' T=T
Undefined function or variable 'T'.'
although i don't have a variable named (T)
On what line is the error being thrown?
You didn't read my answer, did you? First of all, there is no T=T line in your code. Secondly, you still have the TT==s line, which shows you didn't read the FAQ link I gave you. Third, kt and TT are still complex numbers. Please read my answer and try again.
i wrote the code in a new script
when i press run
i have this error in the command window
No you don't, because I simply copied and pasted and it ran through 12000 iterations and quit, thanks to the one suggestion of mine that you did take which was to put in a failsafe (n<12000) - otherwise you would have got into an infinite loop and hung the computer. I ran the exact code you put there and there was no error whatsoever.
when i run it form the new script i have an error message in the command window
when i copied it and paste in the command window there is no error message but the i found that TT==s as i want and the value of n is 12000
what i want from this code is 'increasing (n) until TT=s and then giving me the value of (n) and (TT)
But what does it mean when TT is a complex number a + i * b?
ameen
ameen 2013-5-30
编辑:ameen 2013-5-30
i don't want it complex number that's why i fixed by putting while 100<n<12000;
IA,
I think the i and j is a real number. the parameters name might be confusing though.
Edit: He is missing the line
j=va/(n*d);
Edit: No, he is not :)
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!
Yes, you are correct for n = 2, but when n>2, parameter j becomes smaller than 1, which makes kt no longer complex.

请先登录,再进行评论。

ameen
ameen 2013-5-30
Thank you all very much for your help and replys

类别

帮助中心File Exchange 中查找有关 Parallel Computing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by