% Taylor_Series_e.m - Greg Bucks - 10/8/2013
% Description: this script will compute the Taylor series of the function
% e^x about the point a = 0.  The script will prompt the user for the value
% of x and the number of terms to use and display the resulting
% approximation, the actual value of e^x (using exp), and the error.
% Usage: >> Taylor_Series_e;

% get values from user
x = input('Please enter the value of x: ');
N = input('Please enter the number of terms: ');

% compute actual value of exponential
actual = exp(x);

% compute Taylor series expansion
taylor_series = 0;
for k = 0:N-1
    taylor_series = taylor_series + x^k/factorial(k);
end

% compute error
error_value = abs(taylor_series - actual);

% display results
fprintf('The actual value of e^%0.2f is %0.4f.\n',x,actual);
fprintf('The Taylor series estimate of e^%0.2f is %0.4f.\n',x,taylor_series);
fprintf('The error between the actual and estimate is %0.4f.\n',error_value);