Numerical minimum of a one-valued function
$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]]]}]
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
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.