Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wolfram mathematica Mathematica中的函数和_Wolfram Mathematica - Fatal编程技术网

Wolfram mathematica Mathematica中的函数和

Wolfram mathematica Mathematica中的函数和,wolfram-mathematica,Wolfram Mathematica,我试图用曲线拟合一个函数的任意分布高斯和——每个函数都有自己的参数集。目前,如果我想使用二十个函数,我会执行以下操作 φ[α_?NumberQ, x_?NumberQ, xi_?NumberQ, c_?NumberQ] := ( c E^(- α (x - xi)^2/2))/Sqrt[α/π]; Data := Table[{n/50, N[f[n/50]]}, {n, -300, 300}]; model = φ[a1, x, x1, c1] + φ[a2, x, x2, c2] +

我试图用曲线拟合一个函数的任意分布高斯和——每个函数都有自己的参数集。目前,如果我想使用二十个函数,我会执行以下操作

φ[α_?NumberQ, x_?NumberQ, xi_?NumberQ, c_?NumberQ] := (
  c E^(- α (x - xi)^2/2))/Sqrt[α/π];
Data := Table[{n/50, N[f[n/50]]}, {n, -300, 300}];

model = φ[a1, x, x1, c1] + φ[a2, x, x2, c2] + φ[a3, x, x3, c3] + 
   φ[a4, x, x4, c4] + φ[a5, x, x5, c5] + φ[a6, x, x6, c6] + 
   φ[a7, x, x7, c7] + φ[a8, x, x8, c8] + φ[a9, x, x9, c9] + 
   φ[a10, x, x10, c10] + φ[a11, x, x11, c11] + φ[a12, x, x12, c12] + 
   φ[a13, x, x13, c13] + φ[a14, x, x14, c14] + φ[a15, x, x15, c15] + 
   φ[a16, x, x16, c16] + φ[a17, x, x17, c17] + φ[a18, x, x18, c18] + 
   φ[a19, x, x19, c19] + φ[a20, x, x20, c20];
nlm = NonlinearModelFit[Data, 
   model, {a1, x1, c1, a2, x2, c2, a3, x3, c3, a4, x4, c4, a5, x5, c5, a6, x6,
     c6, a7, x7, c7, a8, x8, c8, a9, x9, c9, a10, x10, c10, a11, x11, c11, 
     a12, x12, c12, a13, x13, c13, a14, x14, c14, a15, x15, c15, a16, x16, c16,
     a17, x17, c17, a18, x18, c18, a19, x19, c19, a20, x20, c20}, x];
这很有效,但手工创建这些线性组合很繁琐。这将是一个很好的函数的线性组合,系数向量A,席,和C。我只是不确定如何处理这个问题,我希望你们能对此提供一些见解

最好的


托马斯

我以前做过类似的事情:

params = Flatten[
   Table[{Subscript[a, i], Subscript[m, i], Subscript[c, i]}, {i, 1, 
     n}]];
model = Sum[
   Phi[Subscript[a, i], x, Subscript[m, i], Subscript[c, i]], {i, 1, 
    n}];
fit = NonlinearModelFit[data, model, params, x]];
只要用你所需要的高斯数替换n。显然,如果你有不同的基函数,你就必须做其他的事情,但是当你只处理一组(甚至两个)基函数时,这很有效

以下是一些概念验证代码:

Phi[x_, a_, b_, c_] := c Exp[-(x - a)^2/b^2]/(b Sqrt[\[Pi]])

n = 10;
Ap = RandomReal[{-5, +5}, {n}];
Bp = RandomReal[{0.2, 2}, {n}];
Cp = RandomReal[{-3, +3}, {n}];
f[x_] := Evaluate[Sum[Phi[x, Ap[[i]], Bp[[i]], Cp[[i]]], {i, n}]]

data = Module[{x, y},
   x = RandomReal[{-10, +10}, {3000}];
   y = f[x];
   Transpose[{x, y}]];
(* Force data to be precision to be 50 digits, so we can use higher precision in NLMF *)
data = N[Round[data * 10^50] / 10^50, 50];


params = Flatten@Table[{a@i, b@i, c@i}, {i, n}];
model = Sum[Phi[x, a@i, b@i, c@i], {i, n}];
fit = Normal@NonlinearModelFit[data, model, params, x, WorkingPrecision->50];

Show[ListPlot[data, PlotStyle -> Red], Plot[fit, {x, -5, +5}], 
 PlotRange -> All]
你可以尝试:

Phi[α_, x_, xi_, c_] := (c E^(- α (x - xi)^2/2))/Sqrt[α/π];

model = Sum[Phi[a@i, x, xx@i, c@i], {i, 20}];

nlm = NonlinearModelFit[Data, model, Flatten@Table[{a@i, xx@i, c@i}, {i, 20}], x]
编辑

未经测试,但我认为,对于不确定高斯数的情况,您还可以执行以下操作:

nlm[n_] := NonlinearModelFit[Data, Sum[Phi[a@i, x, xx@i, c@i], {i, n}]
                                   Flatten@Table[{a@i, xx@i, c@i}, {i, n}], x];

nlm[20]

我想你不知怎么搞混了
f
Phi
是的。我试过做
FindFit
,显然
FindFit
可以得到与
非线性模型相同的拟合,所以我从答案中删除了这一点。我想这是意料之中的,
NonlinearModelFit
更一般化了。@Mike,我对上面的代码()进行了修改,得到了以下错误:LinearAlgebra
BLAS
TRSV::oflow:计算过程中遇到了机器溢出,但只针对22个或更多的基函数。你对如何正确处理这个问题有什么建议吗?@Thomas:你有没有尝试过提高计算的精度?我将添加一个修改,演示如何执行此操作。@Thomas:溢出也可能是由于大量的基函数造成的。它在优化参数时尝试的一种拟合可能会产生太大而无法表示的结果。在这种情况下,您可能需要减少基函数的数量或提高计算的准确性和精度。您在最后缺少了一个x。我喜欢我们最终发布几乎完全相同的东西。@belisarius哇,这真是太棒了!非常感谢。我完全错过了扁平化命令,哇。