p09: Polynomial interpolation in equispaced and chebyshev points#
%config InlineBackend.figure_format='svg'
from numpy import pi,inf,linspace,arange,cos
from numpy.linalg import norm
from scipy.interpolate import barycentric_interpolate
from matplotlib.pyplot import figure,subplot,plot,axis,title,text
N = 16
xx = linspace(-1.01,1.01,400,True)
figure(figsize=(10,5))
for i in range(2):
if i==0:
s = 'equispaced points'; x = -1.0 + 2.0*arange(0,N+1)/N
if i==1:
s = 'Chebyshev points'; x = -cos(pi*arange(0,N+1)/N)
subplot(1,2,i+1)
u = 1.0/(1.0 + 16.0*x**2)
uu = 1.0/(1.0 + 16.0*xx**2)
pp= barycentric_interpolate(x, u, xx)
plot(x,u,'o',xx,pp)
axis([-1.1, 1.1, -1.0, 1.5])
title(s+", N="+str(N))
error = norm(uu-pp, inf)
text(-0.6,-0.5,'max error='+str(error));