MATLAB: For Loop Not Executing

for loop

Hi all!
Consider the following example:
h=(2/1000);
for i=8*h+h:h:9*h
i
end
i =
0.0180
And similarly,
>> for i=11*h+h:h:12*h
i
end
i =
0.0240
But when I do this:
>> for i=9*h+h:h:10*h
i
end
it doesn't enter the for loop! What is going on? Thanks for the help!

Best Answer

Gopal, this is because
9*h+h - 10*h
ans =
3.469446951953614e-18
and
11*h+h - 12*h
ans =
0
Just because the terms above are all symbolically equal to zero, does not necessarily mean they are also numerically equal to zero. The first expression is not which is why the loop does not get executed.
Star Strider provides more detail below.