三次样条插值获取系数 [英] Cubic spline interpolation get coefficients

查看:81
本文介绍了三次样条插值获取系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 python 中有两个数组,我希望得到(并实际使用)这些点之间的三次样条插值.(即:我希望集成该功能).我非常喜欢使用 numpy scipy 的方法.

Say I have two arrays in python and I wish to get (and actually use) the cubic spline interpolation between those points. (IE: I wish to integrate the function). I would strongly prefer a way using numpy scipy.

我知道 scipy.interpolate.interp1d.然而,这只允许我评估点,例如非常简单的功能:

I know about scipy.interpolate.interp1d. However that only allows me to evalute the points, for say the very simply function of:

现在我可以做一些简单的事情:

Now I could do something simply like:

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

y = np.array([0,2,3,4,8,10,12,12,12,10,9,8,7,1,0,0,1,2])
x = np.array(range(len(y)))
xvals = np.linspace(0, len(y)-1, len(y)*100, endpoint = False)
func = scipy.interpolate.interp1d(x, y, kind = "cubic")
yvals = func(xvals)
plt.plot(xvals,yvals)
plt.plot(x,y, "o")

但是我希望进一步处理这个三次样条(即我需要得到积分).对于手动操作,我需要得到因子,所以:

However I wish to further process this cubic spline (ie I need to get the integration).. For manual doing things I need to get the factors, so:

a_i * x^3 + b_i * x^2 + c_i * x + d_i where i goes from 0 to n/3 

(n = 元素的数量 - 这只是第 i 个三次方的定义)

(n = number of elemetns - this is just the definition of the ith cubic)

因此,我希望有一个描述所有样条的元组(或二维数组)列表.- 或者一种获得第 i 个三次方的方法,真的,真的很想得到一个方便的x-to-i"来找到我目前所在的样条.

I hence expect a list of tuples (or 2d array) describing all splines. - Or a way to get the ith cubic, and really, really would love to get have a convenience "x-to-i" to find in which spline I am currently.

(当然,后一个问题是在排序列表中简单搜索大于引用的第一个值 - 如果需要,我可以轻松地手动完成).

(Though of course this latter problem is a simple search for first value larger than reference in a sorted list - I could do that by hand easily if need be).

推荐答案

只是一个可能有帮助的想法.来自文档,您可以以不同的方式获得三次样条插值,这可能对您有所帮助:

Just a thought that might help. From the docs, you can get a cubic spline interpolation in a different way which may help you:

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

y = np.array([0,2,3,4,8,10,12,12,12,10,9,8,7,1,0,0,1,2])
x = np.array(range(len(y)))
xvals = np.linspace(0, len(y)-1, len(y)*100, endpoint = False)
func = scipy.interpolate.splrep(x, y, s=0)
yvals = scipy.interpolate.splev(xvals, func, der=0)

# display original vs cubic spline representation for security...
plt.figure()
plt.plot(x, y, 'x', xvals, yvals, x, y, 'b')
plt.legend(['Linear', 'Cubic Spline'])
plt.axis([-0.05, 20, -2, 20])
plt.title('Cubic-spline interpolation')
plt.show()

这使您可以通过

pp = scipy.interpolate.spltopp(func[0][1:-1],func[1],func[2])

#Print the coefficient arrays, one for cubed terms, one for squared etc
print(pp.coeffs)

它还在页面上提供了一个示例,说明如何使用此三次样条表示进行积分(更改常数以适应您的情况,我希望 - 您的里程可能会有所不同):

It also gives an example on the page of how to integrate using this cubic spline representation (changed the constant to suit your situation, I hope - your mileage may vary):

def integ(x, tck, constant=0):
    x = np.atleast_1d(x)
    out = np.zeros(x.shape, dtype=x.dtype)
    for n in xrange(len(out)):
        out[n] = scipy.interpolate.splint(0, x[n], tck)
    out += constant
    return out

const_of_integration = 0
yint = integ(xvals, func, const_of_integration)

这篇关于三次样条插值获取系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆