scipy 三次样条插值

scipy 三次样条插值

3次样条插值即用两次连续可微的分段三次多项式插值数据,详细可参考 https://blog.csdn.net/bodybo/article/details/77335129

scipy.interpolate.CubicSpline

三次样条数据插值器,用两次连续可微的分段三次多项式插值数据。结果为PPoly类的对象,断点与给定数据匹配。

class scipy.interpolate.CubicSpline(x, y, axis=0, bc_type=‘not-a-knot’, extrapolate=Non )

Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable . The result is represented as a PPoly instance with breakpoints matching the given data.

#参数表
x:xarray_like, shape (n,)
包含自变量值的一维数组。值必须是实的、有限的和严格递增的顺序。
y: yarray_like
包含因变量值的数组。它可以有任意数量的尺寸,但沿轴的长度必须与x的长度匹配。值必须是有限的。
axis: int, optional
Axis along which y is assumed to be varying. Meaning that for x[i] the corresponding values are np.take(y, i, axis=axis). Default is 0.
假设y沿其变化的轴。含义为 x[i] 对应的值为 np.take(y,i,axis = axis).缺省为0.

bc_type: string or 2-tuple, optional, 字符串或2-tuple, 可选。
边界条件类型。由边界条件给出的两个附加方程需要确定每段多项式的所有系数。
如果bc_type是字符串,则指定的条件将应用于样条曲线的两端。可用条件为:

  • not-a-knot’ (默认值): 曲线末端的第一段和第二段是相同的多项式。当没有关于边界条件的信息时,这是一个很好的默认值。

  • periodic’: The interpolated functions is assumed to be periodic of period x[-1] - x[0]. The first and last value of y must be identical: y[0] == y[-1]. This boundary condition will result in y'[0] == y'[-1] and y''[0] == y''[-1].
    插值函数假定为周期函数,周期为从x[-1]到x[0]. y的第一个与最后一个值必须相等:y[0] == y[-1]. 这种边界条件的结果是 y'[0] == y'[-1] and `y’’[0] == y’’[-1],即 上下边界的一阶导数、二街导数相同。

  • clamped’: The first derivative at curves ends are zero. Assuming a 1D y, bc_type=((1, 0.0), (1, 0.0)) is the same condition. 曲线端点(上下限)的一阶导数为0. 假设1维的y数组,与 边界条件 bc_type = ((1, 0.0), (1, 0.0))相同。

  • ‘natural’: The second derivative at curve ends are zero. Assuming a 1D y, bc_type=((2, 0.0), (2, 0.0)) is the same condition. 曲线端点(上下限)的二阶导数为0. 假设1维的y数组,与 边界条件 bc_type = ((2, 0.0), (2, 0.0))相同。

    If bc_type is a 2-tuple, the first and the second value will be applied at the curve start and end respectively. The tuple values can be one of the previously mentioned strings (except ‘periodic’) or a tuple (order, deriv_values) allowing to specify arbitrary derivatives at curve ends:

    如果bc_type是2元组,则第一个值和第二个值将分别应用于曲线的起点和终点。元组值可以是前面提到的字符串之一(除了“周期性”)或元组(阶数、导数值),允许在曲线末端指定任意导数:

  • order: the derivative order, 1 or 2. 边界条件中导数的阶数,可以为1或2.

  • deriv_value: array_like containing derivative values, shape must be the same as y, excluding axis dimension. For example, if y is 1D, then deriv_value must be a scalar. If y is 3D with the shape (n0, n1, n2) and axis=2, then deriv_value must be 2D and have the shape (n0, n1).
    包含导数的数组,shape必须与因变量y相同。 比如,y是1维,deriv_value 必须是标量。 如果y是 3维 并且axis=2, 那么 deriv_value 必须是2D, 形状为(n0,n1)

extrapolate{bool, ‘periodic’, None}: optional, 可选.

​ If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. If None (default), extrapolate is set to ‘periodic’ for bc_type='periodic' and to True otherwise.
​ 如果是布尔值,则确定是基于第一个和最后一个间隔外推到越界点,还是返回NaNs。如果是“周期”,则使用周期外推。如果无(默认值),“extrapolate” 对于 “bc_type“=‘periodic’ 为periodic’, 其它为 ‘True’。

Parameters bc_type and interpolate work independently, i.e. the former controls only construction of a spline, and the latter only evaluation.
参数bc_type和插值是独立工作的,即前者只控制三次样条曲线的构造,后者只进行求值。

scipy.interpolate.PPoly

参数表:

scipy.interpolate.PPoly

# 分段多项式类
class scipy.interpolate.PPoly(c, x, extrapolate=None, axis=0)
# Piecewise polynomial in terms of coefficients and breakpoints
# The polynomial between x[i] and x[i + 1] is written in the local power basis:
# S = sum(c[m, i] * (xp - x[i])**(k-m) for m in range(k+1))
# where k is the degree of the polynomial.

参数表:

c **:**ndarray, shape (k, m, …) 为k次多项式,m个区间,即k行m列数据,每一列代表一个多项式; (Polynomial coefficients, order k and
m intervals)
x **:**ndarray, shape (m+1,) 为多项式区间断点,m+1 个断点有m个区间,必须升序或降序排列 (Polynomial breakpoints. Must be sorted in either
increasing or decreasing order.)
**extrapolate :**bool or ‘periodic’, optional,‘布尔型’ 或 ‘周期型’;
如果是bool型,确定是基于第一个和最后一个间隔外推到边界点,还是返回NaNs。如果是“周期”,则使用周期外推法。默认是真的。 (If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. Default is True.
axis : int, optional,整型可选,插值的轴向,缺省为0. (Interpolation axis. Default is zero.)

举例

from scipy.interpolate import CubicSpline
import matplotlib.pyplot as plt
# 构造x,y 数组,x为自变量、y为因变量
x = np.arange(10)
y = np.sin(x)
# 使用 CubiSpline 构建分段三次样条插值函数cs,cs即为scipy.interpolate.PPoly类的对象
# 默认情况下,三次样条端点边界条件为‘not-a-knot’ (默认值),曲线末端的第一段和第二段是相同的多项式。当没有关于边界条件的信息时,这是一个很好的默认值。
cs = CubicSpline(x, y)

cs为scipy.interpolate._cubic.CubicSpline 的对象,
#其中 cs.c为各分段多项式的系数,[a3i,a2i,a1i,a0i], 本案例有10个数据点,分割为9段,故有9个多项式,依次为:

  		p0			p1				p2			p3			p4			p5			p6			p7			p8
a3	-0.041850	-0.041850	0.146891	0.160545	0.036476	-0.120620	-0.178734	-0.025369	-0.025369
a2	-0.261272	-0.386822	-0.512372	-0.071699	0.409935	0.519363	0.157503	-0.378697	-0.454806
a1	1.144593	0.496499	-0.402696	-0.986768	-0.648533	0.280765	0.957632	0.736438	-0.097065
a0	0.000000	0.841471	0.909297	0.141120	-0.756802	-0.958924	-0.279415	0.656987	0.989358

cs的关键属性除cs.c外,还有cs.x,代表数据点

cs.c.shape, cs.x,cs(x), y
# 输出为 三次样条分段多项式的形状,x数据点,插值x数据点=原始数据y值
((4, 9),
 array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]),
 array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
        -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849]),
 array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
        -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849]))

插值调用接口简单即为 c s ( x i ) cs(x_i) cs(xi), x i x_i xi 可以为单个的数也可为数组

# 构造插值点
xs = np.arange(-0.5, 9.6, 0.1)

fig, ax = plt.subplots(figsize=(6.5, 4))
ax.plot(x, y, 'o', label='data')
ax.plot(xs, np.sin(xs), label='true')
ax.plot(xs, cs(xs), label="S-插值点")
# 一阶导数
ax.plot(xs, cs(xs, 1), label="S'-插值处的一阶导数")

ax.set_xlim(-0.5, 9.5)
ax.legend(loc='lower left', ncol=2)
plt.show()

正弦函数三次样条插值

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值