# Why does the function “NMaximize” product a new result while the condition changes from the subset of the set A to A?

In:= DIU2[a1_, c1_] =
Simplify[1/
8 (Sqrt[1 - a1^2 -
c1^2] (14 Sqrt[1 - a1^2 - c1^2] +
2 Sqrt[1 - a1^2 - c1^2] Cos[2 \[Theta]] +
Sqrt (a1 + c1) Sin[2 \[Theta]]) +
a1 (9 a1 + 5 c1 - (a1 + c1) Cos[2 \[Theta]] +
Sqrt Sqrt[1 - a1^2 - c1^2] Sin[2 \[Theta]]) +
c1 (5 a1 + 9 c1 - (a1 + c1) Cos[2 \[Theta]] +
Sqrt Sqrt[1 - a1^2 - c1^2] Sin[2 \[Theta]]))]

Out= 1/8 (14 - 5 a1^2 + 10 a1 c1 -
5 c1^2 - (-2 + 3 a1^2 + 2 a1 c1 + 3 c1^2) Cos[2 \[Theta]] +
2 Sqrt (a1 + c1) Sqrt[1 - a1^2 - c1^2] Sin[2 \[Theta]])

In:= NMaximize[{Simplify[DIU2[a1, c1] /. {\[Theta] -> 1.2}],
0 <= a1 <= 1 && 0 <= c1 <= 1 && a1^2 + c1^2 <= 1}, {a1, c1}]

\:6B63\:5728\:8BA1\:7B97In:= NMaximize::nrnum: The function value -1.75565-0.0964303 I is not a real number at {a1,c1} = {0.426364,0.95088}.

Out= {1.92067, {a1 -> 0.437452, c1 -> 0.70154}}

In:= NMaximize[{Simplify[DIU2[a1, c1] /. {\[Theta] -> 1.2}],
0 <= a1 <= 1/Sqrt && 0 <= c1 <= 1/Sqrt &&
a1^2 + c1^2 <= 1}, {a1, c1}]

Out= {2., {a1 -> 0.659051, c1 -> 0.659051}}


There are two kinds of boundary conditions, A and B, ant it satisfies A is contained in B. However，the maximum value with the condition A is bigger than B?

• The warning message in the first case indicates that the search terminated when the constraints were violated and the result is the maximum found at that point in the search. In the second case, there is no warning and the search terminated at the maximum. When you get a warning try to figured out what it may imply. – Bob Hanlon Jun 16 at 13:26
• There are workarounds NMaximize[{Simplify[DIU2[a1, c1] /. {\[Theta] -> 1.2}], 0 <= a1 <= 1 && 0 <= c1 <= 1 && a1^2 + c1^2 <= 1}, {a1, c1}, Method -> "RandomSearch"] and NMaximize[{Simplify[DIU2[a1, c1] /. {\[Theta] -> 1.2}], 0 <= a1 <= 1 && 0 <= c1 <= 1 && a1^2 + c1^2 <= 1}, {a1, c1}, Method -> {"DifferentialEvolution", "ScalingFactor" -> 0}]. – user64494 Jun 16 at 14:06
• @user64494 Thanks a lot! Why will it work by adding the order "Method -> RandomSearch" or "Method -> {"DifferentialEvolution", "ScalingFactor" -> 0}"? – Hengji Li Jun 16 at 16:30
• @HengjiLi: Don't know it. – user64494 Jun 16 at 16:56

NMaximize gets confused when coming into an area with complex function values.

Since your conditions allow only variable-values where the function is real, you can set a "Re" before the function.

  NMaximize[{Re[DIU2[a1, a1] /. {\[Theta] -> 1.2}],
0 <= a1 <= 1 && 0 <= c1 <= 1 && a1 == c1 && 2 a1^2 <= 1},
{a1, c1}]

(*   {2., {a1 -> 0.659051, c1 -> 0.659051}}   *)

• The question arises: why does NMaximize[{Simplify[DIU2[a1, c1] /. {\[Theta] -> 1.2}], 0 <= a1 <= 1 && 0 <= c1 <= 1 && a1^2 + c1^2 <= 1}, {a1, c1}, Method -> {"DifferentialEvolution", "ScalingFactor" -> 0}] not need Re? – user64494 Jun 21 at 18:31
• A look into the tutorial "tutorial/ConstrainedOptimizationGlobalNumerical" says, "The j[Null]^th new point is generated by picking three random points, Subscript[x, u], Subscript[x, v] and Subscript[x, w], from the old population, and forming Subscript[x, s]=Subscript[x, w]+s(Subscript[x, u]-Subscript[x, v]), where s is a real scaling factor ..." Here maximum is close to the imaginary area, where it gets confused. ScalingFactor of 10^-3 also does the job. – Akku14 Jun 21 at 19:04
• Thank you. Frankly speaking, I don't understand your explanation "Here maximum is close to the imaginary area, where it gets confused". How about  Method -> "RandomSearch"? – user64494 Jun 21 at 19:20
• The algorithm of "DifferentialEvolution" generates new searching points a little bit away from previous tested points. This distance is determined by "ScalingFactor". It seems this intermediate calculation points are not forced to lie within the area determined by conditions. You get an error when their function values are complex. Since here maximum is close to the area where you get imaginary function values, small "ScalingFactor" helps you not to get intermediate imaginary points, when started from real area. It seem only finally found maximum points are testes for conditions. – Akku14 Jun 22 at 4:02