Why loop is not being excluded with the given condition?

1 visualizzazione (ultimi 30 giorni)
I am using the continue function to exclude the nodes of a sine function in matlab. When I start from 0, it is excluding all the nodes. However, when I start from 0.1 (for some reasons), it does not exclude the nodes i.e. 1.5, 2.5, 3, 3.5. Can you tell me what can be the reason and how to deal with it?
F_b = 16000; % N
A_b = 125.66; % mm^2
for cycles = 0:0.1:10
if cycles/0.5 == fix(cycles/0.5)
continue
end
disp(cycles)
end
0.1000 0.2000 0.3000 0.4000 0.6000 0.7000 0.8000 0.9000 1.1000 1.2000 1.3000 1.4000 1.6000 1.7000 1.8000 1.9000 2.1000 2.2000 2.3000 2.4000 2.6000 2.7000 2.8000 2.9000 3.1000 3.2000 3.3000 3.4000 3.6000 3.7000 3.8000 3.9000 4.1000 4.2000 4.3000 4.4000 4.6000 4.7000 4.8000 4.9000 5.1000 5.2000 5.3000 5.4000 5.6000 5.7000 5.8000 5.9000 6.1000 6.2000 6.3000 6.4000 6.6000 6.7000 6.8000 6.9000 7.1000 7.2000 7.3000 7.4000 7.6000 7.7000 7.8000 7.9000 8.1000 8.2000 8.3000 8.4000 8.6000 8.7000 8.8000 8.9000 9.1000 9.2000 9.3000 9.4000 9.6000 9.7000 9.8000 9.9000
The other is
F_b = 16000; % N
A_b = 125.66; % mm^2
for cycles = 0.1:0.1:10
if cycles/0.5 == fix(cycles/0.5)
continue
end
disp(cycles)
end
0.1000 0.2000 0.3000 0.4000 0.6000 0.7000 0.8000 0.9000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.1000 4.2000 4.3000 4.4000 4.6000 4.7000 4.8000 4.9000 5.1000 5.2000 5.3000 5.4000 5.6000 5.7000 5.8000 5.9000 6.1000 6.2000 6.3000 6.4000 6.6000 6.7000 6.8000 6.9000 7.1000 7.2000 7.3000 7.4000 7.6000 7.7000 7.8000 7.9000 8.1000 8.2000 8.3000 8.4000 8.6000 8.7000 8.8000 8.9000 9.1000 9.2000 9.3000 9.4000 9.6000 9.7000 9.8000 9.9000
Thanks for the help in advance.
Regards,
Muhammad Hassaan Bin Tariq
  2 Commenti
Stephen23
Stephen23 il 18 Ott 2022
"Can you tell me what can be the reason..."
Because 0.1 cannot be exactly stored in a binary floating point number (in exactly the same way that you cannot write 1/3 exactly on a piece of paper using a decimal fraction). The floating point error accumulates differently in the COLON operator, depending on the provided values. Lets compare:
cycles0 = 0:0.1:10;
cycles1 = 0.1:0.1:10;
fprintf('%.40f\n', cycles0(16)/0.5, fix(cycles0(16)/0.5), cycles1(15)/0.5, fix(cycles1(15)/0.5))
3.0000000000000000000000000000000000000000 3.0000000000000000000000000000000000000000 3.0000000000000004440892098500626161694527 3.0000000000000000000000000000000000000000
"...and how to deal with it?"
Either:
  • work with integers, or
  • use tolerances when comparing (i.e. do not use exact equality), avoid FIX, etc.
Your current approach is numerically fragile and should be avoided.
Muhammad Hassaan Bin Tariq
I got your point but why is it avoiding higher points with the same fix command? I mean it is excluding 7, 7.5, 8 etc.
It is a long code. Well the mod() operation is working well.
Thanks for your help very much.
Regards

Accedi per commentare.

Risposta accettata

Tobias Panitz
Tobias Panitz il 18 Ott 2022
Hey,
have you tried using the modulus function?
if mod(cycles,0.5) == 0
continue;
end

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by