NMinimize - function involves a very small number, solution is wrong

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]?

• The question itself is wrong. f[x,y] doesn't have a global minimum. Try e.g. f[1, 10^10] // N Jun 15, 2017 at 10:47
• If +, then it's a simple machine-precision rounding-error problem (due to the 10^-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]. Jun 20, 2017 at 4:37

As @xzczd pointed out, for a minimum to exist the coefficient of the second term should be positive. After doing that, you can improve accuracy by playing with WorkingPrecision, AccuracyGoal, or PrecisionGoal. Here's one example:
f[x_, y_] := (x - 1)^2 + 10^-20 (y - 2)^2

• Accuracy and Precision are relative and not absolute. It's a bit delicate. have a look at this: reference.wolfram.com/language/howto/… Jun 15, 2017 at 16:14
• NMinimize[f[x, y], {x, y}, WorkingPrecision -> 25] Jun 15, 2017 at 18:17
• @paul Also, check this post: mathematica.stackexchange.com/q/118249/1871 I should say after the modification by yohbs, the example becomes interesting. Manual adjustment of AccuracyGoal is useful in this case! Still, I'd like to point out it's not necessary to use a high AccuracyGoal, try NMinimize[f[x, y], {x, y}, WorkingPrecision -> 25, AccuracyGoal -> 1] Jun 16, 2017 at 1:24