# polynomial interpolation # on uniformly distributed points import numpy as np import matplotlib.pyplot as plt from newtdd import newtdd from nest import nest from chebyshev import chebyshev # poits for vizualization Xf = np.linspace(-1,1,500) fs = 24 #font size f = lambda x: np.cos(np.pi/2*x) #f = lambda x: np.exp(-1./max(0.25-x**2,0))/np.exp(-4) #f = lambda x: 1/(1+10*x**2) plt.plot(Xf,f(Xf),'--',linewidth=3,markeredgewidth=3,markersize=10) plt.grid(True) plt.xlabel('x',fontsize=fs) plt.ylabel('y',fontsize=fs) plt.axis([-1, 1,-0.5,1.5]) plt.title('Function f',fontsize=fs) plt.show(block=False) # wait for a key press _ = input("Press [enter] to continue.") for N in range(2,101): # interpolation points Xb = np.linspace(-1,1,N) #Xb = chebyshev(-1,1,N) Yb = f(Xb) # calculate coefficients with Newton's divided differences c = newtdd(Xb,Yb) # evaluate the interpolating polynomial Yf= nest(c,Xf,Xb) # vizualize plt.clf() plt.plot(Xf,f(Xf),'--',Xf,Yf,'-',Xb,Yb,'x',linewidth=3,markeredgewidth=3,markersize=10) plt.show(block=False) plt.grid(True) plt.xlabel('x',fontsize=fs) plt.ylabel('y',fontsize=fs) plt.axis([-1, 1,-0.5,1.5]) plt.title('Interpolation using %3d points' % (N),fontsize=fs) plt.draw() plt.show(block=False) # wait for a key press _ = input("Press [enter] to continue.") end