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!