[英]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))
So this is my code so far to interpolate a set of data and I have got this far but I am being told that I have a "integer division or modulo by zero" and I have had a play around with it but I cant find my mistake. 因此,到目前为止,这是我的代码,可以插入一组数据,但到目前为止,我被告知我有一个“整数除法或取零的模”,我一直在玩弄它,但是找不到错误。
This line causes the exception. 该行导致异常。
scipy.interpolate.interp1d('x', 'y', kind='cubic')
You can look at the traceback and see exactly which line caused the issue. 您可以查看回溯并准确查看引起问题的行。 It's doing this because you're giving it strings (
'x', 'y'
) when it wants arrays. 这样做是因为在需要数组时会给它提供字符串(
'x', 'y'
)。
The line f = interp1d(x, y, kind='cubic')
is correct, but you're not importing interp1d
correctly. 行
f = interp1d(x, y, kind='cubic')
是正确的,但是您没有正确导入interp1d
。 You want something like from scipy.interpolate import interp1d
or f = scipy.interpolate.interp1d(x, y, kind='cubic')
. 您想要
from scipy.interpolate import interp1d
或f = scipy.interpolate.interp1d(x, y, kind='cubic')
。
Doing f(x)
is kind of pointless. 做
f(x)
是没有意义的。 The whole point of interpolation is to create values other than your input points. 插值的整个点是创建输入点以外的值。 As in, points not in the
x
array. 如,点不在
x
数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.