import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['FangSong']
plt.rcParams['axes.unicode_minus'] = False
xs = np.linspace(-1, 1, 11)
ys = np.array([-0.3, -0.5, -0.2, -0.3, 0, 0.4, 0.2, -0.3, 0.2, 0.5, 0.4])
xm = np.linspace(-1, 1, 201)
ym = ((xm**2-1)**3 + 0.5)*np.sin(2*xm) + np.random.random(201)/10 - 0.1
fs4 = np.poly1d(np.polyfit(xs, ys, 4))
fs5 = np.poly1d(np.polyfit(xs, ys, 5))
fs6 = np.poly1d(np.polyfit(xs, ys, 6))
fs7 = np.poly1d(np.polyfit(xs, ys, 7))
fs8 = np.poly1d(np.polyfit(xs, ys, 8))
fs9 = np.poly1d(np.polyfit(xs, ys, 9))
fs10 = np.poly1d(np.polyfit(xs, ys, 10))
fm2 = np.poly1d(np.polyfit(xm, ym, 2))
fm3 = np.poly1d(np.polyfit(xm, ym, 3))
fm4 = np.poly1d(np.polyfit(xm, ym, 4))
fm5 = np.poly1d(np.polyfit(xm, ym, 5))
fm6 = np.poly1d(np.polyfit(xm, ym, 6))
fm7 = np.poly1d(np.polyfit(xm, ym, 7))
plt.subplot(211)
plt.plot(xs, ys, c='r', ls='', marker='o')
plt.plot(xs, fs4(xs), label=u'四次多项式')
plt.plot(xs, fs5(xs), label=u'五次多项式')
plt.plot(xs, fs6(xs), label=u'六次多项式')
plt.plot(xs, fs7(xs), label=u'七次多项式')
plt.plot(xs, fs8(xs), label=u'八次多项式')
plt.plot(xs, fs9(xs), label=u'九次多项式')
plt.legend()
plt.subplot(212)
plt.plot(xm, ym, c='g', ls='', marker='.')
plt.plot(xm, fm2(xm), label=u'二次多项式')
plt.plot(xm, fm3(xm), label=u'三次多项式')
plt.plot(xm, fm4(xm), label=u'四次多项式')
plt.plot(xm, fm5(xm), label=u'五次多项式')
plt.plot(xm, fm6(xm), label=u'六次多项式')
plt.plot(xm, fm7(xm), label=u'七次多项式')
plt.legend()
plt.show()
py作图
最新推荐文章于 2023-07-25 19:55:58 发布