% This code shows exmples of: 
%  Polynomial interpolation at EQUALLY SPACED points
%  Runge phenomena
%  vandermode matrix
% 
% See section 5.4 of the 
% "Computational Science and Engineering" textbook
% by Gilbert Strang


%% Target function
clc,clear,close all
N = 11;      
dx =0.001; xx = -1:dx:1;
yy = 1./(1+25*xx.^2);                % the target function
dx = 2/(N-1);
x = -1:dx:1; x=x';
y  = 1./(1+25*x.^2);


%% Interpolation
% Find a polynomial that EXACTLY passes through the points,
% using the vandermode matrix
v = vander(x);
q = v\y;
w = polyval(q,xx);

%% plot results
h1 = figure(1);
plot(xx,yy,  x,y, 'o', xx, w, '--k' , 'LineWidth',2)
legend('target function',  'sampled data points', 'polynomial interpolant at equally spaced points')
title(['N = ', num2str(N)])

