5
$\begingroup$

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

$\endgroup$
  • 1
    $\begingroup$ The question itself is wrong. f[x,y] doesn't have a global minimum. Try e.g. f[1, 10^10] // N $\endgroup$ – xzczd Jun 15 '17 at 10:47
  • $\begingroup$ 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]. $\endgroup$ – Michael E2 Jun 20 '17 at 4:37
4
$\begingroup$

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
NMinimize[f[x, y], {x, y}]
(* Output: {1.75769*10^-20, {x -> 1., y -> 0.674219}}*)
NMinimize[f[x, y], {x, y}, AccuracyGoal -> 40]
(* Output: {6.70318*10^-29, {x -> 1., y -> 1.99992}} *)
$\endgroup$
  • $\begingroup$ Thank you very much. It was a bad example, and changing the sign makes it a good example. I will try fixing the more complicated problem with your suggestion, and I am optimistic. I was thinking there was some option I could set to specify a relative rather than absolute error to each result. $\endgroup$ – Paul R. Jun 15 '17 at 16:04
  • $\begingroup$ Accuracy and Precision are relative and not absolute. It's a bit delicate. have a look at this: reference.wolfram.com/language/howto/… $\endgroup$ – yohbs Jun 15 '17 at 16:14
  • 1
    $\begingroup$ NMinimize[f[x, y], {x, y}, WorkingPrecision -> 25] $\endgroup$ – Bob Hanlon Jun 15 '17 at 18:17
  • $\begingroup$ @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] $\endgroup$ – xzczd Jun 16 '17 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.