[英]interpolation using cubic spline
#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 interp1d
或f = scipy.interpolate.interp1d(x, y, kind='cubic')
。
做f(x)
是没有意义的。 插值的整个点是创建输入点以外的值。 如,点不在x
数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.