Wolfram mathematica n用数值积分法求极小值

Wolfram mathematica n用数值积分法求极小值,wolfram-mathematica,numerical-integration,Wolfram Mathematica,Numerical Integration,我试图通过用Mathematica最小化一个我知道为零的方程来求函数的系数。我的代码是: Clear[f]; Clear[g]; Clear[GetGood]; Clear[int]; Clear[xlist]; Xmax = 10; n = 10; dx = Xmax/n; xlist = Table[i*dx, {i, n}]; A = 3.5; slope = (A + 2)/3; f[x_, a_, b_, c_, d_, e_] :=a/(1 + b*x + c*x^2 + d*x^3

我试图通过用Mathematica最小化一个我知道为零的方程来求函数的系数。我的代码是:

Clear[f];
Clear[g];
Clear[GetGood];
Clear[int];
Clear[xlist];
Xmax = 10;
n = 10;
dx = Xmax/n;
xlist = Table[i*dx, {i, n}];
A = 3.5;
slope = (A + 2)/3;
f[x_, a_, b_, c_, d_, e_] :=a/(1 + b*x + c*x^2 + d*x^3 + e*x^4)^(slope/4 + 2);
g[x_, a_, b_, c_, d_, e_] :=Derivative[1, 0, 0, 0, 0, 0][f][x, a, b, c, d, e];
int[a_?NumericQ, b_?NumericQ, c_?NumericQ, d_?NumericQ, e_?NumericQ] :=
     Module[{ans, i},ans = 0;Do[ans =ans + Quiet[NIntegrate[
    y^-slope*(f[Sqrt[xlist[[i]]^2 + y^2 + 2*xlist[[i]]*y*m], a, b,
         c, d, e] - f[xlist[[i]], a, b, c, d, e]), {m, -1, 1}, {y,
      10^-8, \[Infinity]}, MaxRecursion -> 30]], {i, 1, 
 Length[xlist]}];
ans
];
GetGood[a_?NumericQ, b_?NumericQ, c_?NumericQ, d_?NumericQ,e_?NumericQ] := 
    Module[{ans},ans = Abs[Sum[3*f[x, a, b, c, d, e] + x*g[x, a, b, c, d,e],
       {x,xlist}]+2*Pi*int[a, b, c, d, e]];
        ans
];
NMinimize[{GetGood[a, b, c, d, e], a > 0, b > 0, c > 0, d > 0, 
 e > 0}, {a, b, c, d, e}]
我在最后一行之后得到的错误是:

Part::pspec: Part specification i$3002170 is neither an integer nor a list of integers. >>
NIntegrate::inumr: "The integrand (-(1.84529/(1+<<3>>+0.595769 Part[<<2>>]^4)^2.45833)+1.84529/(1+<<18>> Sqrt[Plus[<<3>>]]+<<1>>+<<1>>+0.595769 Plus[<<3>>]^2)^2.45833)/y^1.83333 has evaluated to non-numerical values for all sampling points in the region with boundaries {{-1,1},{\[Infinity],1.*10^-8}}"
Part::pspec:partspecification i$3002170既不是整数,也不是整数列表。>>
inumr:“被积函数(-1.84529/(1++0.595769部分[]^4)^2.45833)+1.84529/(1+Sqrt[Plus[]++0.595769+[]^2)^2.45833)/y^1.83333已对边界为{-1,1}、{[Infinity],1.*10^-8}的区域中的所有采样点求值为非数值。”
知道我为什么会出错吗


谢谢

将您的NMinimize更改为

NMinimize[{GetGood[a,b,c,d,e],a>0&&b>0&&c>0&&d>0&&e>0}, {a,b,c,d,e}]
使约束正确工作。他们的帮助页面应该真正展示一个使用多个约束的示例。这一页确实展示了一个例子

如果将int[]更改为

int[a_?NumericQ, b_?NumericQ, c_?NumericQ, d_?NumericQ, e_?NumericQ] :=
Module[{ans, i}, ans = 0; Do[
Print["First i=", i];
ans = ans + Quiet[NIntegrate[
    Print["Second i=", i]; 
    y^-slope*(f[Sqrt[xlist[[i]]^2 + y^2 + 2*xlist[[i]]*y*m], a,b,c,d,e] -
f[xlist[[i]], a,b,c,d,e]), {m,-1,1}, {y,10^-8, \[Infinity]}, MaxRecursion -> 30]],
{i, 1, Length[xlist]}];
ans];
你会看到

First i=1
Second i=1
....
First i=10
Second i=i$28850
其中,第一次调试打印从未显示i=i$nnnn,但第二次调试打印通常会显示,只有在NIntegrate内部,而不是外部,并且只有在我达到值10(xlist的长度)后,才取消分配值,此时您不能用符号下标,并且您会看到错误消息

您的NIntegrate中没有任何内容会改变i的值

我想你可能偶然发现了一个错误,Mathematica在写I的值


看看是否可以简化代码而不隐藏bug。如果你能让它变得更简单,并且仍然显示出问题,那么你可能更有可能让Wolfram承认你发现了一个bug。

谢谢,我会联系他们的