MATLAB: Didn’t I get “Hello” word by running following script? if I change the value in if loop except 13.2 it gives me correct answer.

hellomaterrorMATLAB

clc
display('code executing');
for number=1:0.1:15
if number == 13.2
disp('Hello');
end
end

Best Answer

clc
tol = 10^-3 ;
display('code executing');
for number=1:0.1:15
if abs(number-13.2)<=tol
disp('Hello');
end
end
You should not compare flottant numbers using ==..you should subtract and check is the absolute value of difference is less then a specified tolerance.