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'.'
采纳的回答
更多回答(4 个)
Azzi Abdelmalek
2013-5-29
编辑:Azzi Abdelmalek
2013-5-29
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
Matt J
2013-5-29
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,
Image Analyst
2013-5-29
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 个评论
ameen
2013-5-30
Iain
2013-5-30
On what line is the error being thrown?
Image Analyst
2013-5-30
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.
ameen
2013-5-30
Image Analyst
2013-5-30
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.
ameen
2013-5-30
Image Analyst
2013-5-30
But what does it mean when TT is a complex number a + i * b?
Ilham Hardy
2013-5-30
编辑:Ilham Hardy
2013-5-30
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 :)
Image Analyst
2013-5-30
编辑:Image Analyst
2013-5-30
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
2013-5-30
Yes, you are correct for n = 2, but when n>2, parameter j becomes smaller than 1, which makes kt no longer complex.
类别
在 帮助中心 和 File Exchange 中查找有关 Parallel Computing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!