Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.8K Views
|
5 Replies
|
1 Total Like
View groups...
Share
Share this post:

[?] Minimize a function?

Posted 8 years ago

This is a screen shot of my code. I am trying to minimize a function f(). please help me.

Attachments:
POSTED BY: Arnob Mukherjee
5 Replies

You need to give numerc values to the variables J2 and H for MinValue to evaluate. I would suggest a delayed definition:

e0[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := MinValue[ ...]

I don't understand why you first give a numeric value to J1 and then you use it as a variable for e0 with immediate definition.

POSTED BY: Gianluca Gorni

Your code looks syntactically correct, but it is probably inefficient. Your ContourPlot requires a lot of MinValue minimizations, that take a long time. Moreover, you define your function g[J1, J2, H] to take only 6 possible values, and it seems to be quite constant in your domain:

Table[g[J1, J2, H], {H, -1, 1, 1/2}, {J2, -1, 1, 1/2}]

and ContourPlot struggles to come up with a contour to show.

POSTED BY: Gianluca Gorni

Mr. Daniel Lichtblau,

I have provided the complete code for your and everyone's references. I am trying to plot a phase diagram using contour plot, but it is not working. Further help would be appreciated,

POSTED BY: Arnob Mukherjee
ClearAll[\[Phi]x, \[Phi]y, \[Theta], J2, H, f, f1, f2, f3, f4, e2, \
e3, e4, e1, e0]

f[\[Phi]x_, \[Phi]y_, \[Theta]_, J1_, J2_, H_] := 
  J1 ((Sin[\[Theta]]^2)*Cos[\[Phi]x] + (Sin[\[Theta]]^2)*
       Cos[\[Phi]y] + 2*Cos[\[Theta]]^2)  + 
   J2*0.25*((Sin[\[Theta]]^2)*Cos[\[Phi]x + \[Phi]y] + 
      Cos[\[Theta]]^2) + 
   J2*0.25*((Sin[\[Theta]]^2)*Cos[\[Phi]y - \[Phi]x] + 
      Cos[\[Theta]]^2) - H*Cos[\[Theta]];
e0[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := 
  MinValue[{f[\[Phi]x, \[Phi]y, \[Theta], J1, J2, H], 
    0 <= \[Phi]x <= 2*Pi, 0 <= \[Phi]y <= 2*Pi, 
    0 <= \[Theta] <= Pi}, {\[Phi]x, \[Phi]y, \[Theta]}];

f1[J1_, J2_, \[Theta]_, 
   H_] := (2*J1) + (0.5*J2) - (H* Cos[\[Theta]]);
e1[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := 
  MinValue[{f1[J1, J2, \[Theta], H], 0 <= \[Theta] <= Pi}, {\[Theta]}];

f2[J1_, J2_, \[Theta]_, 
   H_] := (2*J1*Cos[2*\[Theta]]) + (0.5*J2) - (H*Cos[\[Theta]]);
e2[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := 
  MinValue[{f2[J1, J2, \[Theta], H], 0 <= \[Theta] <= Pi}, {\[Theta]}];

f3[J1_, J2_, \[Theta]_, 
   H_] := (2*J1*Cos[\[Theta]]^2) + (0.5*J2*Cos[2*\[Theta]]) - (H*
     Cos[\[Theta]]);
e3[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := 
  MinValue[{f3[J1, J2, \[Theta], H], 0 <= \[Theta] <= Pi}, {\[Theta]}];

f4[J1_, J2_, \[Theta]_, 
   H_] := (2*J1*Cos[\[Theta]]^2) + (0.5*J2*Cos[\[Theta]]^2) - (H*
     Cos[\[Theta]]);
e4[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := 
  MinValue[{f4[J1, J2, \[Theta], H], 0 <= \[Theta] <= Pi}, {\[Theta]}];

g[J1_?NumericQ, J2_?NumericQ, H_?NumericQ] := 
  If [e0[J1, J2, H] < e1[J1, J2, H] &&

    e0[J1, J2, H] < e2[J1, J2, H] &&

    e0[J1, J2, H] < e3[J1, J2, H] &&

    e0[J1, J2, H] < e4[J1, J2, H], 0.0,
   If [e1[J1, J2, H] < e0[J1, J2, H] &&

     e1[J1, J2, H] < e2[J1, J2, H] &&

     e1[J1, J2, H] < e3[J1, J2, H] &&

     e1[J1, J2, H] < e4[J1, J2, H], 0.2,
    If[e2[J1, J2, H] < e0[J1, J2, H] &&

      e2[J1, J2, H] < e1[J1, J2, H] &&

      e2[J1, J2, H] < e3[J1, J2, H] &&

      e2[J1, J2, H] < e4[J1, J2, H], 0.4,
     If[e3[J1, J2, H] < e0[J1, J2, H] &&

       e3[J1, J2, H] < e1[J1, J2, H] &&

       e3[J1, J2, H] < e2[J1, J2, H] &&

       e3[J1, J2, H] < e4[J1, J2, H], 0.6, 
      If[e4[J1, J2, H] < e0[J1, J2, H] &&

        e4[J1, J2, H] < e1[J1, J2, H] &&

        e4[J1, J2, H] < e2[J1, J2, H] &&

        e4[J1, J2, H] < e3[J1, J2, H], 0.8, 1.]]]]];

J2 = 1.0;
J1 = 1.0;

q1 = Plot[{e0[J1, J2, H], e1[J1, J2, H], e2[J1, J2, H], e3[J1, J2, H],
    e4[J1, J2, H]}, {H, -1, 1}, 
  PlotStyle -> {{Black, Thick}, {Red, Thick}, {Green, Thick}, {Blue, 
     Thick}, {Magenta, Thick}}, 
  PlotLegends -> {"Spiral", "FM", "AFM", "Stripe", "Ortho"}, 
  Frame -> True, FrameStyle -> Directive[Black, 18], 
  FrameLabel -> {"H" , "Energy"}, RotateLabel -> True, 
  LabelStyle -> Directive[Black, 22]]

Clear[J2, H]

p4 = ContourPlot[g[J1, J2, H], {H, -1, 1}, {J2, -1, 1}, 
  PlotPoints -> 30, ColorFunction -> "Rainbow", 
  ColorFunctionScaling -> False, Frame -> True, 
  FrameStyle -> Directive[Black, 18], 
  PlotLabel -> 
   Style[Framed["\!\(\*SubscriptBox[\(J\), \(1\)]\)=1.0"], 18, Black, 
    Background -> Lighter[Gray]], 
  FrameLabel -> {"H" , "\!\(\*SubscriptBox[\(J\), \(\"\<2\>\"\)]\)"}, 
  RotateLabel -> False, LabelStyle -> Directive[Black, 26]]
Attachments:
POSTED BY: Arnob Mukherjee

In future provide actual code, not a picture of code. The former is what can actually be used by others in efforts to diagnose issues, suggest improvements, etc.

POSTED BY: Daniel Lichtblau
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard