setdiff not working for a particular value, bug?
Show older comments
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?
Accepted Answer
More Answers (1)
Pelajar UM
on 9 Dec 2021
1 Comment
Stephen23
on 9 Dec 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
Categories
Find more on Operating on Diagonal Matrices 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!