插值插值.splrep

2024-09-21 02:53:01 发布

您现在位置:Python中文网/ 问答频道 /正文

下午好,我想在这里更改样条曲线中的绿线: 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()

我在这方面有个问题:

^{pr2}$

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

感谢您的帮助:)


Tags: importnumpymatplotlibasnppltmatharray
1条回答
网友
1楼 · 发布于 2024-09-21 02:53:01

问题是对splrep()调用中的参数x没有排序,必须对其排序。^根据文档,{}依赖于FITPACK中的curfitFortran例程。有关curfit的代码,请参见https://github.com/scipy/scipy/blob/master/scipy/interpolate/fitpack/curfit.f。在

根据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)是一个多值函数。在

相反,我认为您应该尝试匹配xy并保持{}排序-反转:

^{pr2}$

相关问题 更多 >

    热门问题