2015-11-02 99 views
0
#plotted log values of Re and C(d) 
import numpy as np 
import matplotlib.pyplot as plt 
plt.plot([np.log(0.2),np.log(2), np.log(20), np.log(200), np.log(2000), np.log(20000)], [np.log(103), np.log(13.9), np.log(2.72), np.log(0.800), np.log(0.401), np.log(0.433)], 'r-^') 
plt.ylabel('C(D)') 
plt.xlabel('Re') 
plt.show() 


#Then we Interpolate 
import scipy 
from scipy.interpolate import interpolate 
scipy.interpolate.interp1d('x', 'y', kind='cubic') 
import matplotlib.pyplot as plt 
x = np.linspace[np.log(103), np.log(13.9), np.log(2.72), np.log(0.800), np.log(0.401), np.log(0.433)] 
y = [np.log(0.2), np.log(2), np.log(20), np.log(200), np.log(2000), np.log(20000)] 
f = interp1d(x, y, kind='cubic') 
plt.plot(x, f(x)) 

所以這是我的代碼到目前爲止插入一組數據,我得到了這麼多,但我被告知我有一個「整除或零模」,我有玩一玩,但我不能找到我的錯誤。使用三次樣條插值

回答

0

此行導致異常。

scipy.interpolate.interp1d('x', 'y', kind='cubic') 

您可以查看回溯並準確查看哪條線導致問題。它是這樣做的,因爲當它需要數組時,它會給它字符串('x', 'y')。

該行f = interp1d(x, y, kind='cubic')是正確的,但是您沒有正確導入interp1d。你想要類似from scipy.interpolate import interp1df = scipy.interpolate.interp1d(x, y, kind='cubic')

f(x)是一種毫無意義。整個插值點是創建除輸入點以外的值。如在中,點不在x陣列中。