从图中识别 Mathematica 插值函数(不​​是 Hermite)

发布于 2024-11-09 23:48:43 字数 1521 浏览 3 评论 0原文

我正在对 Mathematica 如何进行列表插值进行逆向工程:

(* Fortunately, Mathematica WILL interpolate an arbitrary list *) 

tab = Table[a[i], {i,1,100}] 

f = Interpolation[tab] 

(* get the coefficient of each term by setting others to zero *) 

Plot[{f[42+x] /. {a[42] -> 0, a[43] ->0, a[44] -> 0, a[41] -> 1}}, 
 {x,0,1}] 

Plot[{f[42+x] /. {a[41] -> 0, a[43] ->0, a[44] -> 0, a[42] -> 1}}, 
 {x,0,1}] 

Plot[{f[42+x] /. {a[42] -> 0, a[41] ->0, a[44] -> 0, a[43] -> 1}}, 
 {x,0,1}] 

Plot[{f[42+x] /. {a[42] -> 0, a[43] ->0, a[41] -> 0, a[44] -> 1}}, 
 {x,0,1}] 

(* above is neither Hermite, nor linear, though some look close *) 

(* these are available at oneoff.barrycarter.info/STACK/ *) 

Table[f[42+x] /. {a[42] -> 0, a[43] ->0, a[44] -> 0, a[41] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff41.txt 

Table[f[42+x] /. {a[41] -> 0, a[43] ->0, a[44] -> 0, a[42] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff42.txt 

Table[f[42+x] /. {a[41] -> 0, a[42] ->0, a[44] -> 0, a[43] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff43.txt 

Table[f[42+x] /. {a[41] -> 0, a[42] ->0, a[43] -> 0, a[44] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff44.txt

编辑:谢谢,whuber!这正是我想要的。作为参考,系数为(按顺序):

(x-2)*(x-1)*x/-6
(x-2)*(x-1)*(x+1)/2
x*(x+1)*(x-2)/-2
(x-1)*x*(x+1)/6

I'm reverse-engineering how Mathematica does list interpolation:

(* Fortunately, Mathematica WILL interpolate an arbitrary list *) 

tab = Table[a[i], {i,1,100}] 

f = Interpolation[tab] 

(* get the coefficient of each term by setting others to zero *) 

Plot[{f[42+x] /. {a[42] -> 0, a[43] ->0, a[44] -> 0, a[41] -> 1}}, 
 {x,0,1}] 

Plot[{f[42+x] /. {a[41] -> 0, a[43] ->0, a[44] -> 0, a[42] -> 1}}, 
 {x,0,1}] 

Plot[{f[42+x] /. {a[42] -> 0, a[41] ->0, a[44] -> 0, a[43] -> 1}}, 
 {x,0,1}] 

Plot[{f[42+x] /. {a[42] -> 0, a[43] ->0, a[41] -> 0, a[44] -> 1}}, 
 {x,0,1}] 

(* above is neither Hermite, nor linear, though some look close *) 

(* these are available at oneoff.barrycarter.info/STACK/ *) 

Table[f[42+x] /. {a[42] -> 0, a[43] ->0, a[44] -> 0, a[41] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff41.txt 

Table[f[42+x] /. {a[41] -> 0, a[43] ->0, a[44] -> 0, a[42] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff42.txt 

Table[f[42+x] /. {a[41] -> 0, a[42] ->0, a[44] -> 0, a[43] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff43.txt 

Table[f[42+x] /. {a[41] -> 0, a[42] ->0, a[43] -> 0, a[44] -> 1}, 
 {x,0,1, 1/100}] >> /home/barrycarter/BCINFO/ONEOFF/STACK/coeff44.txt

EDIT: Thanks, whuber! That did exactly what I wanted. For reference, the coefficients are (in order):

(x-2)*(x-1)*x/-6
(x-2)*(x-1)*(x+1)/2
x*(x+1)*(x-2)/-2
(x-1)*x*(x+1)/6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-11-16 23:48:43

根据文档,插值器是分段多项式。这有点模糊,所以这里有一些东西需要调查。

您可以通过实验确定插值器是数据的线性函数。所有可能数据的良好基础由以下形式的向量组成:{1,0,...,0}, {0,1,0,...,0}, ..., {0,..., 0,1}。为此,让我们构建一个微小的函数来生成这些长度为 $n$ 的向量:

test[n_, i_] := Module[{x = ConstantArray[0,n]},x[[i]] = 1; x]

您可以通过尝试一些像这样的示例来确认线性度,其中系数 $a$ 和 $b$ 作用于 $i^\text{ th}$ 和 $j^\text{th}$ 长度为 $n$ 的基向量:

With[{a=1, b=2.5, n=5, i=2, j=3},
    Plot[{Interpolation[a test[n,i] + b test[n,j]][x], 
        a Interpolation[test[n,i]][x] + b Interpolation[test[n,j]][x]}, {x, 1, n}]
]

只有一条曲线,因为这两个函数是叠加的。

建立线性关系后,分析 $n$ 基本向量上的插值器值就足够了。您可以通过微分来确定多项式的次数。默认情况下,次数为 3,但您可以使用“InterpolatingOrder”参数进行修改。以下代码将使用长度为 $n$ 的数据的所有基向量,绘制由插值器的导数得出的明显分段常数曲线表,用于插值阶数 1 到 ioMax:

With[{n=7, ioMax = 5},
    Table[
        Module[{fns},
            fns = Table[Interpolation[test[n,i], InterpolationOrder->io], {i,1,n}];
            Table[Plot[Evaluate@D[f[#], {#,io}]&[x], {x,1,n},
                PlotRange->Full, PlotStyle->Thick, ImageSize->150], {f, fns}]
        ], {io, 1, ioMax}
    ]
] // TableForm

输出显示中断发生在整数值处参数的长度为 $n$ 的数据和度为 $d$ 的插值器最多有 $nd$ 个不同的段。这些信息应该可以帮助您完成大部分工作。

According to the documentation the interpolator is piecewise polynomial. That's a little vague, so there is something to be investigated here.

You can establish experimentally that the interpolator is a linear function of the data. A nice basis for all possible data consists of vectors of the form {1,0,...,0}, {0,1,0,...,0}, ..., {0,...,0,1}. To this end, let's build a tiny function to produce these vectors of length $n$:

test[n_, i_] := Module[{x = ConstantArray[0,n]},x[[i]] = 1; x]

You can confirm the linearity by trying some examples like this one, with coefficients $a$ and $b$ acting on the $i^\text{th}$ and $j^\text{th}$ basis vectors of length $n$:

With[{a=1, b=2.5, n=5, i=2, j=3},
    Plot[{Interpolation[a test[n,i] + b test[n,j]][x], 
        a Interpolation[test[n,i]][x] + b Interpolation[test[n,j]][x]}, {x, 1, n}]
]

There will be but a single curve because the two functions are superimposed.

Having established the linearity, it will suffice to analyze the interpolator's values on the $n$ basis vectors. You can determine the degrees of the polynomials by differentiation. By default the degree is 3, but you can modify that with the "InterpolatingOrder" parameter. The following code will plot a table of obviously piecewise constant curves resulting from the derivatives of the interpolator for interpolating orders 1 through ioMax, using all the basis vectors for data of length $n$:

With[{n=7, ioMax = 5},
    Table[
        Module[{fns},
            fns = Table[Interpolation[test[n,i], InterpolationOrder->io], {i,1,n}];
            Table[Plot[Evaluate@D[f[#], {#,io}]&[x], {x,1,n},
                PlotRange->Full, PlotStyle->Thick, ImageSize->150], {f, fns}]
        ], {io, 1, ioMax}
    ]
] // TableForm

The output shows that the breaks occur at the integer values of the argument and that there are at most $n-d$ distinct segments for data of length $n$ and an interpolator of degree $d$. This information should get you most of the way there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文