import numpy as np
from scipy.interpolate import lagrange
import matplotlib.pyplot as plt
import matplotlib
yx = lambda x: 1/(1+x**2)def fun(n):x = np.linspace(-5, 5, n+1)p = lagrange(x, yx(x)) # n次插值多项式return px0 = np.linspace(-5, 5, 100)plt.rc('font', family='SimHei')
plt.rc('axes', unicode_minus=False)
#plt.rc('text', usetex=True)
#plt.rc('font', size = 15)
#plt.style.use('fivethirtyeight')
N = [6, 8, 10]
s = ['--*b', '-.', '--c']
for k in range(len(N)):p = fun(N[k])plt.plot(x0, np.polyval(p,x0),s[k])
plt.plot(x0, yx(x0), 'r')
plt.legend(['$n=6$', '$n=8$', '$n=10$', '$1/(1+x^2)$'])
plt.title("龙格振荡现象")
plt.show()