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!