使用interpolate.splrep进行插值

时间:2018-01-15 16:26:36

标签: python scipy interpolation

下午好,我想在这里更改样条曲线中的绿线: enter image description here 所以,我写了这个:

import numpy as np
import math as m
import matplotlib.pyplot as plt
from scipy import interpolate

x=np.array([-1.51680376, -5.59528478,  3.02763056,  3.99336847,  
1.16267044,2.76931318, -0.07837944,  2.16852768, -1.81093278,  
1.82262034,-5.47060927,  1.49435114, -7.97536149,  1.05175547, 
-4.12788774,0.31526786, -4.4241668 ])

y=np.array([45,45,41.1314913,41,40.25582197,40,39.33855863,39,
38.45406219,38,37.69804062,37,37,36,36,35,35])

tck = interpolate.splrep(x, y)
xnew = np.arange(min(x),max(x), 200)
ynew = interpolate.splev(xnew, tck)

plt.plot(x,y,'g',xnew,ynew)  
plt.show()

我在这一行有一个问题:

f48=interpolate.splrep(x,y)

Python说输入数据有错误,但我不明白为什么......

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

问题是对x的调用中的参数splrep()没有排序,必须进行排序。根据文档,splrep()依赖于来自curfit的{​​{1}} Fortran例程。有关FITPACK的代码,请参阅https://github.com/scipy/scipy/blob/master/scipy/interpolate/fitpack/curfit.f

根据curfit中的评论:

curfit.f

c ier=10 : error. on entry, the input data are controlled on validity c the following restrictions must be satisfied. c -1<=iopt<=1, 1<=k<=5, m>k, nest>2*k+2, w(i)>0,i=1,2,...,m c xb<=x(1)<x(2)<...<x(m)<=xe, lwrk>=(k+1)*m+nest*(7+3*k) c if iopt=-1: 2*k+2<=n<=min(nest,m+k+1) c xb<t(k+2)<t(k+3)<...<t(n-k-1)<xe c the schoenberg-whitney conditions, i.e. there c must be a subset of data points xx(j) such that c t(j) < xx(j) < t(j+k+1), j=1,2,...,n-k-1 c if iopt>=0: s>=0 c if s=0 : nest >= m+k+1 c if one of these conditions is found to be violated,control c is immediately repassed to the calling program. in that c case there is no approximation returned. 。对于您的数据,不满足此条件。此外,x(1)<x(2)<x(3)...是一个多值函数。

相反,我认为你应该尝试调整y(x)x,并保持y排序 - 反转它:

y