3

I am trying to find the global minimum of this simple function for real x and y

f = x^2*(1 + Sin[y]);

Of course, I know it is 0. But Mathematica seems not be able to handle this expression since

MinValue[f, {x, y}, Reals]

only returns itself (so Mathematica was not able to find a solution with its rules). As far as I have read, this is due to the capabilities of Minimize and its buddies (MinValue, ...) only to handle polynomial expressions, right? I have read this questions

Simple minimization not evaluating

Mathematica: commands return no output, but itself. Bug?

Question: are there any further general tricks or further specifications you can use for non-polynomial expressions without constraints? Asking for a vanishing derivative is naturally a good option here. But for uglier high-dimensional expressions (with a lot of minima and maxima), it would still be needed to check the second derivative for positive definiteness and so on.

I am actually only interested on the minimal value (or infimum), not on all possible spots for it. I have naturally a more complicated expression, but I wanted to just discuss a simplified problem. I tried some non-polynomial expressions with Maple (yuck!) and, sadly, it works. But before I write my complete code and problem in Maple I just wanted to ask. Thanks!

EDIT 1 (2015-10-26): My actual function is

f = 10381/65536 + (105 Cos[4 nphi])/16384 + (6125 Cos[8 nphi])/
    196608 + (1225 Cos[4 nphi - 8 nth])/
    98304 + (175 Cos[8 nphi - 8 nth])/
    393216 - (175 Cos[4 nphi - 6 nth])/
    4096 - (175 Cos[8 nphi - 6 nth])/
    49152 + (1295 Cos[4 nphi - 4 nth])/
    24576 + (1225 Cos[8 nphi - 4 nth])/
    98304 - (105 Cos[4 nphi - 2 nth])/
    4096 - (1225 Cos[8 nphi - 2 nth])/49152 + (2485 Cos[2 nth])/
    24576 + (2345 Cos[4 nth])/16384 + (2275 Cos[6 nth])/
    24576 + (5775 Cos[8 nth])/65536 - (105 Cos[4 nphi + 2 nth])/
    4096 - (1225 Cos[8 nphi + 2 nth])/
    49152 + (1295 Cos[4 nphi + 4 nth])/
    24576 + (1225 Cos[8 nphi + 4 nth])/
    98304 - (175 Cos[4 nphi + 6 nth])/
    4096 - (175 Cos[8 nphi + 6 nth])/
    49152 + (1225 Cos[4 nphi + 8 nth])/
    98304 + (175 Cos[8 nphi + 8 nth])/
    393216 + (1155 Cos[4 nphi - 4 th])/
    32768 + (175 Cos[8 nphi - 4 th])/
    393216 + (35 Cos[4 nphi - 8 nth - 4 th])/
    196608 + (5 Cos[8 nphi - 8 nth - 4 th])/
    786432 + (35 Cos[4 nphi - 6 nth - 4 th])/
    8192 - (5 Cos[8 nphi - 6 nth - 4 th])/
    98304 - (105 Cos[4 nphi - 4 nth - 4 th])/
    16384 + (35 Cos[8 nphi - 4 nth - 4 th])/
    196608 - (385 Cos[4 nphi - 2 nth - 4 th])/
    24576 - (35 Cos[8 nphi - 2 nth - 4 th])/
    98304 + (525 Cos[2 nth - 4 th])/
    16384 - (385 Cos[4 nphi + 2 nth - 4 th])/
    24576 - (35 Cos[8 nphi + 2 nth - 4 th])/
    98304 + (5705 Cos[4 nth - 4 th])/
    98304 - (105 Cos[4 nphi + 4 nth - 4 th])/
    16384 + (35 Cos[8 nphi + 4 nth - 4 th])/
    196608 - (415 Cos[6 nth - 4 th])/
    49152 + (35 Cos[4 nphi + 6 nth - 4 th])/
    8192 - (5 Cos[8 nphi + 6 nth - 4 th])/
    98304 + (165 Cos[8 nth - 4 th])/
    131072 + (35 Cos[4 nphi + 8 nth - 4 th])/
    196608 + (5 Cos[8 nphi + 8 nth - 4 th])/
    786432 - (315 Cos[4 nphi - 3 th])/4096 - (175 Cos[8 nphi - 3 th])/
    49152 - (35 Cos[4 nphi - 8 nth - 3 th])/
    24576 - (5 Cos[8 nphi - 8 nth - 3 th])/
    98304 - (35 Cos[4 nphi - 6 nth - 3 th])/
    2048 + (5 Cos[8 nphi - 6 nth - 3 th])/
    12288 + (245 Cos[4 nphi - 4 nth - 3 th])/
    6144 - (35 Cos[8 nphi - 4 nth - 3 th])/
    24576 + (35 Cos[4 nphi - 2 nth - 3 th])/
    2048 + (35 Cos[8 nphi - 2 nth - 3 th])/
    12288 - (245 Cos[2 nth - 3 th])/
    6144 + (35 Cos[4 nphi + 2 nth - 3 th])/
    2048 + (35 Cos[8 nphi + 2 nth - 3 th])/
    12288 - (315 Cos[4 nth - 3 th])/
    4096 + (245 Cos[4 nphi + 4 nth - 3 th])/
    6144 - (35 Cos[8 nphi + 4 nth - 3 th])/
    24576 + (205 Cos[6 nth - 3 th])/
    6144 - (35 Cos[4 nphi + 6 nth - 3 th])/
    2048 + (5 Cos[8 nphi + 6 nth - 3 th])/
    12288 - (165 Cos[8 nth - 3 th])/
    16384 - (35 Cos[4 nphi + 8 nth - 3 th])/
    24576 - (5 Cos[8 nphi + 8 nth - 3 th])/
    98304 + (245 Cos[4 nphi - 2 th])/8192 + (1225 Cos[8 nphi - 2 th])/
    98304 + (245 Cos[4 nphi - 8 nth - 2 th])/
    49152 + (35 Cos[8 nphi - 8 nth - 2 th])/
    196608 + (35 Cos[4 nphi - 6 nth - 2 th])/
    2048 - (35 Cos[8 nphi - 6 nth - 2 th])/
    24576 - (805 Cos[4 nphi - 4 nth - 2 th])/
    12288 + (245 Cos[8 nphi - 4 nth - 2 th])/
    49152 + (175 Cos[4 nphi - 2 nth - 2 th])/
    6144 - (245 Cos[8 nphi - 2 nth - 2 th])/
    24576 - (455 Cos[2 nth - 2 th])/
    12288 + (175 Cos[4 nphi + 2 nth - 2 th])/
    6144 - (245 Cos[8 nphi + 2 nth - 2 th])/
    24576 - (1505 Cos[4 nth - 2 th])/
    24576 - (805 Cos[4 nphi + 4 nth - 2 th])/
    12288 + (245 Cos[8 nphi + 4 nth - 2 th])/
    49152 - (385 Cos[6 nth - 2 th])/
    12288 + (35 Cos[4 nphi + 6 nth - 2 th])/
    2048 - (35 Cos[8 nphi + 6 nth - 2 th])/
    24576 + (1155 Cos[8 nth - 2 th])/
    32768 + (245 Cos[4 nphi + 8 nth - 2 th])/
    49152 + (35 Cos[8 nphi + 8 nth - 2 th])/
    196608 + (35 Cos[4 nphi - th])/4096 - (1225 Cos[8 nphi - th])/
    49152 - (245 Cos[4 nphi - 8 nth - th])/
    24576 - (35 Cos[8 nphi - 8 nth - th])/
    98304 + (35 Cos[4 nphi - 6 nth - th])/
    2048 + (35 Cos[8 nphi - 6 nth - th])/
    12288 + (35 Cos[4 nphi - 4 nth - th])/
    6144 - (245 Cos[8 nphi - 4 nth - th])/
    24576 - (35 Cos[4 nphi - 2 nth - th])/
    2048 + (245 Cos[8 nphi - 2 nth - th])/
    12288 - (35 Cos[2 nth - th])/6144 - (35 Cos[4 nphi + 2 nth - th])/
    2048 + (245 Cos[8 nphi + 2 nth - th])/
    12288 + (35 Cos[4 nth - th])/4096 + (35 Cos[4 nphi + 4 nth - th])/
    6144 - (245 Cos[8 nphi + 4 nth - th])/
    24576 - (245 Cos[6 nth - th])/
    6144 + (35 Cos[4 nphi + 6 nth - th])/
    2048 + (35 Cos[8 nphi + 6 nth - th])/
    12288 - (1155 Cos[8 nth - th])/
    16384 - (245 Cos[4 nphi + 8 nth - th])/
    24576 - (35 Cos[8 nphi + 8 nth - th])/98304 + (5285 Cos[th])/
    24576 + (9275 Cos[2 th])/49152 + (4595 Cos[3 th])/
    24576 + (49325 Cos[4 th])/196608 + (35 Cos[4 nphi + th])/
    4096 - (1225 Cos[8 nphi + th])/
    49152 - (245 Cos[4 nphi - 8 nth + th])/
    24576 - (35 Cos[8 nphi - 8 nth + th])/
    98304 + (35 Cos[4 nphi - 6 nth + th])/
    2048 + (35 Cos[8 nphi - 6 nth + th])/
    12288 + (35 Cos[4 nphi - 4 nth + th])/
    6144 - (245 Cos[8 nphi - 4 nth + th])/
    24576 - (35 Cos[4 nphi - 2 nth + th])/
    2048 + (245 Cos[8 nphi - 2 nth + th])/
    12288 - (35 Cos[2 nth + th])/6144 - (35 Cos[4 nphi + 2 nth + th])/
    2048 + (245 Cos[8 nphi + 2 nth + th])/
    12288 + (35 Cos[4 nth + th])/4096 + (35 Cos[4 nphi + 4 nth + th])/
    6144 - (245 Cos[8 nphi + 4 nth + th])/
    24576 - (245 Cos[6 nth + th])/
    6144 + (35 Cos[4 nphi + 6 nth + th])/
    2048 + (35 Cos[8 nphi + 6 nth + th])/
    12288 - (1155 Cos[8 nth + th])/
    16384 - (245 Cos[4 nphi + 8 nth + th])/
    24576 - (35 Cos[8 nphi + 8 nth + th])/
    98304 + (245 Cos[4 nphi + 2 th])/8192 + (1225 Cos[8 nphi + 2 th])/
    98304 + (245 Cos[4 nphi - 8 nth + 2 th])/
    49152 + (35 Cos[8 nphi - 8 nth + 2 th])/
    196608 + (35 Cos[4 nphi - 6 nth + 2 th])/
    2048 - (35 Cos[8 nphi - 6 nth + 2 th])/
    24576 - (805 Cos[4 nphi - 4 nth + 2 th])/
    12288 + (245 Cos[8 nphi - 4 nth + 2 th])/
    49152 + (175 Cos[4 nphi - 2 nth + 2 th])/
    6144 - (245 Cos[8 nphi - 2 nth + 2 th])/
    24576 - (455 Cos[2 nth + 2 th])/
    12288 + (175 Cos[4 nphi + 2 nth + 2 th])/
    6144 - (245 Cos[8 nphi + 2 nth + 2 th])/
    24576 - (1505 Cos[4 nth + 2 th])/
    24576 - (805 Cos[4 nphi + 4 nth + 2 th])/
    12288 + (245 Cos[8 nphi + 4 nth + 2 th])/
    49152 - (385 Cos[6 nth + 2 th])/
    12288 + (35 Cos[4 nphi + 6 nth + 2 th])/
    2048 - (35 Cos[8 nphi + 6 nth + 2 th])/
    24576 + (1155 Cos[8 nth + 2 th])/
    32768 + (245 Cos[4 nphi + 8 nth + 2 th])/
    49152 + (35 Cos[8 nphi + 8 nth + 2 th])/
    196608 - (315 Cos[4 nphi + 3 th])/4096 - (175 Cos[8 nphi + 3 th])/
    49152 - (35 Cos[4 nphi - 8 nth + 3 th])/
    24576 - (5 Cos[8 nphi - 8 nth + 3 th])/
    98304 - (35 Cos[4 nphi - 6 nth + 3 th])/
    2048 + (5 Cos[8 nphi - 6 nth + 3 th])/
    12288 + (245 Cos[4 nphi - 4 nth + 3 th])/
    6144 - (35 Cos[8 nphi - 4 nth + 3 th])/
    24576 + (35 Cos[4 nphi - 2 nth + 3 th])/
    2048 + (35 Cos[8 nphi - 2 nth + 3 th])/
    12288 - (245 Cos[2 nth + 3 th])/
    6144 + (35 Cos[4 nphi + 2 nth + 3 th])/
    2048 + (35 Cos[8 nphi + 2 nth + 3 th])/
    12288 - (315 Cos[4 nth + 3 th])/
    4096 + (245 Cos[4 nphi + 4 nth + 3 th])/
    6144 - (35 Cos[8 nphi + 4 nth + 3 th])/
    24576 + (205 Cos[6 nth + 3 th])/
    6144 - (35 Cos[4 nphi + 6 nth + 3 th])/
    2048 + (5 Cos[8 nphi + 6 nth + 3 th])/
    12288 - (165 Cos[8 nth + 3 th])/
    16384 - (35 Cos[4 nphi + 8 nth + 3 th])/
    24576 - (5 Cos[8 nphi + 8 nth + 3 th])/
    98304 + (1155 Cos[4 nphi + 4 th])/
    32768 + (175 Cos[8 nphi + 4 th])/
    393216 + (35 Cos[4 nphi - 8 nth + 4 th])/
    196608 + (5 Cos[8 nphi - 8 nth + 4 th])/
    786432 + (35 Cos[4 nphi - 6 nth + 4 th])/
    8192 - (5 Cos[8 nphi - 6 nth + 4 th])/
    98304 - (105 Cos[4 nphi - 4 nth + 4 th])/
    16384 + (35 Cos[8 nphi - 4 nth + 4 th])/
    196608 - (385 Cos[4 nphi - 2 nth + 4 th])/
    24576 - (35 Cos[8 nphi - 2 nth + 4 th])/
    98304 + (525 Cos[2 nth + 4 th])/
    16384 - (385 Cos[4 nphi + 2 nth + 4 th])/
    24576 - (35 Cos[8 nphi + 2 nth + 4 th])/
    98304 + (5705 Cos[4 nth + 4 th])/
    98304 - (105 Cos[4 nphi + 4 nth + 4 th])/
    16384 + (35 Cos[8 nphi + 4 nth + 4 th])/
    196608 - (415 Cos[6 nth + 4 th])/
    49152 + (35 Cos[4 nphi + 6 nth + 4 th])/
    8192 - (5 Cos[8 nphi + 6 nth + 4 th])/
    98304 + (165 Cos[8 nth + 4 th])/
    131072 + (35 Cos[4 nphi + 8 nth + 4 th])/
    196608 + (5 Cos[8 nphi + 8 nth + 4 th])/786432;

Just a bunch of sines and cosines. Using NMaxValue I get a value around -0.481481, but naturally I dont know if this is a global minimum.

NMinValue[f, {nth, nphi, th}]
Mauricio Fernández
  • 5,463
  • 21
  • 45
  • 1
    Without bounds of any sort, transcendental objective functions are most often left untouched, even if it can actually be proven that it has one global extremum. – J. M.'s missing motivation Oct 26 '15 at 12:05
  • Ok, thank you. I will try my problem with Maple. – Mauricio Fernández Oct 26 '15 at 12:36
  • Can you give a more realistic example? I can do this one semi-manually with Reduce, but that simple approach might fail with the actual expressions you get. – Szabolcs Oct 26 '15 at 13:54
  • I just added my actual problem function depending on three variables, sorry, a huge bunch of sines and cosines. – Mauricio Fernández Oct 26 '15 at 14:15
  • Do you know that your function has only one global minimum though? Some playing around with NMinimize seems to suggest that multiple (infinite?) equivalent minima exist. Is there a range of variables you could restrict yourself to? – MarcoB Oct 26 '15 at 15:09
  • A global minimum clearly exists, since those are just sines and cosines running between -1 and 1, I would only need to count them to get a lower bound. At which variables the global minimum is reached is not important for me, I am only interested on the value of the global minimum (or infimum). The variables nth, nphi and th have no specific bounds. – Mauricio Fernández Oct 26 '15 at 15:14
  • I think that what @Marco was getting at is that your function is periodic (and may thus achieve the global minimum at infinitely many points), so you may want to consider restricting your search to a "fundamental section" of your objective function to ease the task of searching. – J. M.'s missing motivation Oct 27 '15 at 01:48

2 Answers2

1

I redefined the symbol f to be a function (perhaps not needed but the way I am accustomed to working).

I will only show the first few terms of your large expression copied from your question.

f[nphi_, nth_, th_] := 
 10381/65536 + (105 Cos[4 nphi])/16384 + (6125 Cos[8 nphi])/
   196608 + (1225 Cos[4 nphi - 8 nth])/
   98304 + (175 Cos[8 nphi - 8 nth])/
   393216 - (175 Cos[4 nphi - 6 nth])/4096 - …

The lowest multiplier within the Cos terms for nphi, nth and th are 4, 2 and 2 respectively. Thus it makes sense that we should limit the range to

-π/2 <= nphi <= π/2
-π <= nth <= π
-π <= th <= π

because Cos repeats every 2π

Given constraints NMinValue can locate a minimum:

minValue=NMinValue[{f[x, y, 
   z], -π/2 <= x <= π/2 && -π <= y <= π && -π <= 
    z <= π }, {x, y, z}]

(* -0.481481 *)

Update

One can use FindInstance to extract points which satisfy the equation where the function equals the minimum derived from the solution.

For example here are 10 random results:

FindInstance[f[x, y, z] == min, {x, y, z}, Reals, 10]

giving

{{x -> -233.263, y -> 61.8765, z -> -3.14159}, {x -> -379.347, 
  y -> -26.7035, z -> 306.645}, {x -> 25.9181, y -> 27.8338, 
  z -> 241.317}, {x -> 559.667, y -> 23.9825, 
  z -> -229.336}, {x -> -284.314, y -> -5.81954, 
  z -> -267.876}, {x -> 218.341, y -> -148.762, 
  z -> -291.327}, {x -> 635.065, y -> 80.1106, 
  z -> 2.30052}, {x -> 282.743, y -> -44.7677, 
  z -> -231.247}, {x -> 724.137, y -> 7.06858, 
  z -> -121.291}, {x -> -455.067, y -> -137.08, z -> -304.734}}

Now let's limit the interval to the expected periodicity:

FindInstance[ f[x, y, z] == min &&
 -π/2 <= x <= π/2 && -π <= y <= π &&
     -π <= z <= π, {x, y, z}, Reals, 4]

giving

{{x -> 0.785398, y -> -0.440511, z -> 2.55591}, {x -> -0.785398, 
  y -> 0.61548, z -> 3.14159}, {x -> 0, y -> 0.785398, 
  z -> -1.91063}, {x -> 0.785398, y -> 0.955317, z -> 1.0472}}

We can plot one of the examples holding one parameter constant and allowing the other to vary.

Plot3D[f[x, y, 
  2.555907110132642`], {x, -π/2, π/2}, {y, -π, π}]

Mathematica graphics

If you rotate this you will see that there are a number of points that match the minValue (-0.481481), even within this limited region.

There are no points below minValue.

Here is another example from this group:

Plot3D[f[0, y, z], {y, -π, π}, {z, -π, π}]

Mathematica graphics

It can be seen more clearly if you use a RegionFunction

Plot3D[f[0, y, z], {y, -π, π}, {z, -π, π}, 
 RegionFunction -> Function[{x, y, z}, z < -0.4], 
 PlotRange -> {{ -π, π}, { -π, π}, {-0.5, 1.0}}]

Mathematica graphics

Finally, use FindInstance with a small delta applied to minValue

FindInstance[f[x, y, z] == min - 0.001, {x, y, z}, Reals]
(* {} *)
Jack LaVigne
  • 14,462
  • 2
  • 25
  • 37
  • Thank you. But due to periodicity NMinimize should obtain the same result as I, see last line of my question. I am trying to be careful here: this numerical result still does not mean, that -0.481481 is the global minimum, right? – Mauricio Fernández Oct 28 '15 at 06:08
  • @Mauricio Lobos I don't know how to prove it but given the periodicity of the Cos terms and the form of your function I believe it is the global minimum. I have updated the answer to show some examples and graphics. – Jack LaVigne Oct 29 '15 at 14:45
0

In the first place, thank you for all comments and the answer. I just want to add my brute approach, it's not elegant at all but may be it is useful for someone. Since I have never liked (it's just a personal thing) to search numerically for a global minimum of a function with more than 2 variables (I can never see a thing then), I evaluated the function f over the region of Jack LaVigne (see his answer) with a "high" resolution (5*10^2 points in each direction = 5*10^6 points total). On my computer this took over 9 hours over the night with numerical evaluation and parallelization (code given below). On how to parallelize, see this question:

Parallel function evaluation for minimal value

I saved the exact values of the variables delivering the minimal results on every kernel and extracted the candidates. At least with this, you obtain candidates for some variables, naturally you can restart then within a smaller region around the best candidates of the previous evaluation (or just use NMinValue[f,{nth,nphi,th},Method->"RandomSearch"]). For this specific case, it looks like that {nth -> Pi/4, nphi -> 0} seem to be a good start (see also Jack's answer), at least with that resolution I get good answers around those values. Mathematica is then able to minimize

MinValue[f /. {nth -> Pi/4, nphi -> 0}, th] // FullSimplify
-13/27

Looks like this guy is the global minimum for this specific case since all numerical minimization I have tried deliver a bigger results

-13/27 <= 
 NMinValue[{f, 0 <= nth <= Pi, 0 <= nphi <= 2*Pi, 
   0 <= th <= Pi}, {nth, nphi, th}, WorkingPrecision -> 200]
-13/27 <= 
 NMinValue[{fN, 0 <= nth <= Pi, 0 <= nphi <= 2*Pi, 
   0 <= th <= Pi}, {nth, nphi, th}, 
  Method -> {"RandomSearch", "SearchPoints" -> 10^2}]
True
True

Code for parallel evaluation

(*Create numerical evaluated version of f*)
fN = 1.*f;

(*Resolution*)
n = 10^2*5;

(*First distribute fmin and spot, THEN initialize*)
Clear[fmin, spot]
DistributeDefinitions[fmin, spot];
fmin = fN /. {nth -> 0., nphi -> 0., th -> 0.};
spot = {0, 0, 0};

(*Parallel evaluations and numerical fmin*)
start = DateString[]
ParallelDo[
 ftemp = fN /. {nth -> 1.*xp, nphi -> 1.*yp, th -> 1.*zp};
 If[ftemp < fmin, fmin = ftemp; spot = {xp, yp, zp};];
 , {xp, 0, Pi, Pi/n}
 , {yp, 0, 2*Pi, 2*Pi/n}
 , {zp, 0, Pi, Pi/n}
 , Method -> "CoarsestGrained"
 ]
end = DateString[]
DateDifference[start, end, {"Hour", "Second", "Minute"}]
fmin = ParallelEvaluate[fmin] // Min

(*Extract analytical results from kernels and present results*)
cand = ParallelEvaluate[spot];
fcand = Table[
    f /. {nth -> cand[[i, 1]], nphi -> cand[[i, 2]], 
      th -> cand[[i, 3]]}, {i, Length[cand]}] // FullSimplify;
fcandmin = Min[fcand];
fcandminpos = Position[fcand, fcandmin] // Flatten;
candmin = cand[[fcandminpos]];
Grid[
 {
  {"Candidate(s): ", candmin}
  , {"Analytical result: ", fcandmin}
  , {"Numerical result: ", N[fcandmin]}
  }
 , Alignment -> Left
 , Frame -> True
 ]

Results of parallel evaluation

Mauricio Fernández
  • 5,463
  • 21
  • 45