2
$\begingroup$

I have this two-variable function $f(x,y)$ for {x,-Pi,Pi} and {y,-Pi,Pi} in which $\{a,b,c\}$ are real parameters (can be positive or negative).

f[x_,y_]=a (Cos[x]+Cos[y])+b (Cos[2x]+Cos[2y])+c (Cos[x]Cos[y]);

Question Is it possible to ask Mathematica to find those points $(x,y)$ at which the function may reach its global/local extrema?

P.S. I tried to obtain those points analytically by finding the critical points but it is still difficult to choose which one may be global extrema.

$\endgroup$
1

2 Answers 2

5
$\begingroup$
f[x_, y_] := a (Cos[x] + Cos[y]) + b (Cos[2 x] + Cos[2 y]) + c (Cos[x] Cos[y])

(* Determine where partial derivatives are zero *)
extrema = {x, y, f[x, y]} /. Solve[D[f[x, y], {{x, y}}] == 0, {x, y}] /. C[1] -> 0 /. C[2] -> 0 // FullSimplify

Points of extremes

(* Find minima and maxima for specific values of a, b, and c *)
minmax[aa_, bb_, cc_] := Module[{e, minima, maxima},
  e = extrema /. {a -> aa, b -> bb, c -> cc};
  minima = Select[e, #[[3]] == Min[e[[All, 3]]] &];
  maxima = Select[e, #[[3]] == Max[e[[All, 3]]] &];
  {minima, maxima}]

{minima, maxima} = minmax[10, -7, 4]

(* {{{π, π, -30}}, 
    {{-ArcTan[Sqrt[119]/5], -ArcTan[Sqrt[119]/5], 109/6}, 
     {-ArcTan[Sqrt[119]/5],  ArcTan[Sqrt[119]/5], 109/6},
     { ArcTan[Sqrt[119]/5], -ArcTan[Sqrt[119]/5], 109/6},
     { ArcTan[Sqrt[119]/5],  ArcTan[Sqrt[119]/5], 109/6}}} *) 

Finding solutions in general

There are only 6 unique extrema values/forumulas:

(* Determine where partial derivatives are zero *)
extrema = {x, y, f[x, y]} /. Solve[D[f[x, y], {{x, y}}] == 0, {x, y}] // FullSimplify
(unique = FullSimplify[DeleteDuplicates[extrema[[All, 3]]], 
    Assumptions -> C[1] ∈ Integers && C[2] ∈ Integers]) // TableForm

6 unique extrema values

(Note that I dropped the restriction /. C[1] -> 0 /. C[2] -> 0.)

The first value in unique is the global maximum when the following occurs:

Reduce[Table[unique[[1]] >= unique[[i]], {i, 2, 6}]]

Conditions when 2 (a + b) + c is the global maximum

In this same fashion one can determine when each unique formula is the global minimum, global maximum, local minimum, or local maximum. It does not appear that a general solution will be short and sweet.

Getting to know the function

Here is some Manipulate code that shows the global minimum and maximum location(s) along with other points where the first partial derivatives are zero:

f[x_, y_, a_, b_, c_] := 
 a (Cos[x] + Cos[y]) + b (Cos[2 x] + Cos[2 y]) + c (Cos[x] Cos[y])

Manipulate[
 extrema = {x, y, f[x, y, a, b, c]} /. 
       Solve[D[f[x, y, a, b, c], {{x, y}}] == 0, {x, y}, Reals] /. 
      C[1] -> c1 /. C[2] -> c2 // FullSimplify // Quiet;
 extrema = 
  Flatten[Table[Table[extrema[[i]], {c1, -1, 1}, {c2, -1, 1}], {i,  Length[extrema]}], 2];
 extrema = Select[extrema, -π <= #[[1]] <= π && -π <= #[[2]] <= π &];
 maxima = Select[extrema, #[[3]] == Max[extrema[[All, 3]]] &];
 minima = Select[extrema, #[[3]] == Min[extrema[[All, 3]]] &];
 Show[ContourPlot[f[x, y, a, b, c], {x, -π, π}, {y, -π, π}, ContourShading -> None],
  ListPlot[{extrema[[All, {1, 2}]], maxima[[All, {1, 2}]], minima[[All, {1, 2}]]}, 
    PlotStyle -> {LightGray, Black, Red}, 
     PlotLegends -> {"Other", "Global maximum", "Global minimum"}]],
 {{a, 6}, -10, 10, Appearance -> "Labeled"},
 {{b, 1}, -10, 10, Appearance -> "Labeled"},
 {{c, -7}, -10, 10, Appearance -> "Labeled"},
 TrackedSymbols :> {a, b, c}]

Contour plot indicating locations of extrema

$\endgroup$
5
  • 1
    $\begingroup$ Partial answers: (1) That's possible but I don't think it matters (in this case). (2) Yes. (3) Yes and I'll add an approach to do so. $\endgroup$
    – JimB
    Apr 1, 2023 at 20:13
  • 1
    $\begingroup$ Setting C[1] and C[2] to zero does matter after all. I'll think about that more. $\endgroup$
    – JimB
    Apr 1, 2023 at 20:47
  • 1
    $\begingroup$ Legit values for C[1] and C[2] can be found with Table[Reduce[{-\[Pi] <= extrema[[i, 1]] <= \[Pi], -\[Pi] <= extrema[[i, 2]] <= \[Pi]}], {i, Length[extrema]}] // FullSimplify. $\endgroup$
    – JimB
    Apr 1, 2023 at 21:06
  • $\begingroup$ Are you sure about the global maximum being at $(x,y)=(0,0)$ when at least one of $a$, $b$, and $c$ are negative? $\endgroup$
    – JimB
    Apr 1, 2023 at 22:23
  • $\begingroup$ I have another question; can we claim that none of your 6 unique extrema values can be larger than $2(\lvert a \rvert+\lvert b \rvert)+\lvert c \rvert$? or, in other words, to claim that this value is the larger value that the function $f$ can take? I guess so since this happens at $(x,y)=(0,0)$ but when I check numerically by RegionPlot3D the result implies that your sixth value can be larger than this value! @JimB $\endgroup$
    – MsMath
    Apr 16, 2023 at 17:43
1
$\begingroup$

Another approach is as follows.

f[a_?NumericQ, b_?NumericQ, c_?NumericQ] := 
Maximize[{a (Cos[x] + Cos[y]) + b (Cos[2 x] + Cos[2 y]) +  c (Cos[x] Cos[y]), 
x >= -Pi && x <= Pi && y >= -Pi && y <= Pi}, {x, y}];
f[1, 2, -E]

{4 + E, {x -> -\[Pi], y -> 0}}

f[1.0, 2, -E]

{6.71828, {x -> 3.14159, y -> -8.54234*10^-9}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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