1
$\begingroup$
In[1]:= 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[2] (a1 + c1) Sin[2 \[Theta]]) + 
     a1 (9 a1 + 5 c1 - (a1 + c1) Cos[2 \[Theta]] + 
        Sqrt[2] Sqrt[1 - a1^2 - c1^2] Sin[2 \[Theta]]) + 
     c1 (5 a1 + 9 c1 - (a1 + c1) Cos[2 \[Theta]] + 
        Sqrt[2] Sqrt[1 - a1^2 - c1^2] Sin[2 \[Theta]]))]

Out[1]= 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[2] (a1 + c1) Sqrt[1 - a1^2 - c1^2] Sin[2 \[Theta]])

In[14]:= 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[14]:= NMaximize::nrnum: The function value -1.75565-0.0964303 I is not a real number at {a1,c1} = {0.426364,0.95088}.

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

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

Out[13]= {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?

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ – Bob Hanlon Jun 16 at 13:26
  • $\begingroup$ 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}]. $\endgroup$ – user64494 Jun 16 at 14:06
  • $\begingroup$ @user64494 Thanks a lot! Why will it work by adding the order "Method -> RandomSearch" or "Method -> {"DifferentialEvolution", "ScalingFactor" -> 0}"? $\endgroup$ – Hengji Li Jun 16 at 16:30
  • $\begingroup$ @HengjiLi: Don't know it. $\endgroup$ – user64494 Jun 16 at 16:56
0
$\begingroup$

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}}   *)
$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$ – user64494 Jun 21 at 18:31
  • $\begingroup$ 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. $\endgroup$ – Akku14 Jun 21 at 19:04
  • $\begingroup$ 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"`? $\endgroup$ – user64494 Jun 21 at 19:20
  • $\begingroup$ 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. $\endgroup$ – Akku14 Jun 22 at 4:02

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.