%% Branches of Complex Logarithm
% Create two Cartesian grids for X and Y in the interval [-4,4].

[X,Y] = meshgrid(-4:0.25:4,-4:0.25:4);

%
% Calculate the complex logarithm on the grid. Use 1i for improved speed and robustness with complex arithmetic.

Z = log(X + 1i*Y);

%
% Make a surface plot of the imaginary portion of the function.

surf(X,Y,imag(Z))
grid on
hold on
xlabel('X')
ylabel('Y')
zlabel('Z')
title('Principal Branch of Im[$\log(X+iY)$]','Interpreter','latex')
view(40,40)

% On the complex plane, the natural logarithm is a multivalued function that winds around the origin.
%
% To obtain a different branch of the function, add 2\pi i to the Z values.

z2 = Z + 2*pi*1i;
surf(X,Y,imag(z2))
title('Two Branches of the Complex Logarithm')

% In this plot, the branches are stacked on top of each other and meet along the negative real axis.
% Copyright 2015 The MathWorks, Inc.