7.3

import numpy as np
from scipy.interpolate import interp1d, interp2d, UnivariateSpline, griddata
import matplotlib.pyplot as plt
t0 = np.linspace(700, 780, 5)
v0 = np.array([0.0977, 0.1218, 0.1406, 0.1551, 0.1664])
f1 = interp1d(t0, v0)
f2 = interp1d(t0, v0, 'cubic')

tp = np.array([750, 770])
vp1 = f1(tp)
vp2 = f2(tp)
print("线性插值的预测结果为:", vp1)
print("三次样条插值的预测结果为:", vp2)

t = np.linspace(700, 780, 200)
v1 = f1(t)
v2 = f2(t)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(t, v1, '--', label='线性插值')
ax.plot(t, v2, '-', label='三次样条插值', color='#ff9900')
ax.scatter(t0, v0, label='数据', zorder=10, marker='.', s=15, color='r')
ax.legend(fontsize=8)
fig.show()

posted @ 2024-10-28 21:55  qi11  阅读(6)  评论(0编辑  收藏  举报