%% Complex Exponentials in MATLAB
%
%% Examples of exp() in Matlab
% Let's investigate the following complex numbers in MATLAB:
% z_1 = e^{i \pi} = -1
% z_2 = e^{i \pi / 2} = i
% z_3 = e^{i 5 \pi /2} = e^{i \pi /2} = i
% z_4 = e^{8 \pi / 2} = e^{4 \pi } = e^{2 \pi} =1
% z_5 = e^{i \pi / 4} = \sqrt{2} + i\sqrt{2}
% z_6 = 2e^{i 2 \pi /3} = 2(1/2 + i\sqrt{3}/2) = 1 +  i\sqrt{3}

z1 = exp(1i*pi) % Euler's famous formula
z2 = exp(1i*pi/2)
z3 = exp(1i*5*pi/2)
z4 = exp(1i*8*pi/2)
z5 = exp(-1i*pi/4)
z6 = 2*exp(1i*2*pi/6)

%
%% Get the real part, imaginary part, conjugate, modulus, and argument of a complex number
%
% Use the real() function to extract the real component of a complex number:
% \mathrm{Re}(z_1) = -1
% \mathrm{Re}(z_2) = 0
% \mathrm{Re}(z_6) = 1

real(z1)
real(z2)
real(z6)

%
% Use the imag() function in MATLAB to extract the imaginary component of a complex number:
% \mathrm{Im}(z_1) = 0
% \mathrm{Im}(z_2) = 1
% \mathrm{Im}(z_6) = 1

imag(z1)
imag(z2)
imag(z6)

%
% Use the conj() function to find the complex conjugate.
% The complex conjugate of z is denoted by \bar{z}, or sometimes z^*.
% Recall z_6 = 2e^{i 2 \pi /3} = 1 +  i\sqrt{3}. Then \bar{z_6} = 2e^{-i 2 \pi /3} = 1 -  i\sqrt{3}.

z6
conj(z6)

%
% Use the abs() function to evaluate the complex magnitude, a.k.a. modulus:
% |z_1| = 1
% |z_2| = 1
% |z_6| =\sqrt{3}
% Note: When abs() is called on a real number, it returns the absolute value.

abs(z1)
abs(z2)
abs(z6)

% Use the angle() function to evalaute the argument / "angle" of a complex number. (Recall, for z = e^{i\theta}, we call \theta the "argument". We denote the "argument of z" using the notation \angle{z}. If a is the real part of z, and b is the imaginary part of z, such that z = a + bi, then \angle{z} = \arctan(b/a).)
% \angle{z_1} = \pi
% \angle z_2 = \pi/2
% \angle{z_6} = 2\pi/3

angle(z1)
angle(z2)
angle(z3)

%
%% Visualize complex numbers by plotting on the complex plane
%

z_array = [z1; z2; z3; z4; z5; z6];
real_array = real(z_array); % real() can be called on an array
im_array = imag(z_array); % imag() can also be called on an array
vec_origins = zeros(size(z_array));

quiver(vec_origins, vec_origins, real_array, im_array);
xlabel('Re') 
ylabel('Im') 
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
axis equal;
strings = {'z1';'z2';'z3';'z4';'z5';'z6'};
text(real_array,im_array,strings);

%
% Given a complex number z = Ae^{i\theta}, plot z and \bar{z}
% Play with this demo, and you'll notice that if z is real, then z = \bar{z}.
% Similarly, if z is pure imaginary, then z = -\bar{z}

A = 0.7; theta = 0.31;
z = A*exp(1i * theta);
z_array = [z, conj(z)]; real_array = real(z_array); im_array = imag(z_array); 
vec_origins = zeros(size(z_array));
quiver(vec_origins, vec_origins, real_array, im_array);
xlabel('Re'); ylabel('Im');
ax = gca; ax.XAxisLocation = 'origin'; ax.YAxisLocation = 'origin'; axis equal; xlim([-1 1]); ylim([-1 1]);
strings = {'z';'z*'}; text(real_array,im_array,strings);

%
%% Phasors
% Consider the function z(t) = Ae^{i(\omega t + \phi)}. This function is known as a phasor, because A, \omega, and \theta are assumed not to vary with t.

A = 0.8; t = 0; 
omega = 0.5;  phi = 0.43; 
z = A*exp(1i*(omega*t + phi));
z_array = [z]; real_array = real(z_array); im_array = imag(z_array); 
vec_origins = zeros(size(z_array));
quiver(vec_origins, vec_origins, real_array, im_array); xlabel('Re'); ylabel('Im');
ax = gca; ax.XAxisLocation = 'origin'; ax.YAxisLocation = 'origin'; axis equal; xlim([-1 1]); ylim([-1 1]);

% The real component (i.e. the "x-position") of the time-varying vector above can be thought of as a point on a cosine wave, and the imaginary compnent (i.e. the "y-position") of the vector above can be thought of as a point on a sine wave. The position of this point varies with time. See the animation below, where y(t) is a phasor. (Author: Antonio González Fernández, on Wikipedia, permission for use granted under the CC By-SA 3.0 license.)
% Here, imagine that y(t) = e^{i(\omega t + \phi)}
%
% See also this animation, by Lucas Viera (released on Wikimedia Commons under a public domain license).
% Here, imagine that \theta = i(\omega t + \phi)
%
%% Licensing
% This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported license.
%