Energy minimization - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-23T19:00:03Z https://mathematica.stackexchange.com/feeds/question/167889 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/167889 0 Energy minimization Serhii https://mathematica.stackexchange.com/users/55878 2018-03-13T23:28:31Z 2018-03-14T09:23:56Z <p>I am trying to fit some experimental data, and for that I need to solve the following problems:</p> <p><strong>1) Minimize the energy of a system of the following form:</strong></p> <p>(Not a code, but editor require it to be so)</p> <pre><code>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) </code></pre> <p>for each value of B in a range with some step. (-0.7 to 0.7 in Tesla), taking psi1 and psi2 as variables.</p> <p>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.</p> <p><strong>2) Plug in found equlibrium values into the expression:</strong></p> <p>(Not a code, but editor require it to be so)</p> <pre><code>M(B)/Ms = [M1*Cos(Psi1eq- alpha) + M2*Cos(Psi2eq- alpha)]/(M1 + M2) </code></pre> <p>And create interpolated function of B. Where again M1,M2,Ms,alpha are given and Psi1eq, Psi2eq are angles found in 1) </p> <p>Here is my intermediate solution: (This is the code I run in mathematica)</p> <pre><code>(*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 -&gt; fieldTesla, 0 &lt;= Psi1 &lt; 2*Pi, 0 &lt;= Psi2 &lt; 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 -&gt; minima[[i, 2]][], Psi2eq -&gt; minima[[i, 2]][]}}, {i, 1, Length[minima]}]; Magnetizationfunc = Interpolation[Magnetizationtable]; </code></pre> <p>Results have the following form: </p> <pre><code>ListPlot[Magnetizationtable, PlotRange -&gt; All] </code></pre> <p><a href="https://i.stack.imgur.com/iCOqL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iCOqL.jpg" alt="enter image description here"></a></p> <pre><code>Plot[Magnetizationfunc[x], {x, -0.7, 0.7}] </code></pre> <p><a href="https://i.stack.imgur.com/hXyhg.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hXyhg.jpg" alt="enter image description here"></a></p> <p>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.</p> <p>If I change alpha by Pi keeping everything the same, situation reverses:</p> <pre><code>Plot[Magnetizationfunc[x], {x, -0.7, 0.7}] </code></pre> <p><a href="https://i.stack.imgur.com/DoQu9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DoQu9.jpg" alt="enter image description here"></a></p> <p>I expect graph to be symmetric (atleast for sure for high fields). Some assymetry may be involved approximately for B &lt; 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. <strong>How to overcome this?</strong></p> <p><strong>3) (Not the OP, but I would be very appreciate for any help on that</strong>) 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).</p> <p>Data Example: <a href="https://www.dropbox.com/s/azik6tffvug92eo/data%20exa%2Cple%20for%20fitting.txt?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/azik6tffvug92eo/data%20exa%2Cple%20for%20fitting.txt?dl=0</a></p> <p><em>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.</em></p>