% Vicol Vlad, small changes by M.O.
% Project 6
% problem 5;

% Note: this program is NOT optimized for speed!

N=[5 8 12 17 20 25];

for k=1:6
    
if(k==1)
    subplot(3,2,1);
end
if(k==2)
    subplot(3,2,2);
end
if(k==3)
    subplot(3,2,3);
end
if(k==4)
    subplot(3,2,4);
end 
if(k==5)
    subplot(3,2,5);
end
if(k==6)
    subplot(3,2,6);
end

    x  = linspace (-5,5,1001);
    yp = x;
    yf = x;
    for i=1:length(x)
        yp(i) = LagrangeN(x(i),N(k));
        yf(i) = f(x(i));        
    end
    plot(x,yf,'r.',x,yp,'b.');
    %the function with red;
    % the polynomial with blue
 
end

% as we can claerly see from the graphs,
% as n increases the polynomial approximates our function more and more
% precise on the interval [-3.7,3.7] but clearly blows up more and more at
% the ends.
