% Taylor_Series_e_v2.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 determine the number of terms to use to reach a maximum error of
% 0.01.  It will display the approximation, the actual value of e^x (using 
% exp), and the error.
% Usage: >> Taylor_Series_e_v2;

% get values from user
x = input('Please enter the value of x: ');
tolerance = 0.01;

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

% compute Taylor series expansion
k = 0;
taylor_series = 0;
while abs(taylor_series - actual) > tolerance
    taylor_series = taylor_series + x^k/factorial(k);
    k = k + 1;
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);
fprintf('%d terms were used to produce the estimate.\n',k);