% compare errors polynomials using equally spaced points and chebyshev points function poly3(n) set(0,'defaultlinelinewidth',3); set(0,'defaulttextfontsize',16); set(0,'defaultaxesfontsize',16); % set up the interpolating points % first equally spaced points dx = 2/n; x = -1:dx:1; % x are the interpolation points % Second: use Chebyshev points for interpolation npoints = linspace(0,n,n+1); xcheb = cos(pi*npoints/n); % Third; the "evaluation" points t = -1.:.025:1.; % evaluate and poly the polys using these points for i = 1:length(t) this_t = t(i); prodeq = 1; prodcheb = 1; for j = 0:n prodeq = prodeq*(this_t - x(j+1)); prodcheb = prodcheb*(this_t - xcheb(j+1)); end poly_equal(i) = prodeq; poly_cheb(i) = prodcheb; end hold off plot(t,poly_equal,'b'); hold on plot(t,poly_cheb,'m'); grid on legend('equally spaced points','chebyshev points'); end % ============================================================= % illustrate Runge phenomena using standard function f function y = f(x) y = 1./(1+25*x.*x); end