setdiff not working for a particular value, bug?
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
Pelajar UM
2021-12-9
1 个评论
Stephen23
2021-12-9
"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
类别
在 帮助中心 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!