Effacer les filtres
Effacer les filtres

what is wrong with my 'For' loop

1 vue (au cours des 30 derniers jours)
ameen
ameen le 4 Juin 2013
this is my FOR loop code
clear all
clc
close all
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);
n=100;
s=rr/(1-t);
for n=100:12000;
j=va/(n*d);
kt=0.392*(1-(j/0.95))^0.8;
TT(n)=kt*density*(n./60)^2*d^4;
if TT(n)/s>0.9988
n
TT(end)
s
return
end
end
and it works just fine, but when i make the input which is v to be a vector from 0.5:0.1:1.5 it is not working and give me that message error
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in stupidfile (line 17)
TT(n)=kt*density*(n./60).^2*d^4;
and this is the code,
clear all
clc
close all
v=0.5:0.1:1.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);
n=100;
s=rr/(1-t);
for n=100:12000;
j=va/(n*d);
kt=0.392*(1-(j/0.95)).^0.8;
TT(n)=kt*density*(n./60).^2*d^4;
if TT(n)/s>0.9988
n
TT(end)
s
return
end
end
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 4 Juin 2013
Avoid using clear all

Connectez-vous pour commenter.

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 4 Juin 2013
clc
close all
v=[0.2 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);
n=100;
s=rr/(1-t);
TT=[];
for n=100:12000;
j=va/(n*d);
kt=0.392*(1-(j/0.95)).^0.8;
TT(end+1,:)=kt*density*(n./60).^2*d.^4;
if TT(end,:)/s>0.9988
n
TT(end,:)
s
return
end
end
  1 commentaire
ameen
ameen le 4 Juin 2013
Thank you but when i run this code it gives me one results only while i want for every (v) the corresponding (n) so i want to calculate n for v from 0.2 to 1.5

Connectez-vous pour commenter.


Mark
Mark le 4 Juin 2013
In your loop, the calculation: kt*density*(n./60).^2*d^4 gives you a vector (1x11 in your case). You are trying to assign this vector to the nth index of TT. MATLAB can't do that. You either have to loop through the elements, or use some command to select one of the elements to put into TT(n), like max(), etc.
Also in the following line, TT(n)/s will have problems because you are dividing a constant by a vector.
  7 commentaires
Mark
Mark le 4 Juin 2013
Are you trying to store TT values for all n, or just the n from 100 until it reaches TT=0.9988s? If it's storage for all n, just run the n loop all the way to 12000 using TT(n,:)=kt*... as the array, and then you can use a loop on the columns of TT to see where TT surpassed 0.9988s in each column:
for col = 1:size(v,2)
nEq(col) = find(TT(:,col)>0.9988*s,1)
end
The vector nEq will be 11 numbers, giving the values of n that correspond to when TT first was greater than 0.9988s.
ameen
ameen le 5 Juin 2013
Thank you Mark very much

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by