I have a matrix that depends on one variable and I want to maximize the sum of the first two eigenvalues. I wrote the following mathematica code:
Clear[T, f, x]
T[x_] := {{0, 1, 1, 1}, {1, 0, Exp[I*x], 1}, {1, Exp[-I*x], 0, 1}, {1,
1, 1, 0}}
f[x_] := Sum[Sort[Re[Eigenvalues[T[x]]], Greater][[n]], {n, 1, 2}]
Plot[f[x], {x, 0, 2 Pi}]
NArgMax[{f[x], 0 < x < 2 Pi}, x]
NMaxValue[{f[x], 0 < x < 2 Pi}, x]
A little algebra shows that the maximum of this function is attained at x=Pi where f(Pi)=1+Sqrt[5], this also agrees with the plot. However, the values that NArgMax finds is x=1.5708 (=Pi/2) and NMaxValue finds 1.70928. So Mathematica is clearly not doing what I expect it to do. What am I missing/ misunderstanding? Thanks.
Note that the eigenvalues of the matrix are all real for real x, I put in the Re because otherwise Mathematica will complain that f is not real valued due to numerical inaccuracies.