ex7.1
import numpy as np
import matplotlib.pyplot as plt
x0 = np.arange(1, 7)
y0 = np.array([16, 18, 21, 17, 15, 12])
A = np.vander(x0) # 范德蒙行列式
p = np.linalg.inv(A) @ y0 # 求插值多项式的系数
print("从高到低插值多项式的系数为:\n", np.round(p, 4))
prediction = np.polyval(p, [1.5, 2.6])
print("预测值为:\n", prediction)
xs = np.linspace(1, 6, 100)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x0, y0, 'o', zorder=10)
ax.plot(xs, np.polyval(p, xs))
fig.show()


浙公网安备 33010602011771号