Compute the Asymptotic Series

  Math 613 o Fall 2019 o Victor Matveev o Homework #5 o Problem 1
clear;
x = 0.1;           % Duh
N = 23;            % Maximal number of terms (a little higher than 20)
T = zeros(N, 1);   % Initialize array of terms
S = zeros(N, 1);   % Initialize the partial sum array
T(1) = x;          % First term
S(1) = x;          % First partial sum

for n = 2 : N
   T(n) = -T(n-1) * (n - 1) * x;  % Calculate (-1)^(n-1)*x^n*(n-1)! recursively
   S(n) = S(n-1) + T(n);          % (...Never calculate factorials directly!!)
end

%%%%%%%%%%%%%%%%%%%%%%%%  Now, plot the results: %%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(1,2,1);
plot(1:N, S, 'mo-', 'linewidth', 1);
xlabel('n'); ylabel('S(n)');
title('Partial sums');
axis tight;

subplot(1,2,2);
plot(1:N, abs(T), 'bo-', 'linewidth', 1);
xlabel('n'); ylabel('| Term_n |');
title('| Series Term |, logarithmic scale');
set(gca, 'yscale', 'log');
axis tight;