setdiff not working for a particular value, bug?
Mostra commenti meno recenti
Set 1:
R = 1.2;
F = 1.78;
D = 1.29;
M = 0.2
Set 2:
R = 1.2;
F = 2.5;
D = 1.59;
M = 0.5
And here's the function:
V= R/(F*(1/(M)-1)+R);
X = setdiff( 1:0.005:2,D);
err = (abs((((((D-(1-V).*X)./V)).*(X./((((D-(1-V).*X)./V)).*(1./M-1)+X))+(1-(X./((((D-(1-V).*X)./V)).*(1./M-1)+X))).*X)-D)./D)).*100;
[~,imin]=min(err);
New=X(imin);
For set 1, it gives the right value for New = 1.205 but for set 2 it gives New = D = 1.59.
Why is that?
Risposta accettata
Più risposte (1)
Pelajar UM
il 9 Dic 2021
1 Commento
Stephen23
il 9 Dic 2021
"It can be solved by using smaller steps:"
No, that does not solve the problem. If you want to write robust code then you need to avoid testing for exact equivalence of binary floating point numbers (i.e. avoid SETDIFF, EQ, ISMEMBER, etc.)
Instead compare the absolute difference against a tolerance (selected to suit your data):
abs(A-B)<tol
Categorie
Scopri di più su Operating on Diagonal Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!