MATLAB: What is wrong with the ‘While’ function

while loop

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

Best Answer

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