# Equality contraint violated with NMinimize

I am attempting to numerically optimize a function of only 2 parameters k, θ. There are two inequality constraints (1 each on k and on θ), and one equality constraint on both k and θ. The MWE is provided below. This question is a follow-on from this question that was answered by @user293787. I have seen a related post, but the solution did not work here.

I have noticed that the Minimize performs well when only the two inequality constraints are included. However, I get a warning when I include the equality constraint, which is always violated.

How can I get around this?

MWE:

s[x_]:=Total[x*Log2[x]];

A=DiagonalMatrix[{0.0632,0.3065,0.0632,0.3065}];
B=DiagonalMatrix[{0.3065,0.0632,0.3065,0.0632}];
AB=MatrixPower[A.A+B.B,1/2];

f[k_?NumericQ,θ_?NumericQ]:=With[{T={
{2 k Cos[θ]^2+k+1,0,0,6 k Cos[θ]^2-k-1},
{0,2 k Sin[θ]^2+3-3 k,0,0},
{0,0,2 k Sin[θ]^2+3-3 k,0},
{6 k Cos[θ]^2-k-1,0,0,2 k Cos[θ]^2+k+1}}},
s[Eigenvalues[AB.T.AB]]-s[Eigenvalues[A.T.A]]-s[Eigenvalues[B.T.B]]
];


I would like to optimise f[k,θ] using the following

x1 = 0 <= k <= 1;
x2 = 0 < θ <= 2 \[Pi];
x3 = 3 - (2 k - 3) - k Cos[2 θ] == 0;

{min, arg} = NMinimize[{f[k,θ], x1 && x2 && x3}, {k, θ}]


The output to this NMinimize yields outputs a result with a comments "The function value Indeterminate is not a number at {k,θ} = {1,6.2831}". However, despite an output when I check the constraints I notice that the equality constraint is always violated and by a lot, i.e.

x3 /. arg


yields False. In checking the value of x3 using the optimization values in arg, I obtain 3 (should be 0), which is a strong violation of the constraint. If I remove the equality constraint x3 I notice that the optimization behaves properly. Rationalising A and B does not help.

What is the cause of this and a workaround to enforce the equality constraint?

• Is it clear that plugging a floating-point solution into an equation is a bad way to check it? E.g. x^2 - 3 == 0 /. x -> Sqrt[3.] returns False. (Apparently not the main problem here, tho.) Commented Dec 6, 2022 at 14:04
• @MichaelE2, in a convoluted way you could use Chop to check this statement, is correct with floating-point solutions. As you note though, it's not the main problem here. I may need to revisit the constraints
– Sid
Commented Dec 6, 2022 at 15:51

Reduce[x1 && x2 && x3, {k, \[Theta]}]