I am trying to solve minimization problem for the simingly not too complex energy function with the following parameters

J = -0.1; Ms1 = 800; Ms2 = 800; d1 = 3; d2 = 3; J2 = 0.; Hu1 = 0; Hu2 = 0; \[Phi]B = 0; 

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.

enter image description here

If we change d1 = 9 in the parameter list, the sign flip becomes more apparent, although again the shape of the solution is reasonable.

enter image description here

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.


1 Answer 1


The problem is that if {x,y} is a solution, so is {y,x} and NMinimize isn't particular about which solution it's picking. An easy way to get things to look nice in these plots is to sort the answers so that x is always the smaller of the two angles.

J = -0.1; Ms1 = 800; Ms2 = 800; d1 = 3; d2 = 3; J2 = 0.; Hu1 \
= 0; Hu2 = 0; ϕB = 0;


f4[x_, y_, B_] := -(J*10^7)*Cos[x - y] - B*Ms1*d1*Cos[ϕB - y] - 
   B*Ms2*d2*Cos[ϕB - x];

result = Module[
   {b, sol},
   b = Range[2, 200, 1];
   sol = Table[
     {\[FormalA], \[FormalB]} /. 
      Last[NMinimize[{f4[\[FormalA], \[FormalB], 
          10*bb], -π < \[FormalA] <= π && -π < \[FormalB] \
<= π}, {\[FormalA], \[FormalB]}]]
     , {bb, b}
   sol = Sort /@ sol; (*Sort the solutions.*)
   Transpose@{b, sol[[;; , 1]], sol[[;; , 2]]} (* combine solutions into a single list. {{b1,x1,y1},{b2,x2,y2},...}*)

 {result[[;; , {1, 2}]], result[[;; , {1, 3}]]}
 , PlotRange -> All]

enter image description here

If you have solutions that cross over one another and you want the coloring to follow the 'obvious' lines you see, this won't work. This problem has been addressed here: 111315

  • $\begingroup$ Thank you for the reply. This indeed helps with the colors. But if I run this code for d1=9 (with all other parameters being the same),I still get this weird discontinuous behavior as in 2nd graph of the OP. Because of that, coloring also becomes wrong, as solutions are flipping sign (same as crossing basically). So the problem here is more like NMinimize fails to find correct solution in that region, giving it instead with the opposite sign to what it should be. $\endgroup$
    – kek
    Commented Apr 24, 2020 at 18:09
  • $\begingroup$ @Serhii it looks like the problem may just be undesirable phase wrapping. If you open up your constraints to +/- 2Pi or so and then wrap the phases yourself you might have better luck - I tried it with your d1=9 case and it looked better. You can probably also automate this process, but it'll be quite a bit more work. You might have some luck if you search phase unwrapping or something similar on here. $\endgroup$
    – N.J.Evans
    Commented Apr 28, 2020 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.