# Energy minimization

I am trying to fit some experimental data, and for that I need to solve the following problems:

1) Minimize the energy of a system of the following form:

(Not a code, but editor require it to be so)

Energy = (Ku*Sin[phi1] + Ms*B*Cos[alpha - phi1])*
t1 + (Ku*Sin[phi2] + Ms*B*Cos[alpha - phi2])*
t2 + (-J1*Cos[phi1 - phi2] - J2*Cos[phi1 - phi2]^2)


for each value of B in a range with some step. (-0.7 to 0.7 in Tesla), taking psi1 and psi2 as variables.

Ms, Ku, t1, t2 are just given values and J1,J2 idealy should be fitting parameters, but for now I just want to focus on a simple case, just substituting reasonable values for them.

2) Plug in found equlibrium values into the expression:

(Not a code, but editor require it to be so)

M(B)/Ms = [M1*Cos(Psi1eq- alpha) + M2*Cos(Psi2eq- alpha)]/(M1 + M2)


And create interpolated function of B. Where again M1,M2,Ms,alpha are given and Psi1eq, Psi2eq are angles found in 1)

Here is my intermediate solution: (This is the code I run in mathematica)

(*Parameters*)
t1 = 9*10^-3; t2 = 3*10^-3; M1 = 0.890; M2 = 0.625; Ms = 0.824; Ku = 10^-3;
alpha =  Pi/4; J1 = -0.0002; J2 = 0;
(*Evaluation*)
Ez1 = Ku*Sin[Psi1]^2 - Ms*B*Cos[Alpha - Psi1];
Ez2 = Ku*Sin[Psi2]^2 - Ms*B*Cos[Alpha - Psi2];
Eex = -J1*Cos[Psi1 - Psi2] - J2*Cos[Psi1 - Psi2]^2;
Etot[Psi1_, Psi2_] := Ez1*t1 + Ez2*t2 + Eex;

minima = Table[{fieldTesla, {Psi1, Psi2} /.
Last[FindMinimum[{Etot[Psi1, Psi2] /. B -> fieldTesla,
0 <= Psi1 < 2*Pi, 0 <= Psi2 < 2*Pi}, {Psi1, Psi2}]}, {fieldTesla,
0.7, -0.7, -0.001}];
ApproximationExpr = (M1*Cos[Psi1eq - Alpha] + M2*Cos[Psi2eq - Alpha])/(M1 + M2);
Magnetizationtable = Table[{minima[[i, 1]], ApproximationExpr /. {Psi1eq ->
minima[[i, 2]][[1]], Psi2eq -> minima[[i, 2]][[2]]}}, {i, 1,
Length[minima]}];

Magnetizationfunc = Interpolation[Magnetizationtable];


Results have the following form:

ListPlot[Magnetizationtable, PlotRange -> All]


Plot[Magnetizationfunc[x], {x, -0.7, 0.7}]


As one can see for negative fields, something strange going with numerics and I can't fix it. I have been trying numerical minimum search NMinimize or simply equating derivatives to 0 and doing NSolve. Also changing AccuracyGoal, WorkingPrecission etc.

If I change alpha by Pi keeping everything the same, situation reverses:

Plot[Magnetizationfunc[x], {x, -0.7, 0.7}]


I expect graph to be symmetric (atleast for sure for high fields). Some assymetry may be involved approximately for B < 0.01. But definetely not this spikes, especially one them is lower that -1, which is matematically impossible, considering the form of the function in 2. How to overcome this?

3) (Not the OP, but I would be very appreciate for any help on that) Modify the code, so it will be able to fit the data from real experiment with interpolated function mentioned in 2) using J1 and J2 as a parameters (mb even with some constraints).

Notes: 1) It is my 1st post here, so please don't hesitate to tell me, if I need to add or remove something. 2) I dont mind using other mathematical packages to solve this.

• Some remarks on your posted code: 1.) E is the Euler number; you cannot assign values to it. 2.) Function arguments have to be wrapped in brackets [], not in parentheses (). 3.) When defining functions, use SetDelayed (:=) instead of Set (=), e.g., it should read Etot[Psi1_, Psi2_] := – Henrik Schumacher Mar 13 '18 at 23:38
• corrected 1,3. I am not sure what you mean about 2nd. If it is about ApproximationFunc it is an expression, not func (sorry for confusion). – Serhii Mar 14 '18 at 0:27
• in this expression [M1*Cos(Psi1eq- alpha) +.. the square brackets are a syntax error. The left side is illegal as well, so maybe that whole thing is not meant to be code..? – george2079 Mar 14 '18 at 0:50
• I don't understand what you mean about it. Its just an expression, I substitute values in and make a table out of it. I agree name is confusing, so i will change it. But I don't understand what is the syntax error connected with it – Serhii Mar 14 '18 at 0:58
• They are saying that if you try to evaluate M(B)/Ms = [M1*Cos(Psi1eq- alpha) + M2*Cos(Psi2eq- alpha)]/(M1 + M2) itself in Mathematica, you'll get an error. (Try it yourself!) – J. M. is away Mar 14 '18 at 1:38