8
$\begingroup$

Bug introduced in 12.0 [CASE:4332003]


My problem is that the Kernel cannot finish computation and eats up memory when a simple constraint like 0 <= x <= 2 is specified in FindMinimum.

I have the function

f[x_] := 
  7/(5 Sqrt[5 Pi] + 2 Sqrt[11 Pi]) (2/7 Exp[-(x - 3)^2/11] + 5/7 Exp[-(x + 2)^2/5])
Plot[f[x], {x, -10, 10}]

enter image description here

I would like to find the local minimum near 1.95, and the two local maxima. For the maxima, the following works:

FindMaximum[f[x], {x, 3}]
FindMaximum[f[x], {x, -3}]

For the minimum, however, the method seems to be highly sensitive to the starting value: with FindMinimum[f[x], {x, 0}] the minimum is found, but with FindMinimum[f[x], {x, 1.9}] or any other value close to the local minimum, I end up with a large value of x (and a value of f[x] close to 0, of course).

I tried to add a constraint, with FindMinimum[{f[x], 1 <= x <= 2}, {x, 1.9}], but Mathematica takes forever, eats up gigabytes of memory, and I had to halt the execution.

I would like to know what I do wrong. There is the alternative of differentiating and using FindRoot which works well, but I think I am probably doing something wrong with FindMinimum. What should I do?

$\endgroup$
  • 1
    $\begingroup$ @m_goldberg Why you removed my Bug header? I've checked with previous versions and found that the problem is introduced in version 12.0. $\endgroup$ – Alexey Popkov Sep 5 at 6:10
  • $\begingroup$ @AlexeyPopkov. I thought that a bugs would be sufficient. If that kind of header is now in vogue, please restore it. $\endgroup$ – m_goldberg Sep 6 at 2:59
  • 2
    $\begingroup$ @m_goldberg The consensus about the bug header is here already for a while, please see "Standard header for bugs-tagged posts, for easy searching." and also discussion in comments there. I restore the header. $\endgroup$ – Alexey Popkov Sep 6 at 3:43
  • $\begingroup$ Reported to the support as [CASE:4332003]. $\endgroup$ – Alexey Popkov Oct 15 at 15:13
  • $\begingroup$ This is quite fast NMinimize[{f[x], 0 < x < 3}, x, Method -> "DifferentialEvolution"] $\endgroup$ – OkkesDulgerci Oct 15 at 15:33
6
$\begingroup$
$Version

"12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)"

f[x_] := 7/(5 Sqrt[5 Pi] + 2 Sqrt[11 Pi]) (2/7 Exp[-(x - 3)^2/11] + 
     5/7 Exp[-(x + 2)^2/5]) // FullSimplify

For FindMinimum use the WorkingPrecision option

min = FindMinimum[{f[x], 1 < x < 3}, {x, 2}, WorkingPrecision -> 20]

(* {0.064291094806372406402, {x -> 1.9667863700044219133}} *)

maxg = FindMaximum[f[x], {x, -3}]

(* {0.165184, {x -> -1.89931}} *)

maxl = FindMaximum[{f[x], 2 < x < 5}, {x, 7/2}]

(* {0.0647397, {x -> 2.66797}} *)

Plot[f[x], {x, -10, 10},
 PlotStyle -> LightGray,
 Epilog ->
  {AbsolutePointSize[3], Red, Point[{x, f[x]} /. {maxg, maxl}[[All, 2]]],
   Blue, Point[{x, f[x]} /. min[[2]]]}]

enter image description here

$\endgroup$
  • $\begingroup$ Works like a charm with WorkingPrecision. That's what I was missing, thanks! $\endgroup$ – user64074 Sep 4 at 14:36
6
$\begingroup$

For most functions of a single variable using standard numerical functions, you can use Solve (or NSolve) to find the zeros of the derivative of the function (within a specified interval), and then use the second derivative test to determine whether the zeros are minima or maxima (I will use NSolve):

sol = NSolve[f'[x] == 0 && -10 < x < 10 && f''[x] > 0, x]

{{x -> 1.96679}}

The minimum value:

f[x] /. First @ sol

0.0642911

$\endgroup$
5
$\begingroup$

I tried to add a constraint, with FindMinimum[{f[x], 1 <= x <= 2}, {x, 1.9}], but Mathematica takes forever, eats up gigabytes of memory, and I had to halt the execution.

I think I am probably doing something wrong with FindMinimum. How should I do?

You are doing everything right, it is just a bug in FindMinimum introduced in version 12.0. Please report it to the support. Note that in version 11.3 it works as expected and returns the answer about 20 times faster than version 12.0 with the workaround given below.

A Workaround

The Documentation page "Numerical Nonlinear Local Optimization" says:

Currently, the only method available for constrained optimization is the interior point algorithm.

If we specify this method explicitly, Mathematica returns quickly using either of the two documented ways to specify the constraints:

FindMinimum[f[x], {x, 1.9, 0, 2}, Method -> "InteriorPoint"]
{0.0642912, {x -> 1.96117}}
FindMinimum[{f[x], 0 <= x <= 2}, {x, 1.9}, Method -> "InteriorPoint"]
{0.0642912, {x -> 1.96097}}

Note that the first method is slightly faster.

$\endgroup$

Your Answer

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