what is wrong with my 'For' loop
조회 수: 2 (최근 30일)
이전 댓글 표시
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
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!