【问题标题】:NMinimize with numerical integratingNMinimize 与数值积分
【发布时间】:2013-11-24 11:15:41
【问题描述】:

我试图通过最小化一个我知道在 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}}"

任何想法为什么我会收到错误?

谢谢

【问题讨论】:

    标签: wolfram-mathematica numerical-integration


    【解决方案1】:

    将您的 NMinimize 更改为

    NMinimize[{GetGood[a,b,c,d,e],a>0&&b>0&&c>0&&d>0&&e>0}, {a,b,c,d,e}]
    

    让您的约束正常工作。他们的帮助页面应该真正显示使用多个约束的示例。这个旧页面确实显示了一个示例。

    http://reference.wolfram.com/legacy/v5_2/functions/AdvancedDocumentationNMinimize

    如果您将 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 但第二个调试打印确实经常显示 i 仅在您的 NIntegrate 内部而不是在它外部分配一个值,并且只有在 i 达到值 10 之后,你的 xlist 的长度,此时你不能用符号下标,你会得到你看到的错误消息。

    NIntegrate 内部没有任何东西会改变 i 的值。

    我认为您可能偶然发现了一个错误,即 Mathematica 正在写入 i 的值。

    看看你是否可以在不隐藏错误的情况下简化代码。如果您可以使它更简单并且仍然显示问题,那么您可能更有可能成功让 Wolfram 承认您发现了一个错误。

    【讨论】:

    • 谢谢,我会联系他们的
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多