%plots P_i(x) for i=0,...,N-1 and for x in [-w,w]
%f(x) = exp(x) is also plotted, in red

N = 4;
w = 2;
mesh = 100;      
x = linspace(-w,w,mesh);  %vector of mesh equally spaced points in [-w,w]
M = zeros(N,mesh);        %Taylor polynomial values, M(i+1,j) = P_i(x(j))
M(1,:) = ones(1,mesh);    %sets P_0(x(j)) = 1 for all j 
                          %M(i,:) is the ith row of M

for i=2:N
    for j=1:mesh
        M(i,j) = M(i-1,j)+x(j)^(i-1)/factorial(i-1);
    end
end

close all;    %closes all figure windows
hold on;      %allows plots to remain in the same window

for i=1:N
    plot(x,M(i,:))            %plots P_i(x) for i=0,...,N-1
end

f = @(x) exp(x);
fplot(f,[-w,w],'color','r')   %plots f on [-w,w], colored red

    



