平滑样条De Boor的方法

时间:2017-04-05 16:43:52

标签: python numpy scipy

是否有可能根据De Boor在Python中的方法找到平滑样条?用于数据近似。

之前我在matlab中使用了Smoothing spline,我需要在python中使用完全相同的算法。

1 个答案:

答案 0 :(得分:2)

您可能想要使用scipy.interpolate.CubicSpline。以下示例直接取自文档:

enter image description here

在此示例中,三次样条用于插值采样的正弦曲线。可以看出,样条连续性属性适用于一阶和二阶导数,并且仅违反三阶导数。还有另一个例子;不确定你到底需要什么。

这是产生图形所需的代码:

from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
y = np.sin(x)
cs = CubicSpline(x, y)
xs = np.arange(-0.5, 9.6, 0.1)
plt.figure(figsize=(6.5, 4))
plt.plot(x, y, 'o', label='data')
plt.plot(xs, np.sin(xs), label='true')
plt.plot(xs, cs(xs), label="S")
plt.plot(xs, cs(xs, 1), label="S'")
plt.plot(xs, cs(xs, 2), label="S''")
plt.plot(xs, cs(xs, 3), label="S'''")
plt.xlim(-0.5, 9.5)
plt.legend(loc='lower left', ncol=2)
plt.show()

您还可以查看scipy.interpolate.splrepscipy.interpolate.InterpolatedUnivariateSpline。此外,还有github repository可能有所帮助。您可以将这些方法与您使用的Matlab函数进行比较,并选择合适的方法。

相关问题