7.3

2839663913發表於2024-11-18

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()

2023310143007

相關文章