20
$\begingroup$

No explanation needed:

Maximize[Cos[x + 1], x]
Maximize[Cos[x - 1], x]

producing

{1, {x -> -1 + 2 π}}
Maximize[Cos[1 - x], x]

Needless to say, this is pretty amusing.

Does anyone know why this is happening? I am using V10.0.2 Windows 7 64-bit, although I also verified it to occur on V10.0.1 Mac Snow Leopard (I haven't tested V9 and older).

Backstory

I was showing my friend how Mathematica works, and wanted to show him that the maximum of $\cos(f\,t−k\,x)$ was $1$ (he was taking an intro physics class). I applied Maximize to it, thinking I would get the answer. Hilarity ensued.

It didn't take long to find a significantly shorter minimal example.

$\endgroup$
  • 1
    $\begingroup$ Strange. Wonder why this works Maximize[Cos[x - 1.], x] giving {1., {x -> 1.}} $\endgroup$ – Nasser Jan 24 '15 at 22:59
  • 1
    $\begingroup$ I see same behavior on 9.0.1 MacOS X 10.9.5 $\endgroup$ – QuantumDot Jan 24 '15 at 23:01
  • 1
    $\begingroup$ @Nasser: Yup, as Chen Yu said, according to the documentation: "If Maximize is given an expression containing approximate numbers, it automatically calls NMaximize.", so it's calling NMaximize and numerically finding a maximum in your example. $\endgroup$ – DumpsterDoofus Jan 25 '15 at 0:12
  • 4
    $\begingroup$ I think it's bug. Maximize calculates the period as -2 π which it rejects since it does not satisfy 0 < -2 π < Infinity. Without the constraint, it gets to ToRules[C[1] ∈ Integers && x == 1 - 2 π C[1]], which it rejects since it does not evaluate to rules. $\endgroup$ – Michael E2 Jan 25 '15 at 1:24
  • 3
    $\begingroup$ Well, it seems to be fixed in Mathematica 10.2 (running on OS X here). $\endgroup$ – Wizard Jul 26 '15 at 16:46
11
$\begingroup$

Oh boy, it's hard to fix this at the top level. It should be easy for WRI to fix, though. I hope you've reported it. If we fix Periodic`PeriodicFunctionPeriod to return nonnegative periods, Maximize will work properly. The internal code has somewhat complicated pattern constraints on the definitions. I couldn't get the Villegas-Gayley technique to work, so I added an Abs* a real-only version of absolute value to the top-level internal function.

Block[{Periodic`PeriodicFunctionPeriod},

 Periodic`PeriodicFunctionPeriod[Periodic`PeriodicFunctionDump`expr__] /; 
   Periodic`Private`PDValidExpressionQ[{Periodic`PeriodicFunctionDump`expr}] := 
  Block[{Periodic`PeriodicFunctionDump`res}, 
   Periodic`PeriodicFunctionDump`res = 
    Periodic`PeriodicFunctionDump`periodicFunctionPeriod[Periodic`PeriodicFunctionDump`expr]; 
   If[Quiet[TrueQ[# < 0]], -#, #] &@ Periodic`PeriodicFunctionDump`res /;   (* <-- the "fix" *)
    FreeQ[Periodic`PeriodicFunctionDump`res, $Failed]];

 Maximize[Cos[1 - x], x]
 ]
(*
  {1, {x -> 1}}
*)

I cannot be sure that such a fix won't break something. It could be that a "negative period" is a sign of something that is used somewhere inside Mathematica. Mathematically speaking, the period should be a positive number.


The problem, as I mentioned in a comment, is that negative coefficients on x results in a negative period:

Periodic`PeriodicFunctionPeriod[Cos[1 - x], x]
(*  -2 π  *)

Maximize checks that the period is between 0 and Infinity. (So Maximize is assuming the mathematical notion of period.) Since it's not, the period is discarded and the equation for the critical points is solved using Reduce without the constraint of the period. Reduce produces the indeterminate result:

Reduce[D[Cos[1 - x], x] == 0, x, Reals, WorkingPrecision -> ∞]
(*  C[1] ∈ Integers && (x == 1 - 2 π C[1] || x == 1 - π - 2 π C[1])  *)

ToRules is applied to this. Maximize assumes that if it does not evaluate to rules, then the problem has not been solved. ToRules returns unevaluated. In any case that's the nature of the bug. It looks easy to fix (for WRI).


Update note -- I just realized my comment shows that it is possible to have complex-imaginary periods. This might lead to a case where the original fix of applying Abs to the period would mess things up. I changed the fix so that only negative real numbers have their sign changed.

$\endgroup$
  • 1
    $\begingroup$ It does seem strange that Maximize checks that the period is nonnegative, unless it is actually expecting to receive negative periods for some reason. Nor is this a new phenomenon; the same results are observed all the way back to (at least) version 5.2. But without knowing their reasons for doing it this way, it's hard to assess whether this is a bug or not. Anyway, +1 for a good diagnosis. $\endgroup$ – Oleksandr R. Jan 25 '15 at 18:16
  • $\begingroup$ @OleksandrR. Thanks. I'm surprised it goes back so far. I would have thought someone would have discovered it. It seems neither desirable nor hard to fix. Try Periodic`PeriodicFunctionPeriod[Exp[x], x], which yields a complex imaginary period, not a positive real. (I haven't thought of a way to get Infinity for a period.) $\endgroup$ – Michael E2 Jan 25 '15 at 19:47
  • $\begingroup$ Thanks for this answer. I sent a bug-report to WRI citing this page, hopefully it will get some attention. $\endgroup$ – DumpsterDoofus Jan 26 '15 at 15:47
  • $\begingroup$ WRI sent back this message: "Thank you for taking the time to send us this report. Since Maximize's primary domain is polynomials, I filed this with developers as a suggestion." Personally I think it ought to be expected to work with more than just polynomials, but hopefully they'll take a look and patch it. $\endgroup$ – DumpsterDoofus Jan 27 '15 at 0:11
  • $\begingroup$ @DumpsterDoofus I think it's hard to state a larger class of functions than polynomials for which one could guarantee that maxima will be found. It seems clear to me that Maximize is meant to handle this simple case. As you say, hopefully they'll fix it. Maybe they'll look at this Q&A and see how easy (I think) it is. $\endgroup$ – Michael E2 Jan 27 '15 at 1:09
6
$\begingroup$

For a periodic function, specify a period or use Solve or Reduce for a general solution.

f1[x_] = Cos[x + 1];

Maximize[{f1[x], -Pi <= x <= Pi}, x]

{1, {x -> -1}}

Maximize[{f1[x], 0 <= x <= 2 Pi}, x]

{1, {x -> -1 + 2*Pi}}

{f1[x] /. #, #} &@Solve[{f1'[x] == 0, f1''[x] < 0}, x][[1]] //
 Simplify[#, Element[C[1], Integers]] &

{1, {x -> -1 + 2*Pi*C[1]}}

f2[x_] = Cos[x - 1];

Maximize[{f2[x], -Pi <= x <= Pi}, x]

{1, {x -> 1}}

Maximize[{f2[x], 0 <= x <= 2 Pi}, x]

{1, {x -> 1}}

{f2[x] /. #, #} &@Solve[{f2'[x] == 0, f2''[x] < 0}, x][[1]] //
 Simplify[#, Element[C[1], Integers]] &

{1, {x -> 1 - 2*Pi*C[1]}}

$\endgroup$
  • 3
    $\begingroup$ Thank you for the workaround by manually manipulating the expression. Still, it's silly that this is even needed in the first place; this is a high-school-level math problem, and the fact that Mathematica gets tripped up on it is really unsatisfying. $\endgroup$ – DumpsterDoofus Jan 25 '15 at 14:21
  • 1
    $\begingroup$ I find that you almost always get better results when you specify a range. For some problems it's just specifying that a number is real or greater than 0. In this case, it seems to work if you specify any range, e.g. Maximize[{Cos[x - 1], -1000 Pi <= x <= +1000 Pi}, x]. My second note is that although setting the derivative to 0 and solving works most of the time, it fails on discontinuous functions. $\endgroup$ – 2012rcampion Jan 25 '15 at 17:47

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.