For loope small increments

조회 수: 5 (최근 30일)
marvin corado
marvin corado 2011년 3월 11일
I cannot seem to find the solution to this problem. I write a for loop and i tell it to increment by a small value, but instead of starting at 1, it starts at 2.9110 here is the loop
for m=1:.001:4
m
end
does anyone know what its going on? i expect it to start at 1 and increment by .001 every time so that it goes 1.001, 1.002, 1.003 and so on thank you

채택된 답변

Matt Fig
Matt Fig 2011년 3월 11일
I don't know why you think this starts at 2.910. Perhaps you are running into the limit of your command window when trying to scroll back up after running the loop. Do this:
for m=1:.001:4
m
pause(.5)
end
Now you should see m incrementing like you expect as it prints slowly out.
  댓글 수: 2
Paulo Silva
Paulo Silva 2011년 3월 11일
Matt that code might freak out someone who doesn't know how to stop it (CTRL+C in MSWIN OS)
Matt Fig
Matt Fig 2011년 3월 11일
True, but I thought everyone know about ctrl+c!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Paulo Silva
Paulo Silva 2011년 3월 11일
It works fine here, try this
m=1:.001:4;
What's the first value of the vector m?

marvin corado
marvin corado 2011년 3월 11일
yes matt you are right, since the answer was printed in the command window and it always started at 2.910, but when i add the puase it starts at 1 and increments as i expect. thankfully i did know about ctrl_c :)

marvin corado
marvin corado 2011년 3월 11일
ok but my problem seems to be something else then because my code is supposed to increment M starting at 2 and increment it .0001 up to 3. here is my code
f=0.006;
M= 3;
gamma = 1.4;
L_D=50;
Mx=1;
tol=1e2;
for Mx=2:.0001:3
a =(1-(M^2))/(gamma*M^2);
b = (gamma+1)/(2*gamma);
c = ((gamma+1)*(M^2))/(2+((gamma-1)*M^2));
%friction parameter coefficient for m1
Fp1 = a+(b*log(c));
%guessing
%friction parameter coefficient for guess
a1 =(1-(Mx^2))/(gamma*Mx^2);
b1 = (gamma+1)/(2*gamma);
c1 = ((gamma+1)*(Mx^2))/(2+((gamma-1)*Mx^2));
Fpx = a1+(b1*log(c1));
%Lx = L1star-Lxstar
Fpg=Fp1-Fpx;
%doing table A2 Normal shock
num=1+(((gamma-1)/2)*Mx^2);
den= (gamma*Mx^2)-((gamma-1)/2);
my= sqrt(num/den);
%friction parameter coefficient normal shock
a2 =(1-(my^2))/(gamma*my^2);
b2 = (gamma+1)/(2*gamma);
c2 = ((gamma+1)*(my^2))/(2+((gamma-1)*my^2));
Fpy = a2+(b2*log(c2));
%checking Lx+Ly* = L
X1=Fpg+Fpy;
R=4*f*L_D;
end
if abs(X1-R)<tol
z=Fpy
a=Fpg
a1=a/(4*f)
end
break
but that if statement does not happen for 2:.0001:3 but it happens for 2.5:.0001:3 do you guys know what is going on? thank you
  댓글 수: 1
Matt Fig
Matt Fig 2011년 3월 11일
Please select a best answer for this question, then start a new one. That way people can search Answers for a specific issue they are having.

댓글을 달려면 로그인하십시오.


marvin corado
marvin corado 2011년 3월 11일
should i start a new one? with my question above?
  댓글 수: 1
Matt Fig
Matt Fig 2011년 3월 11일
Yes, that way we keep one Question per topic.

댓글을 달려면 로그인하십시오.

카테고리

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