NMinimize apparently doesn't worry much about the accuracy of small numbers in the result - They don't contribute much to the overall accuracy. Example:
f[x_, y_] := (x - 1)^2 - 10^-20 (y - 2)^2
NMinimize[f[x, y], {x, y}]
yields {-1.75769*10^-20, {x -> 1., y -> 0.674219}}
rather than {x->1,y->2}
.
Is there a way to fix this, without knowing the nature of f[x,y]
?
f[x,y]
doesn't have a global minimum. Try e.g.f[1, 10^10] // N
$\endgroup$+
, then it's a simple machine-precision rounding-error problem (due to the10^-20
). It doesn't take much to make the second term effectively vanish (adding it doesn't change the value) at machine precision. (See$MachineEpsilon
.) Any working precision of 21 digits or higher gives the correct result:NMinimize[(x - 1)^2 + 10^-20 (y - 2)^2, {x, y}, WorkingPrecision -> 21]
. $\endgroup$