% Generates Talyor series approximation of e^x of degree n, and computes error
% David Bloore, July 2011

clc;
clear all;

n=input('Degree of Taylor Series approximation?');

boolean1=0;

while boolean1<1
    x0=input('Lower bound of domain?');
    xn=input('Upper bound of domain?');
    if x0<xn
        boolean1=1;
    end
end

d=0;
while d<2
    d=input('Number of points along x for which e^x, Taylor, and error will be evaluated?');
end

x=linspace(x0,xn,d);
f=exp(x);
taylor=ones(1,d);

counter=1;
while counter<n+1
    taylor=taylor+(x.^counter)/factorial(counter);
    counter=counter+1;
end

error=taylor-f;

plot(x,f,'-',x,taylor,'--',x,error,'-.');
title(['e^x Approximated by Taylor Series of Degree ' sprintf('%d',n) ' on x\in [' sprintf('%d,%d',x0,xn) ' ]']);
xlabel([sprintf('%d',x0) ' \leq x \leq ' sprintf('%d',xn)]);
ylabel('e^x');
legend('e^x','taylor series approximation','error (taylor-e^x)','Location','best');
grid on;