使用 Mathematica 查找函数的最小值

FindMinimum of function with Mathematica

我以这样的方式通过 Mathematica 安装了一些函数

parameter = {a, b};
parameter
data = {{0, 1.2}, {0, 0.1}, {0.1, 0.2}, {1.1, 0}}
Ftest[x_, y_] := a*x^2 + b*y^2
fit = FindMinimum[Total[(Ftest @@@ data - 2)^2], parameter]
ContourPlot[(Ftest[x, y] /. fit[[2]]) == 2, {x, 0, 1.5}, {y, 0, 1.5}, 
Epilog -> {Red, Point /@ data}]

有了合适的结果。然而,我的实际函数更复杂,由一个 sinus/cosinus/arccos 函数和一些 8 次方的函数组成。将我的实际代码与实际函数一起使用我得到一些错误:

The function value {3.74166 (-2.+81. (256. Power[<<2>>]+256. Power[<<2>>]+256. Power[<<2>>]) (Times[<<4>>]+Times[<<2>>])^4)^2} is not a list of real numbers with dimensions {1} at {apb3d,bpb3d,cpb3d,fpb3d,gpb3d,hpb3d} = {1.,1.,1.,1.,1.,1.}. >>

我想要拟合的函数位于 x 为 0 和 90 而 y 介于 0 和 2 之间的区域。但是,由于它是一系列正弦函数,因此拟合函数是对称的。我想要拟合的参数是-2 和 2 之间的值。已经执行了一些具有已知参数值的测试,因此实际功能是正确的。 Mathematica 是否需要进一步输入将参数搜索限制在何处?

更准确地说,我添加了我想要安装的功能。 源自:

mpb3d = 8;

sigxx = x;
sigyy = y;
sigzz = 0;
sigxy = z;
sigxz = 0;
sigyz = 0;

Ab3d = (sigyy - sigzz)*apb3d;
Bb3d = (sigzz - sigxx)*bpb3d;
Cb3d = (sigxx - sigyy)*cpb3d;
Fb3d = (sigyz)*fpb3d;
Gb3d = (sigxz)*gpb3d;
Hb3d = (sigxy)*hpb3d;

I2b3d = (Fb3d^2 + Gb3d^2 + Hb3d^2)/
    3 + ((Ab3d - Cb3d)^2 + (Cb3d - Bb3d)^2 + (Bb3d - Ab3d)^2)/54;
I3b3d = ((Cb3d - Bb3d)*(Ab3d - Cb3d)*(Bb3d - Ab3d))/54 + 
   Fb3d*Gb3d*
    Hb3d - ((Cb3d - Bb3d)*Fb3d^2 + (Ab3d - Cb3d)*
       Gb3d^2 + (Bb3d - Ab3d)*Hb3d^2)/6;

thetab3d = ArcCos[I3b3d/((I2b3d)^(3/2))];

phib3d = (3*I2b3d)^(mpb3d/2)*((2*Cos[(2*thetab3d + Pi)/6])^
     mpb3d + (2*Cos[(2*thetab3d - 3*Pi)/6])^
     mpb3d + (-2*Cos[(2*thetab3d + 5*Pi)/6])^mpb3d)

这导致了我想作为 phib3d 解决的功能:

   1 (1/54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
        apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (hpb3d^2 z^2)/
   3)^4 (256 Cos[
     1/6 (-3 \[Pi] + 
        2 ArcCos[(1/
              54 (bpb3d x + cpb3d (x - y)) (-bpb3d x - 
                apb3d y) (-cpb3d (x - y) + apb3d y) - 
             1/6 hpb3d^2 (-bpb3d x - apb3d y) z^2)/(1/
              54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
                  apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (
             hpb3d^2 z^2)/3)^(3/2)])]^8 + 
   256 Cos[1/
      6 (\[Pi] + 
        2 ArcCos[(1/
              54 (bpb3d x + cpb3d (x - y)) (-bpb3d x - 
                apb3d y) (-cpb3d (x - y) + apb3d y) - 
             1/6 hpb3d^2 (-bpb3d x - apb3d y) z^2)/(1/
              54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
                  apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (
             hpb3d^2 z^2)/3)^(3/2)])]^8 + 
   256 Cos[1/
      6 (5 \[Pi] + 
        2 ArcCos[(1/
              54 (bpb3d x + cpb3d (x - y)) (-bpb3d x - 
                apb3d y) (-cpb3d (x - y) + apb3d y) - 
             1/6 hpb3d^2 (-bpb3d x - apb3d y) z^2)/(1/
              54 ((bpb3d x + cpb3d (x - y))^2 + (-bpb3d x - 
                  apb3d y)^2 + (-cpb3d (x - y) + apb3d y)^2) + (
             hpb3d^2 z^2)/3)^(3/2)])]^8)

使用变量 x、y、z(11、22 和 12 方向的应力)和参数 apb3d、bpb3d、cpb3d、hpb3d。

以及拟合数据:

   nYoFIT3D112212 = {{-(160/313), 160/313, 0}, {1, 0, 0}, {290/313, 21/313, 78/313}, {236/
      313, 79/313, 137/313}, {8/17, 8/17, 152/313}, {76/313, 227/313, 131/
      313}, {21/313, 294/313, 79/313}, {0, 333/313, 0}}

我想通过以下方式解决:

parameter = {apb3d, bpb3d, cpb3d, hpb3d};

Ftest[x_, y_, z_] := phib3d

fittning = 
 FindMinimum[Total[(Ftest @@@ nYoFIT3D112212 - 2)^2], parameter]

ContourPlot[(Ftest[x, y, z] /. fittning[[2]]) == 2, {x, 0, 1.5}, {y, 
   0, 1.5}, {z, 0, 1.5}, Epilog -> {Red, Point /@ nYoFIT3D112212}];

感谢您详细说明您正在尝试做的事情。这对于获得可以使用的答案至关重要。

为了尽可能准确地找出错误,如果我评估这个

mpb3d=8; sigxx=x; sigyy=y; sigzz=0; sigxy=z; sigxz=0; sigyz=0;
Ab3d=(sigyy-sigzz)*apb3d; Bb3d=(sigzz-sigxx)*bpb3d;
Cb3d=(sigxx-sigyy)*cpb3d; Fb3d=(sigyz)*fpb3d;
Gb3d=(sigxz)*gpb3d; Hb3d=(sigxy)*hpb3d;
I2b3d=(Fb3d^2+Gb3d^2+Hb3d^2)/3+((Ab3d-Cb3d)^2+(Cb3d-Bb3d)^2+(Bb3d-Ab3d)^2)/54;
I3b3d=((Cb3d-Bb3d)*(Ab3d-Cb3d)*(Bb3d-Ab3d))/54+Fb3d*Gb3d*Hb3d-
  ((Cb3d-Bb3d)*Fb3d^2+(Ab3d-Cb3d)*Gb3d^2+(Bb3d-Ab3d)*Hb3d^2)/6;
thetab3d = ArcCos[I3b3d/((I2b3d)^(3/2))];
phib3d=(3*I2b3d)^(mpb3d/2)*((2*Cos[(2*thetab3d+Pi)/6])^mpb3d +
  (2*Cos[(2*thetab3d-3*Pi)/6])^mpb3d+(-2*Cos[(2*thetab3d+5*Pi)/6])^mpb3d);
nYoFIT3D112212={{-(160/313),160/313,0},{1,0,0},{290/313,21/313, 78/313},
  {236/313,79/313,137/313},{8/17,8/17,152/313},{76/313,227/313,131/313},
  {21/313,294/313,79/313},{0,333/313,0}};
parameter = {apb3d, bpb3d, cpb3d, hpb3d};
Ftest[x_, y_, z_] := phib3d;
Total[(Ftest @@@ nYoFIT3D112212 - 2)^2]

确切地看到你要给 FindMinimum 什么然后它显示

8*(-2+81*(((bpb3d*x+cpb3d*(x-y))^2+(-(bpb3d*x)-apb3d*y)^2+(-(cpb3d*(x-y))+apb3d*y)^2)/54+
  (hpb3d^2*z^2)/3)^4*(256*Cos[(-3*Pi+2*ArcCos[(((bpb3d*x+cpb3d*(x-y))*(-(bpb3d*x)-apb3d*y)*
  etc, etc, etc.

请注意,您所有的 x、y、z 都保留了下来,并且 none 被 nYoFIT3D112212 中的三元组所取代,就像我假设您所期望的那样。

这是我试图让您发现的关键步骤。

可能有几种不同的方法可以解决这个问题。一个是如果我替换

Total[(Ftest @@@ nYoFIT3D112212 - 2)^2]

Total[Map[(phib3d-2)^2/.{x->#[[1]],y->#[[2]],z->#[[3]]} &,nYoFIT3D112212]]

然后我确实看到你所有的 x、y、z 都被 nYoFIT3D112212 中的系数替换了。

然后这个

fittning=FindMinimum[
  Total[Map[(phib3d-2)^2/.{x->#[[1]],y->#[[2]],z->#[[3]]}&,nYoFIT3D112212]], parameter]

不再抱怨不是{Real}列表。

请非常仔细地检查所有这些,以确保在每一步中都对 x、y、z 进行了正确的替换,并且在我所做的事情中没有潜伏的其他错误。