0
我試圖運行下面的代碼:scipy.interpolate.pchip遇到錯誤
x = [0, 1, 2, 3, 4, 5]
y = [1, 2, 1.5, 2.5, 3, 2.5]
xs = np.linspace(x[0],x[-1],100)
curve = interpolate.pchip(x,y)
ys = curve(xs)
dys = curve.derivative(xs)
pl.plot(xs,ys,label=u'pchip')
pl.plot(xs,dys,label=u'derivative')
pl.plot(x,y,'o')
pl.legend(loc='best')
pl.grid()
pl.margins(0.1,0.1)
pl.show()
卻得到了這樣的錯誤:
ValueError Traceback (most recent call last)
<ipython-input-61-256eb8fb78c2> in <module>()
4 curve = interpolate.pchip(x,y)
5 ys = curve(xs)
----> 6 dys = curve.derivative(xs)#Construct a new piecewise polynomial
representing the derivative.
7 pl.plot(xs,ys,label=u'pchip')
8 pl.plot(xs,dys,label=u'1 class diff')
C:\Program Files\Anaconda3\lib\site-packages\scipy\interpolate\interpolate.py in derivative(self, nu)
1377
1378 """
-> 1379 if nu < 0:
1380 return self.antiderivative(-nu)
1381
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我使用的系統是Windows + Python3.5 + Anaconda + Jupyter筆記本。
謝謝。
這workds。非常感謝你! –
或'curve(xs,1)'來評估一階導數。 –