使用三次样条进行插值


#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))

所以这是我到目前为止插入一组数据的代码,我已经做到了这一点,但我被告知我有一个"整数除法或模乘以零",我已经玩过它,但我找不到我的错误。

此行会导致异常。

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)有点毫无意义。 插值的全部意义在于创建输入点以外的值。 如 in,点不在 x 数组中。

最新更新