%------------------------------------------------------------
% Taylor expansion of Log about z0:
%------------------------------------------------------------
% The region of convergence for a Taylor expansion is the 
% radius of the largest circular disk that can be 
% established about the expansion point. In the case of Log, 
% the "stopping" singularity is at z = 0. 
%------------------------------------------------------------
function log_series(z0)

x   = linspace(0,2*z0);
y   = zeros(1,length(x));

% Number of Taylor terms to use:
%------------------------------------------------------------
N   = 20;

for j = 1:length(x)
    sum = log(z0);
    for k = 1:N
        term = nth_derivative(k,z0)/factorial(k)*(x(j)-z0)^k;
        sum  = sum+term;
    end
    y(j) = sum;
end

%------------------------------------------------------------
% Results:
%------------------------------------------------------------
plot(x,y);
hold on;
y2 = log(x);
plot(x,y2,'r');
grid on;
hold off;
xlabel('x');
ylabel('Log(x)');
title('Log(x): Red; Taylor expansion: Blue');

%------------------------------------------------------------
% Calculate the coefficients
%------------------------------------------------------------
function coeff = nth_derivative(n,z0)
coeff = (-1)^(n+1)*factorial(n-1)/(z0^n);