如何在所有数据点上放置导数零的约束以进行样条插值? [英] How can I put constraint of derivative zero at all data points for spline interpolation?
问题描述
scipy中是否有用于样条插值的方法,可以在每个数据点上使用对导数的约束?我找到了一个"scipy.interpolate.PiecewisePolynomial",但不推荐使用PiecewisePolynomial类.
Is there any method in scipy for spline interpolation in which I can use constraint on derivative at each data points? I found one "scipy.interpolate.PiecewisePolynomial" but PiecewisePolynomial class has been deprecated.
推荐答案
是.
scipy.interpolate
中的BPoly
类具有一种构造方法
Yes.
The BPoly
class in scipy.interpolate
has a method that constructs
伯恩斯坦基础上的分段多项式,与断点处的指定值和导数兼容.
a piecewise polynomial in the Bernstein basis, compatible with the specified values and derivatives at breakpoints.
如scipy
参考中所述
as stated in the scipy
reference, here.
Python3
上的基本用法可以如下:
Basic usage on Python3
can go as following:
from numpy import linspace, sin, pi
from scipy.interpolate import BPoly, CubicSpline
xi = linspace(0, pi, 5)
xnew = linspace(0, pi, 50)
yi = sin(xi)
ynew = sin(xnew)
yder = [[yi[i], 0] for i in range(len(yi))]
cubic = CubicSpline(xi, yi)
bpoly = BPoly.from_derivatives(xi, yder)
y_bpoly = bpoly(xnew)
y_cubic = cubic(xnew)
解释
此程序为senoid的第一个半周期创建两个样条插值,一个使用CubicSpline
类,一个使用BPoly
类的from_derivatives
方法,将导数设置为0
在原始曲线的每个点上.
Explaining
This program creates two spline interpolations for the first semi-period of a senoid, one using the CubicSpline
class, and one using the from_derivatives
method of the BPoly
class, setting the derivative as 0
at each point of the original curve.
与该方法有关的主要问题是,除非您在每个点上指定导数,否则该算法不能保证平滑过渡.但是,可以保证在指定的点上得到导数.不过,这应该不会出现问题,因为您要查找的是在每个点上将导数设置为0
.
The main issue regarding this method is that, unless you specify the derivatives at each point, the algorithm doesn't guarantee smooth transitions. The derivative is, however, guaranteed at the points it was specified. Still, this should not present a problem since what you're looking for is to set the derivative as 0
at each point.
使用以下内容,您可以正确看到不同的结果,如此图片 :
Using the following you can properly see the different results, as in this image:
import matplotlib.pyplot as plt
plt.figure()
plt.plot(xnew, y_bpoly, '-g', xnew, ynew, '--c', xnew, y_cubic, '-.m', xi, yi, '*r')
plt.legend(['BPoly', 'True', 'Cubic', 'Points'])
plt.title('Spline interpolation')
plt.grid()
plt.show()
这篇关于如何在所有数据点上放置导数零的约束以进行样条插值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!