Test of Approximately Equality

조회 수: 40 (최근 30일)
John Miller
John Miller 2012년 6월 16일
댓글: Walter Roberson 2017년 12월 29일
Hello,
I have a value b and c. b and c are approximatly equal (b~c). They have +-5% difference.
I want to test of approximately equality..like this:
if b~c (with +-5% difference)
return something...
end

답변 (3개)

Jan
Jan 2012년 6월 17일
You only have to decide if it should be 5% of the first or the second argument. Then:
if abs(b - c) < 0.05 * b
or
if abs(b - c) < 0.05 * max(b, c)
or
if abs(b - c) < 0.05 * min(b, c)
or
if abs(b - c) < 0.05 * (b + c) * 0.5
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 6월 17일
Some of those expressions will have difficulty if "b" is negative.
Greg Heath
Greg Heath 2012년 6월 18일
1. Try using "abs" on the RHS
2. Might also consider median(abs(b),abs(c))
Greg

댓글을 달려면 로그인하십시오.


Stephen Nagy
Stephen Nagy 2017년 12월 29일
Your desired goal isn't entirely clear, but depending on the outcome you seek, the functions "uniquetol" and some of its related functions might be helpful.
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 12월 29일
ismembertol() and uniquetol() did not exist in 2012 though ;-)

댓글을 달려면 로그인하십시오.


per isakson
per isakson 2012년 6월 16일
See isalmost(a,b,tol) in the File Exchange. There are more.

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by