%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Math 613 * Fall 2018 * Homework 5 * Fall 2018 * Victor Matveev
%
%  Problem 1: Calculate f(x) = integral( exp(-t)/(1 - xt), 0, infty)
%             Estimate f(0.1)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;
x = 0.1;
a(1) = x;          % Second term in the series (the first one is 0!x^0 = 1
S(1) = 1 + a(1);   % Initial value of the partial sum

for n = 2 : 22
    a(n) = a(n-1) * x * n; % Avoid direct factorial and power calculation - use recursion!
    S(n) = S(n-1) + a(n);  % Sum the new term in the series
end

subplot(121);
plot(S, 'o-'); axis tight;
xlabel('N (terms in the partial sum)');
title('Plot of partial sums');
ylabel('S_N');

subplot(122);
plot(a, 'o-'); axis tight
xlabel('n');
ylabel('x^n n!');
title('n^{th} term size (x^n n!), on logarithmic scale');
set(gca,'yscale','log');