% Runge's phenomenon
% ==================
% This code is to test the polynomial interpolation on f(x)=(1+25x^2)^(-1)
% for x between -1 and 1. It can be observed that high order polynomial
% interpolation has a large point-wise error.
% Test function f
f = @(x) 1./(1+25*x.^2);
% Mesh for the plots
X = linspace(-1,1,100);
% Plot f
plot(X, f(X), 'r-');
hold on;
% Choose degree of polynomials for interpolations
n = 5;
% Interpolating on equally distributed points
% For part b), Change this for Chebyshev nodes
x = 2*(0:n)/n-1;
% Values at the points
fv = f(x);
plot(x,fv,'ko');
% Interpolating
% Here we use Vandermonde matrix to get the coefficients (not the most
% efficent one)
% Feel free to change this part using Langrange/Newton repersentation
% and see if the results coincide (not mendatory for the homework).
c = vander(x)\fv';
V = vander(X);
pn = V(:,end-n:end)*c;
% Plot the interpolant Pn
plot(X,pn,'b-');
hold off;