what is wrong with my 'For' loop

1 次查看(过去 30 天)
ameen
ameen 2013-6-4
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

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-6-4
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 个评论
ameen
ameen 2013-6-4
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

请先登录,再进行评论。


Mark
Mark 2013-6-4
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 个评论
Mark
Mark 2013-6-4
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 2013-6-5
Thank you Mark very much

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by