How to set a tolerance level for equality constraints
You can use Congruent
.
ClearAll[Congruent]
TOL = 0.05;
Congruent[a_, b_] :=
If[Abs[a - b] > 0, Abs[a - b] / Norm[{a, b}, Infinity] <= TOL, True];
Addendum
your answer does not show how I test these equations with Congruent. Can you show me how I use Congruent?
Please note that lines 37 and 39 on the attached screenshot use Congruent
.
It can be seen in the linked function page that the infix symbol of Congruent
("≡") can be entered as "Esc === Esc".
x + y ≡ 250
(* True *)
z + p ≡ 65
(* False *)
Using Block
we can also make bulk evaluations of many equalities.
Block[{Equal = Congruent},
{x + y == 250, z + p == 65}
]
(* {True, False} *)
Some more ways, with the relative error e = 0.05
:
Block[{Internal`$EqualTolerance = MachinePrecision + Log10[e]},
{x + y == 250., z + p == 65.} (* advantage: equations written in terms of == *)
]
(* {True, False} *)
{SetPrecision[x + y, -Log10[e]] == SetPrecision[250, -Log10[e]],
SetPrecision[z + p, -Log10[e]] == SetPrecision[65, -Log10[e]]}
(* {True, False} *)
svn = NDSolve`ScaledVectorNorm[Infinity, {e, 0}];
{svn[{x + y - 250}, {250}] < 1, svn[{z + p - 65}, {65}] < 1}
(* {True, False} *)
ClearAll[choppedEqual]
SetAttributes[choppedEqual, {HoldFirst, Listable}]
choppedEqual[a_ == b_, c_] := Chop[N@(a - b)/a, c] == 0.
Examples:
choppedEqual[x + y == 250, .05]
True
choppedEqual[z + p == 65, .05]
False
choppedEqual[{x + y == 250, z + p == 65}, .05]
{True, False}