%% Interpolation with polynomial

%% Example: f(x)=|x| on interval [-1,1]
% We want to approximate f(x)=|x| on the interval [-1,1] by a polynomial of degree <= 10.
% We choose 11 equidistant nodes.

x = linspace(-1,1,11);         % 11 equidistant nodes
xe = linspace(-1,1,1000);      % 1000 points on [-1,1] for plotting and finding the maximal error
y = abs(x);                    
ye = abs(xe);
plot(xe,ye,x,y,'o');           % plot f(x) and mark nodes with 'o'.
hold on

%% Find interpolating polynomial using modified Lagrange formula
% Note that you need to download modlagr.m from the course web page.

pe = modlagr(x,y,xe);          % find interpolating polynomial and evaluate at points xe
plot(xe,pe);                   % plot interpolating polynomial
hold off

%% Plot error and find $\|f-p\|_\infty$ (approximatively)

plot(xe,ye-pe);                % plot error
grid on; title('Interpolation error')
maxerr = norm(ye-pe,Inf)       % find maximal error at points xe



