从 scipy.interpolate.splrep 获取三次样条的系数
Getting coefficients of a cubic spline from scipy.interpolate.splrep
我正在使用 scipy.interpolate.splrep 进行三次样条插值,如下所示:
import numpy as np
import scipy.interpolate
x = np.linspace(0, 10, 10)
y = np.sin(x)
tck = scipy.interpolate.splrep(x, y, task=0, s=0)
F = scipy.interpolate.PPoly.from_spline(tck)
我打印 t 和 c:
print F.x
array([ 0. , 0. , 0. , 0. ,
2.22222222, 3.33333333, 4.44444444, 5.55555556,
6.66666667, 7.77777778, 10. , 10. ,
10. , 10. ])
print F.c
array([[ -1.82100357e-02, -1.82100357e-02, -1.82100357e-02,
-1.82100357e-02, 1.72952212e-01, 1.26008293e-01,
-4.93704109e-02, -1.71230879e-01, -1.08680287e-01,
1.00658224e-01, 1.00658224e-01, 1.00658224e-01,
1.00658224e-01],
[ -3.43151441e-01, -3.43151441e-01, -3.43151441e-01,
-3.43151441e-01, -4.64551679e-01, 1.11955696e-01,
5.31983340e-01, 3.67415303e-01, -2.03354294e-01,
-5.65621916e-01, 1.05432909e-01, 1.05432909e-01,
1.05432909e-01],
[ 1.21033389e+00, 1.21033389e+00, 1.21033389e+00,
1.21033389e+00, -5.84561936e-01, -9.76335250e-01,
-2.60847433e-01, 7.38484392e-01, 9.20774403e-01,
6.63563923e-02, -9.56285846e-01, -9.56285846e-01,
-9.56285846e-01],
[ -4.94881722e-18, -4.94881722e-18, -4.94881722e-18,
-4.94881722e-18, 7.95220057e-01, -1.90567963e-01,
-9.64317117e-01, -6.65101515e-01, 3.74151231e-01,
9.97097891e-01, -5.44021111e-01, -5.44021111e-01,
-5.44021111e-01]])
所以我提供了 x 数组:
array([ 0. , 1.11111111, 2.22222222, 3.33333333,
4.44444444, 5.55555556, 6.66666667, 7.77777778,
8.88888889, 10. ])
Q.1:F.x(结)与原始 x 数组不同并且具有重复值(可能强制一阶导数为零?)。 F.x 中还缺少 x (1.11111111, 8.88888889) 中的某些值。有什么想法吗?
Q.2 F.c的形状是(4, 13)。我知道 4 来自三次样条拟合的事实。但是我不知道如何为我想要的 9 个部分中的每个部分 select 系数(从 x = 0 到 x=1.11111,x = 1.111111 到 x = 2.222222 等等)。在提取不同段的系数方面的任何帮助将不胜感激。
如果你想在曲线的特定位置打结,你需要使用 splrep
的参数 task=-1
并给出一个 interior[=32= 的数组] 结作为 t
参数。
t
中的节点必须满足以下条件:
If provided, knots t must satisfy the Schoenberg-Whitney conditions, i.e., there must be a subset of data points x[j] such that t[j] < x[j] < t[j+k+1], for j=0, 1,...,n-k-2.
请参阅文档 here。
然后你应该得到 F.c
以下大小 (4, <length of t> + 2*(k+1)-1)
对应于沿曲线的连续间隔(k+1
节点在曲线的两端添加 splrep
).
尝试以下操作:
import numpy as np
import scipy.interpolate
x = np.linspace(0, 10, 20)
y = np.sin(x)
t = np.linspace(0, 10, 10)
tck = scipy.interpolate.splrep(x, y, t=t[1:-1])
F = scipy.interpolate.PPoly.from_spline(tck)
print(F.x)
print(F.c)
# Accessing coeffs of nth segment: index = k + n - 1
# Eg. for second segment:
print(F.c[:,4])
我正在使用 scipy.interpolate.splrep 进行三次样条插值,如下所示:
import numpy as np
import scipy.interpolate
x = np.linspace(0, 10, 10)
y = np.sin(x)
tck = scipy.interpolate.splrep(x, y, task=0, s=0)
F = scipy.interpolate.PPoly.from_spline(tck)
我打印 t 和 c:
print F.x
array([ 0. , 0. , 0. , 0. ,
2.22222222, 3.33333333, 4.44444444, 5.55555556,
6.66666667, 7.77777778, 10. , 10. ,
10. , 10. ])
print F.c
array([[ -1.82100357e-02, -1.82100357e-02, -1.82100357e-02,
-1.82100357e-02, 1.72952212e-01, 1.26008293e-01,
-4.93704109e-02, -1.71230879e-01, -1.08680287e-01,
1.00658224e-01, 1.00658224e-01, 1.00658224e-01,
1.00658224e-01],
[ -3.43151441e-01, -3.43151441e-01, -3.43151441e-01,
-3.43151441e-01, -4.64551679e-01, 1.11955696e-01,
5.31983340e-01, 3.67415303e-01, -2.03354294e-01,
-5.65621916e-01, 1.05432909e-01, 1.05432909e-01,
1.05432909e-01],
[ 1.21033389e+00, 1.21033389e+00, 1.21033389e+00,
1.21033389e+00, -5.84561936e-01, -9.76335250e-01,
-2.60847433e-01, 7.38484392e-01, 9.20774403e-01,
6.63563923e-02, -9.56285846e-01, -9.56285846e-01,
-9.56285846e-01],
[ -4.94881722e-18, -4.94881722e-18, -4.94881722e-18,
-4.94881722e-18, 7.95220057e-01, -1.90567963e-01,
-9.64317117e-01, -6.65101515e-01, 3.74151231e-01,
9.97097891e-01, -5.44021111e-01, -5.44021111e-01,
-5.44021111e-01]])
所以我提供了 x 数组:
array([ 0. , 1.11111111, 2.22222222, 3.33333333,
4.44444444, 5.55555556, 6.66666667, 7.77777778,
8.88888889, 10. ])
Q.1:F.x(结)与原始 x 数组不同并且具有重复值(可能强制一阶导数为零?)。 F.x 中还缺少 x (1.11111111, 8.88888889) 中的某些值。有什么想法吗?
Q.2 F.c的形状是(4, 13)。我知道 4 来自三次样条拟合的事实。但是我不知道如何为我想要的 9 个部分中的每个部分 select 系数(从 x = 0 到 x=1.11111,x = 1.111111 到 x = 2.222222 等等)。在提取不同段的系数方面的任何帮助将不胜感激。
如果你想在曲线的特定位置打结,你需要使用 splrep
的参数 task=-1
并给出一个 interior[=32= 的数组] 结作为 t
参数。
t
中的节点必须满足以下条件:
If provided, knots t must satisfy the Schoenberg-Whitney conditions, i.e., there must be a subset of data points x[j] such that t[j] < x[j] < t[j+k+1], for j=0, 1,...,n-k-2.
请参阅文档 here。
然后你应该得到 F.c
以下大小 (4, <length of t> + 2*(k+1)-1)
对应于沿曲线的连续间隔(k+1
节点在曲线的两端添加 splrep
).
尝试以下操作:
import numpy as np
import scipy.interpolate
x = np.linspace(0, 10, 20)
y = np.sin(x)
t = np.linspace(0, 10, 10)
tck = scipy.interpolate.splrep(x, y, t=t[1:-1])
F = scipy.interpolate.PPoly.from_spline(tck)
print(F.x)
print(F.c)
# Accessing coeffs of nth segment: index = k + n - 1
# Eg. for second segment:
print(F.c[:,4])