what is wrong with my 'For' loop

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

Answers (2)

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 Comment

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

Sign in to comment.

Mark
Mark on 4 Jun 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 Comments

ameen
ameen on 4 Jun 2013
Edited: ameen on 4 Jun 2013
i want to calculate the 11 value of n in one step
Mark
Mark on 4 Jun 2013
Edited: Mark on 4 Jun 2013
The value of n is the index of the loop. Do you mean the 11 values of TT? Then make TT an array with size 100x11, and then TT(n,:) = kt*density*(n./60).^2*d^4; This will store the vectors as rows of TT.
But you still have a problem with the if statement. What are you trying to compare to 0.9988? Do all the values of TT/s have to be bigger than 0.9988 or just the biggest one? If it's just the max, then you could do if max(TT(n,:)./s)>0.9988
if it's all 11 of them that have to be bigger than 0.9988, use the min function instead of max.
i want to calculate n when TT = s
but the problem it will not equal exactly, that's why i used TT(n)/s=0.9988
with the first code, it calculates one (n) at one (v)
i am trying in the second code to calculate number of (n) at number of (v)
Then you may be better off looping through each index of v and finding each n individually, since the n values at which TT=s may be different. There doesn't seem to be much point calculating all 11 columns at the same time unless you also want to store all the TT values for later use.
how to off looping for each index of (v)?? and of course i may need to store (TT) values at every (v)
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.
Thank you Mark very much

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 4 Jun 2013

Community Treasure Hunt

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

Start Hunting!