我需要在 x-y 平面中定义的一维线上拟合由三阶多项式组成的样条线。多项式的导数在关节处必须相等。预期输出是三阶多项式列表,由它们的起点(样条结)及其多项式系数定义。
我认为 scipy 的 splrep 函数非常适合此目的。但是我很难理解该函数的结果。例如,以下代码会产生以下结果:
x = np.linspace(0, 10, 100)
y = np.sin(x)
sp1 = scipy.interpolate.splrep(x, y, k = 3, t = [2, 6])
结果(sp1 中的“结”和“系数”数组):
knots: [ 0., 0., 0., 0., 2., 6., 10., 10., 10., 10.]
coefficients: [-0.32946251, 1.55647594, 0.19883333, -2.08984459, 2.79531098,
-1.14372454, 0. , 0. , 0. , 0. ]
根据文档和我的数学理解,我希望底层代码适合这些点上的 3 条样条线:一条从 0 到 2,一条从 2 到 6,一条从 6 到 10。这些样条线的方程是什么然后?为什么我只有 6 个系数?我不应该有 12 (3*4) 个吗?
注意:我知道有 splev 等函数来评估结果 - 我不需要它。我只需要样条线的参数。
最佳答案
这有点难以解释。
Notes
See
splev
for evaluation of the spline and its derivatives. The number of dimensions N must be smaller than 11. The number of coefficients in thec
array isk+1
less then the number of knots,len(t)
. This is in contrast withsplrep
, which zero-pads the array of coefficients to have the same length as the array of knots. These additional coefficients are ignored by evaluation routines,splev
andBSpline
.
由于这组样条插值函数环绕 FITPACK的子程序splev ,您必须了解系数的含义。在子例程的文档中:
c t : array,length n, which contains the position of the knots.
c n : integer, giving the total number of knots of s(x).
c c : array,length n, which contains the b-spline coefficients.
回到scipy
。它阐明了 BSpline formula同时还表明它如何使用系数以及它们的预期长度是多少。
为了确保您提供正确的输入,将其与 BSplines 的数学定义关联起来始终很重要。确定给定的 BSpline 阶数需要多少个节点和控制点。
关于python - 如何解释 scipy.interpolate.splrep 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54574497/