# \[?\]  Minimize a function?

**URL:** <https://community.wolfram.com/t/minimize-a-function/10746>\
**Category:** Questions\
**Tags:** physics\
**Created:** [June 27, 2017, 7:12am UTC](https://community.wolfram.com/t/minimize-a-function/10746 "2017-06-27T07:12:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arnobmukherjee1988](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@arnobmukherjee1988](https://community.wolfram.com/u/arnobmukherjee1988)\
**Post date:** [June 27, 2017, 7:12am UTC](https://community.wolfram.com/t/minimize-a-function/10746/1 "2017-06-27T07:12:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 27, 2017, 7:44am UTC](https://community.wolfram.com/t/minimize-a-function/10746/2 "2017-06-27T07:44:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [June 27, 2017, 3:34pm UTC](https://community.wolfram.com/t/minimize-a-function/10746/3 "2017-06-27T15:34:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arnobmukherjee1988](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@arnobmukherjee1988](https://community.wolfram.com/u/arnobmukherjee1988)\
**Post date:** [June 28, 2017, 7:09am UTC](https://community.wolfram.com/t/minimize-a-function/10746/4 "2017-06-28T07:09:09Z")

</div>

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]]

```

---

<div class="post-metadata">

**Author:** ![arnobmukherjee1988](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@arnobmukherjee1988](https://community.wolfram.com/u/arnobmukherjee1988)\
**Post date:** [June 28, 2017, 7:14am UTC](https://community.wolfram.com/t/minimize-a-function/10746/5 "2017-06-28T07:14:22Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [June 28, 2017, 9:09am UTC](https://community.wolfram.com/t/minimize-a-function/10746/6 "2017-06-28T09:09:33Z")

</div>

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.
