# Problem with Maximize

As $\{p1,p2,p3,p4,p5\}$ represents a probability distribution and $l1,l2$ are the eigenvalues of a stochastic matrix, the function $f$, as defined below, has to be maximized over a certain region.

L[p_List] := (p[[1]] - 0.2)*Log[p[[1]]] + (p[[2]] - 0.2)*
Log[p[[2]]] + (p[[3]] - 0.2)*Log[p[[3]]] + (p[[4]] - 0.2)*
Log[p[[4]]] + (p[[5]] - 0.2)*Log[p[[5]]]

CirculantMatrix[l_List?VectorQ] :=
NestList[RotateRight, RotateRight[l], Length[l] - 1]
CirculantMatrix[l_List?VectorQ, n_Integer] :=
NestList[RotateRight,
RotateRight[Join[Table[0, {n - Length[l]}], l]], n - 1] /;
n >= Length[l]

invFou[l_List] := 0.2*{1 + 2 l[[1]] Cos[2 Pi/5] + 2 l[[2]] Cos[4 Pi/5],
1 + 2 l[[1]] Cos[4 Pi/5] + 2 l[[2]] Cos[2 Pi/5],
1 + 2 l[[1]] Cos[4 Pi/5] + 2 l[[2]] Cos[2 Pi/5],
1 + 2 l[[1]] Cos[2 Pi/5] + 2 l[[2]] Cos[4 Pi/5],
1 + 2 l[[1]] + 2 l[[2]]}

m[l_List] := CirculantMatrix[invFou[l]]

f[p_List, M_List] := L[p.m[M]]/L[p]


We may assume that $l1=0.5$ and $0.37\leq l2 \leq 0.5$. If we try

Maximize[{f[{p1, p2, p3, p4, p5}, {0.5,
l2}], {p1 + p2 + p3 + p4 + p5 == 1 && p1 >= 0 && p2 >= 0 &&
p3 >= 0 && p4 >= 0 && p5 >= 0 && 0.37 <= l2 <= 0.5}}, {p1, p2, p3, p4, p5, l2}]


we get an error:

NMaximize::nrnum: The function value -0.206184-0.256075 I is not a real number at {l2,p1,p2,p3,p4,p5} = {0.446053,-0.0362605,0.263705,0.324533,0.369047,0.0789762}. >>

Why doesn't Mathematica take the maximum over the specified region?

• If your function is sufficiently messy, you may need to try using FindMaximum instead, noting that it finds local maxima, depending on where you start the search. But then, NMaximize may also yield local maxima. Aug 6, 2014 at 18:46

The docs specify that the domain should (usually) be Reals or Integers. These are keywords. You probably want the "domain" to be specified as a constraint.

Maximize[{
Abs[f[{p1, p2, p3, p4, p5}, {0.5, l2}]],
{p1 + p2 + p3 + p4 + p5 == 1 && p1 >= 0 && p2 >= 0 && p3 >= 0 && p4 >= 0 &&
p5 >= 0 && 0 <= l2 <= 0.5}},
{p1, p2, p3, p4, p5, l2}]


Or in V10 you can use the new region functions to specify the domain.

Maximize[
{Abs[f[{p1, p2, p3, p4, p5}, {0.5, l2}]]},
{p1, p2, p3, p4, p5, l2} \[Element]
ImplicitRegion[
p1 + p2 + p3 + p4 + p5 == 1 && p1 >= 0 && p2 >= 0 && p3 >= 0 &&
p4 >= 0 && p5 >= 0 && 0 <= l2 <= 0.5, {p1, p2, p3, p4, p5, l2}]]


With a capriciously chosen objective function

f[p_, l_] := p.p - Flatten[{1, l, l}].p


both of the above return

{0.45,
{p1 -> 0.4, p2 -> 0.149999, p3 -> 0.150002, p4 -> 0.149999, p5 -> 0.150002, l2 -> 0.5}}

• I tried your last suggestion. Unfortunately, I get another error: NMaximize::nnum: The function value Indeterminate is not a number at {l2,p1,p2,p3,p4,p5} = {0.482834,-0.218535,0.398641,0.613972,0.205921,0.}. >> For some reason Mathematica doesn't maximize the function over the specified region.
– phil
Aug 6, 2014 at 12:34
• @phil Which function did you try? My f above evaluates to 0.245845 at {l2,p1,p2,p3,p4,p5} = {0.482834,-0.218535,0.398641,0.613972,0.205921,0.}, not Indeterminate. (No one else can test your function, since you haven't posted it. The problem is probably dependent on your f.) Aug 7, 2014 at 1:38