I am trying to solve minimization problem for the simingly not too complex energy function with the following parameters
(*Pars*)
J = -0.1; Ms1 = 800; Ms2 = 800; d1 = 3; d2 = 3; J2 = 0.; Hu1 = 0; Hu2 = 0; \[Phi]B = 0;
(*Function*)
f4[x_, y_, B_] := -(J*10^7)*Cos[x - y] - B*Ms1*d1*Cos[\[Phi]B - y] - B*Ms2*d2*Cos[\[Phi]B - x];
resultXangle = Chop[Table[{B, First[{x, y} /. Last[NMinimize[f4[x, y, 10*B],
-Pi - 0.1 <= x <= Pi + 0.1 && -Pi - 0.1 <= y <= Pi + 0.1, {x, y}]]]}, {B, 2, 200, 1}]]
resultYangle = Chop[Table[{B, Last[{x, y} /. Last[NMinimize[f4[x, y, 10*B],
-Pi - 0.1 <= x <= Pi + 0.1 && -Pi - 0.1 <= y <= Pi + 0.1, {x, y}]]]}, {B, 2, 200, 1}]]
ListPlot[{resultXangle, resultYangle}, PlotRange -> All]
This gives the following result. Which is physically correct, but as you can see several points at about 75 have opposite signs, from what it should be.
If we change d1 = 9 in the parameter list, the sign flip becomes more apparent, although again the shape of the solution is reasonable.
I can not figure out what is the reason for this numerical error and how to fix it.
P. S. Before, I tried to split f4 into two equation for x and y, using FindRoot afterwards and equating derivative to zero. With manual change of the starting values I could get the correct solution, but the situation was even worse. I assumed it is because FindRoot also solves for maxima, so I changed to NMinimize, which solved most of the problems except this one.
Any alternative solutions are also welcome. Thank you in advance.
In case someone finds it usefull.
In addition to the answer, after fixing the more restrictive NMinimize boundaries for the angles: -pi <= y <= 0 and 0 <= x <= pi, the problem was solved. The function itself had two similar minima from -pi to pi, hence the algorithm was indecisive sometimes.