% Example taken from Chapter 8 of N. Trefethen's book 
% "Approximation Theory and Approximation Practice" (SIAM, 2013)

clear all, close all

figure(1)
x = chebfun('x'); f = 1./(1+25*x.^2); nn = 0:10:200; ee = 0*nn;
for j = 1:length(nn)
    n = nn(j); fn = chebfun(f,n+1); ee(j) = norm(f-fn,inf);
end
hold off, semilogy(nn,ee,'.'), grid on, axis([0 200 1e-17 10])
FS = 'fontsize';
xlabel('n'), ylabel('||f-p_n||');
title(['Geometric convergence of Chebyshev ' ...
      ' interpolants -- analytic function'],FS,9)

figure(2)
f = cos(20*x); nn = 0:2:60; ee = 0*nn;
for j = 1:length(nn)
    n = nn(j); fn = chebfun(f,n+1); ee(j) = norm(f-fn,inf);
end
semilogy(nn,ee,'.'), grid on, axis([0 60 1e-16 100])
xlabel('n'), ylabel('||f-p_n||');
title('Convergence of Chebyshev interpolants -- entire function',FS,9)
