% DEMO:         APPROXIMATING e USING THREE DIFFERENT METHODS
%
% Program:      approx_e.m
% Written by:   
% For:          EE 311
%               Demo for Computer Lab 1
% Date:         5-31-2001
% Purpose:	    	Computes three approximations to e, based on
%     		      (i) integral definition of e
%     		     (ii) Euler's approximation of e
%     		    (iii) Taylor series approximation of e
clear all, close all, clf reset, clc

disp('This program computes integral, Euler, and')
disp('Taylor series approximations of e.')

NoT = input('Enter the number of terms to use: ');
NoT = round(abs(NoT));      % Make it a non-negative integer

if NoT < 2
    NoT = 2;                % NoT must be >= 2
end

nn = 1:NoT;                 % Array of n's (1,2,3, ..., NoT)
one = ones(1,length(nn));   % Array of ones

% (i) integral definition of e: integral from 1 to e of dt/t = 1
for n = 1:NoT
    ssz = 1.7/n;            % Step size for integration
    ssz2 = ssz/2;           % Half of step size
    igral=0;                % Initialize intgeral to zero
    
    for t=1+ssz2:ssz:3
        igral = igral + (ssz/t);    % Numerical integration
        if igral >= 1, break, end
    end
    
    e1(n) = t + ssz - (igral-1)*t;  % Approximation of e in n steps
end


% (ii) Euler's approximation of e: e=(1+1/n)^n
e2 = (one + one./nn).^nn;  % Approximation of e in n steps
                            % Implicit "for" loop

                            
% (iii) Taylor seris approximation of e: e = sum of 1/n!
fac = 1;                        % 1! (one factorial)
e3(1) = 2;                      % First approximation of e: 1/0! + 1/1!
for n = 2:NoT
    fac = n*fac;                % n! (n factorial); n! = n * (n-1)
    e3(n) = e3(n-1) + 1/fac;    % Approximation of e in n steps
end


% Reference e from MATLAB
eR = exp(1) * one;              % Array of e's

% Plot the results
plot(nn,e1,'r',nn,e2,'-b',nn,e3,'g',nn,eR,'k')

if NoT >= 5
    text(3,e1(2)-0.01,'Integral')
    text(4,e2(3)-0.03,'Euler')
    text(4,e3(3)-0.03,'Taylor')
end

grid
title('Integral, Euler, and Taylor Series Approximations to e')
xlabel('Number of Terms')
ylabel('Value of e')
axis([1 NoT 2 3.7])

pause

% Print the results
clc
disp('******************* RESULTS ****************************')
disp('');

% Format numbers so there are 13 digits total, 12 of which appear
% after the decimal point.
es = sprintf('%13.12f', exp(1));    % Matlab value of e
s = ['MATLAB''S value of e:         ' es];
disp(s)

s = ['Number of terms used:        ' num2str(NoT)];
disp(s)

es = sprintf('%13.12f', e1(NoT));    % Integral approx of e
s = ['Integral approximation of e: ' es];
disp(s)

es = sprintf('%13.12f', e2(NoT));    % Euler's approx of e
s = ['Euler''s approximation of e:  ' es];
disp(s)

es = sprintf('%13.12f', e3(NoT));    % Taylor's series approx of e
s = ['Taylor series approx. of e:  ' es];
disp(s)

% Compute approximation errors in percent
err1 = 100 * abs(e1(NoT)-exp(1)) / exp(1);
err2 = 100 * abs(e2(NoT)-exp(1)) / exp(1);
err3 = 100 * abs(e3(NoT)-exp(1)) / exp(1);

disp('')
errs = num2str(err1);
s = ['Integral approximation error (percent): ' errs];
disp(s)

errs = num2str(err2);
s = ['Euler''s approximation error (percent):  ' errs];
disp(s)

errs = num2str(err3);
s = ['Taylor series approx. error (percent):  ' errs];
disp(s)

disp('')
disp('********************************************************')
disp('')