what is wrong with my 'For' loop

조회 수: 2 (최근 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에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by