如何在Mathematica中最小化带有约束的函数

时间:2019-04-11 08:37:50

标签: constraints wolfram-mathematica minimization

我想最小化以下功能:

Ef[J2_, D2_, θA1_, θC1_, \[Phi]A1_, \[Phi]C1_] := 
  J2 (Sin[θA1] Sin[θC1] (Cos[\[Phi]A1] Cos[\[Phi]C1] + 
         Sin[\[Phi]A1] Sin[\[Phi]C1]) + Cos[θA1] Cos[θC1]) - 
   D2 (Sin[θA1] Cos[\[Phi]A1] Cos[θC1] - 
      Cos[θA1] Sin[θC1] Cos[\[Phi]C1]);

例如J2=0.1;D2=0.1;,其中0 <= theta <= pi和0 <= phi <= 2 pi 我尝试过

Minimize[{Ef[J2, D2, θA1, θC1, \[Phi]A1, \[Phi]C1], 
{θA1 \[Epsilon][0, π], θC1 \[Epsilon][0, π], \[Phi]A1 \[Epsilon][0, 2 π], \[Phi]C1 \[Epsilon][0, 2 π], }}, {θA1, θC1, \[Phi]A1, \[Phi]C1}]

但是我重印了我的输入内容...

有什么建议吗?

谢谢, M

1 个答案:

答案 0 :(得分:1)

我将尝试一种不同的方法,看看它是否可以找到解决方案

Ef[J2_, D2_, θA1_, θC1_, φA1_, φC1_] :=  J2 (Sin[θA1] Sin[θC1]*
  (Cos[φA1] Cos[φC1] + Sin[φA1] Sin[φC1]) + Cos[θA1] Cos[θC1]) -
  D2 (Sin[θA1] Cos[φA1] Cos[θC1] - Cos[θA1] Sin[θC1] Cos[φC1]);
J2=1/10;D2=1/10;
NMinimize[{Ef[J2, D2, θA1, θC1, φA1, φC1], 0<=θA1<=Pi&&0<=θC1<=Pi&&
  0<=φA1<=2 Pi&&0<=φC1<=2 Pi}, {θA1, θC1, φA1, φC1},WorkingPrecision->32]

立即给我

{-0.1414213562373095048801688724209560058569711627807200358098`32., 
 {θA1 -> 1.3609367058891979125401402005460735210021598955174397243367`32., 
  θC1 -> 2.5660541110980434566496343521125515860033321979399030055079`32., 
  φA1 -> 3.1415926535897927498306628362843651999778557816889010918926`32., 
  φC1 -> 6.2831853071795854493988148705854136770428612595496860217646`32.}}

我注意到φA1几乎可以肯定是Pi,φC1几乎可以肯定是2 Pi

s=Simplify[Ef[J2, D2, θA1, θC1, φA1, φC1]/.{φA1->Pi,φC1->2 Pi}]

给予

(Cos[θA1 + θC1] + Sin[θA1 + θC1])/10

现在我尝试找到解决方案

FullSimplify[Minimize[{s, 0<=θA1<=Pi&&0<=θC1<=Pi}, {θA1, θC1}]]

给予

{-1/(5*Sqrt[2]), {θA1 -> Pi, θC1 -> Pi/4}}

请仔细检查以确保没有错误。

查看-Sqrt [2] / 10的局部最小值,并且满足您的约束条件

Length[Select[Partition[Flatten[Table[
  {Ef[J2, D2, θA1, θC1, φA1, φC1], θA1, θC1, φA1, φC1},
  {θA1,0,Pi,Pi/4},{θC1,0,Pi,Pi/4},{φA1,0,2Pi,Pi/64},{φC1,0,2 Pi,Pi/64}]],
  5],#[[1]]==-Sqrt[2]/10&]]

可能还会更多。