MATLAB: Conditional loop to loop until result is an integer.

loopwhile loop

I am trying to create a script that will give me the value for an integer 'n' such that 2n/k = a where a is also an integer. K varies from 0.01 to 1 in increments of 0.01. I am then using this result to find delta where delta = 2*pi*n. The first few results are correct when verified with a calculator. The 6th and 7th gives results 9 and 77 as the lowest value for n for k=0.06 and 0.07 where a is an integer however from a calculator it can be seen that the values 3 and 7 for n would also return an integer. For k=0.12 the script cannot find a value for n and runs infinitely despite the fact that n=3 would an integer value for a (2*3/0.12 = 50)
Currently I have
k(1)=0.01;
n(1)=1;
for i=1:100;
a(i)=(2*n(i))/k(i);
while floor(a(i))~=ceil(a(i))
n(i)=n(i)+1;
a(i)=(2*n(i))/k(i);
end
deltamax(i)=2*pi*n(i);
k(i+1)=k(i)+0.01;
n(i+1)=1;
end

Best Answer

As it is currently implemented that algorithm will not work because it does not take into account the behavior of binary floating point numbers. Most of those fractional values you are working with cannot be represented exactly: in general any operations on them will accumulate floating point error. If you want to see the actual value of 0.12 (or any of the other fractional values) then download this function:
or (depending on your OS and MATLAB version) you can try SPRINTF/FPRINTF:
fprintf('%.42f\n',0.12)
0.119999999999999995559107901499373838305473
Some workarounds to this are that you could:
  1. use symbolic mathematics, or
  2. allow for some tolerance in your numeric calculations and logical operations, or
  3. adjust the algorithm to work only with integer values, e.g. here multiplied by 100 so all values are integers:
k = 12; % 0.12
n = 100; % 1
while mod(2*n/k,10)
n = n+100;
end
n = n/100
n = 3