插值

# hanbb
# come on!!!
import scipy.interpolate as spi
import matplotlib.pyplot as plt
import numpy as np

# 样条插值
def f(x):
    return np.sin(x)+0.5*x
x = np.linspace(-2*np.pi,2*np.pi,50)

ipo = spi.splrep(x,f(x),k=1)    # k 决定两点之间是几次方程拟合
iy  = spi.splev(x,ipo)          # 拟合值

'''
# 画图
plt.plot(x,f(x),'b',label="f(x)")
plt.plot(x,iy,'r.',label="interpolation")
plt.legend(loc=0)
plt.show()
'''

# 两点之间
xd = np.linspace(1.0,3.0,20)
lyd = spi.splev(xd,ipo)
# 画图
plt.plot(xd,f(xd),'b',label="f(x)")
plt.plot(xd,lyd,'r.',label="interpolation")
plt.legend(loc=0)
plt.show()

posted @ 2017-11-24 21:50  hbb360  阅读(248)  评论(0编辑  收藏  举报