python - Trying to understand scipy and numpy interpolation -


if have measured data function don't know (let's it's either not important, or computationally difficult), such as

x2 = [0, 1, 10, 25, 30] y2 = [5, 12, 50, 73, 23] 

and use numpy.interp find intermediate values, gives me linear interpolant between points , straight line:

xinterp = np.arange(31) yinterp1 = np.interp(xinterp, x2, y2) plt.scatter(xinterp, yinterp1) plt.plot(x2, y2, color = 'red', marker = '.') 

enter image description here

the example scipy.interpolate docs gives

x = np.linspace(0, 10, num=11, endpoint=true) y = np.cos(-x**2/9.0) f = interp1d(x, y) f2 = interp1d(x, y, kind='cubic') xnew = np.linspace(0, 10, num=41, endpoint=true) plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--') plt.legend(['data', 'linear', 'cubic'], loc='best') plt.show() 

enter image description here

with different kinds of interpolation smoother curve. if want points in smooth curve, rather curve? there function in numpy or scipy can give discrete points along smoothed curve?

you can generate function points , reassign them variable when generated them , plotted them:

y_lin = f(xnew) y_cub = f2(xnew) 

interp1d returns function can use generate data in turns can reassigned other variables , used way want. both outputs plotted together:

plt.plot(x, y, 'o', xnew, f(xnew), xnew, y_lin, '-', xnew, f2(xnew), xnew, y_cub, '--') plt.legend(['data', 'linear' ,'reassigned linear', 'cubic', 'reassigned cubic'], loc='best') plt.show() 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -