習題7.3

等我刷把宗师發表於2024-11-06
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()
plt.rc('font', family='SimHei')
plt.rc('axes', unicode_minus=False)
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()


相關文章