简体   繁体   中英

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.

The line f = interp1d(x, y, kind='cubic') is correct, but you're not importing interp1d correctly. You want something like from scipy.interpolate import interp1d or f = scipy.interpolate.interp1d(x, y, kind='cubic') .

Doing f(x) is kind of pointless. The whole point of interpolation is to create values other than your input points. As in, points not in the x array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM