  % Script File: ExpTaylor
  % Plots, as a function of n, the relative error in the 
  % Taylor approximation
  %
  %           1 + x + x^2/2! +...+ x^n/n! 
  %
  % to exp(x).
  %Explore a range of x-vaules for progressively longer summations
  close all
  clc
  clear
  nTerms = 50; %maximum number of terms in summation
  for x=[10 5 1 -1 -5 -10]  %range of argument to exp(x)
     figure
     fx = exp(x);%Exact answer
     s = 1;  %first (k=0)term
     term = 1;  %first (k=0) term
     for k=1:nTerms
        term = x.*term/k;%builds up the next term in series
        s = s+ term;%  Adds that term in
                     % s is tne summation after k-terms
        err(k) = abs(fx - s);  %err(k) is the absolute error with
                           %k-terms in the series
     end
     relerr = err/fx;%Err is a 50 term abs error vector
                        %fx is the exact answer 
                        %relerr is also 50-term vector
     semilogy(1:nTerms,relerr)
     ylabel('Relative Error in Partial Sum.')
     xlabel('Order of Partial Sum.')
     title(sprintf('x = %5.2f f(x) = %7.4e',x,fx)) 
     pause
  end
