While loop stops interating before end condition is met
11 views (last 30 days)
Show older comments
Hi all,
I'm facing a weird problem in Matlab R2017b. All I do is checking the difference of some data and iterate as long as this difference meets exactly the initial difference of my data (in this case 0.001). See my simplified code below.
% My data
Data = [0;0.00100000000000000;0.00200000000000000;0.00300000000000000;0.00400000000000000;0.00500000000000000;...
0.00600000000000000;0.00700000000000000;0.00800000000000000;0.00900000000000000;0.0100000000000000;0.0120000000000000];
% The initial difference
Samplingrate = Data(2,1)-Data(1,1);
% Take initial differece as comparison value for while-loop
Diff_XValue = Samplingrate;
% Counter for while-loop/data
counter = 1;
% While-loop
while Diff_XValue == Samplingrate % Check if they are the same
counter = counter + 1;
Diff_XValue = Data(counter + 1,1)-Data(counter,1); % next difference
end
% Failed?
if counter < 11
disp('Failed')
else
disp('Yay')
end
As you can see in data there is always a delta of 0.001, except for counter = 11, so you would expect the while loop to end when counter = 11 is reached. That's not the case here. The while-loop ends at counter = 9. But why? I thought 'maybe it is a numerical issue' and changed my while condition as follows...
while (Diff_XValue < 1.0005*Samplingrate) && (Diff_XValue > 0.9995*Samplingrate)
counter = counter + 1;
Diff_XValue = Data(counter + 1,1)-Data(counter,1);
end
This way everything works fine. Again, but why? Are the decimal zeros a problem to Matlab? But even deleting them doesn't change this behaviour. Any ideas?
Leon
Edit: Yep it's a numerical issue. At counter = 9 Diff_XValue is not exactly 0.001. But I don't get why it's working in the previous iterations...
0 Comments
Accepted Answer
Guillaume
on 23 May 2019
there is always a delta of 0.001, except for counter = 11, so you would expect the while loop to end when counter = 11 is reached
No I wouldn't, particularly with a delta of 0.001 which is a number that doesn't exist exactly in floating point binary.
When dealing with floating point numbers, particularly those that result from iterative summation, never ever compare them with ==. Computers don't do exact maths. They have finite storage and thus small errors accumulate. Always compare the difference between the number to an arbitrarily small value (whose magnitude depends on the numbers you want to compare), e.g.
while abs(Diff_XValue - SamplingRate) <= 1e-10 %since the magnitude of your numbers is around 1e-3, a difference of 1e-10 can be considered 0.
end
See FAQ: Why is 0.3 - 0.2 - 0.1 not equal to zero? and many more posts on the internet (not specific to matlab) on floating point arithmetic.
Note that:
Data = [0;0.00100000000000000;0.00200000000000000;0.00300000000000000;0.00400000000000000;0.00500000000000000;...
0.00600000000000000;0.00700000000000000;0.00800000000000000;0.00900000000000000;0.0100000000000000;0.0120000000000000];
I don't know if the notation is because you're trying to avoid precision error, but the above is exactly identical to:
Data = [0;0.001;0.002;0.003;0.004;0.005;0.006;0.007;0.008;0.009;0.010;0.012];
which is a lot easier to read (and type). The trailing zeros make absolutely no difference to what is stored in memory.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!