what is wrong with my 'While' function
Afficher commentaires plus anciens
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'.'
Réponse acceptée
Plus de réponses (4)
Azzi Abdelmalek
le 29 Mai 2013
Modifié(e) : Azzi Abdelmalek
le 29 Mai 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
Matt J
le 29 Mai 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,
Image Analyst
le 29 Mai 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 commentaires
ameen
le 30 Mai 2013
Iain
le 30 Mai 2013
On what line is the error being thrown?
Image Analyst
le 30 Mai 2013
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
le 30 Mai 2013
Image Analyst
le 30 Mai 2013
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
le 30 Mai 2013
Image Analyst
le 30 Mai 2013
But what does it mean when TT is a complex number a + i * b?
Ilham Hardy
le 30 Mai 2013
Modifié(e) : Ilham Hardy
le 30 Mai 2013
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
le 30 Mai 2013
Modifié(e) : Image Analyst
le 30 Mai 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
le 30 Mai 2013
Yes, you are correct for n = 2, but when n>2, parameter j becomes smaller than 1, which makes kt no longer complex.
ameen
le 30 Mai 2013
0 votes
Catégories
En savoir plus sur Parallel Computing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!