% Numerical Analysis written problem 2.27
% Part (a) x_i = -1 + 2i/n
% Part (b) x_i = cos((2i+1)/(2n+2))

% First produce interpolation nodes for n=5, 10, 20
n=[5 10 20];
x_space = 0;

for N = 1:3
nodes = zeros(1,size(n,2));
    for i = 1:n(N)+1
        %nodes(i) = -1 + 2*(i-1)/(n(N));
        nodes(i) = cos((2*(i-1)+1)*pi/(2*n(N) + 2));
    end
    for i = 1:(n(N))
        node_space = linspace(nodes(i),nodes(i+1),21);
        x_space = union(node_space,x_space);
    end
    l_sum = zeros(1, size(x_space,2));
    for i = 1:size(x_space,2)
        l_sum(1,i) = lebesgue(x_space(i),nodes);
    end
    subplot(1,3,N)
    title('Equidistant nodes')
    plot(x_space, l_sum)
end