%% Complex Numbers
% Table of Contents
% 1 Imaginary Numbers
%     Examples: Imaginary Numbers
%     Addition and Subtraction
%     Multiplication
%     Division
%     Powers (Multiplications/Divisions by )
% 2 Complex Numbers
%     Case 1: The need for Complex Numbers
%         CM2104 RECAP: The 3 possible discriminant cases visualised
%     Case 2: The need for Complex Numbers: Applications
%     Definition of Complex Number
%     Recap: Notation – A summary
%         Examples
%     Complex Number Arithmetic
%         The Complex Conjugate
%         Complex Number Division
%     Comparing Two Complex Numbers
%         Equality
%         Corollary: The complex number zero
% 3. Visualising Complex Numbers: The Complex Plane
%     Argand Diagrams
%         Exercise
%     Polar form of complex numbers
%         Measuring the Argument or Phase of Complex Number
%         Example
%         Exercise
%     Converting between the Cartesian and Polar Form
%         Cartesian to Polar Conversion
%         Polar to Cartesian Conversion
%         Exercise
%     Trigonometric form
% 4 MATLAB and Complex Numbers
%     Checking whether is a variable is complex or real
%     Declaring Complex Numbers in MATLAB
%         Using the complex() function:
%     MATLAB: real, imaginary, magnitude and phase
%     MATLAB Complex Arithmetic
%     Plotting Complex Numbers in MATLAB
%         The compass() function
%
%
%% 1 Imaginary Numbers
% There are some equations, for example x^2 +1 = 0, for which we cannot yet find solutions.
% x^2 +1 = 0\\ x^2 = -1\\ x = \pm {\bf \sqrt{-1}?}
%
% The Problem: We cannot (yet) find the square root of a negative number using real numbers since:
% When any real number is squared the result is either positive or zero, i.e. for all real numbers n^2 \geq 0, n \in \mathbb{R}. (Note: \mathbb{R}denotes the set of all real numbers)
% We, therefore. need another category of numbers, the set of numbers whose squares are negative real numbers.
% Members of this set are called imaginary numbers.
% We define \sqrt{-1} = i
% Note: If you read engineering books rather than maths books you may see j used in place of i - this is just a quirk in notation.
% Do not worry about this. As We will see MATLAB undertsands both i and j notation but strictly enforces i!
% Every imaginary number can be written in the form:
% {{ni}}              where n is real (\in \mathbb{R}) and {i = \sqrt{-1}}.
%
%% Examples: Imaginary Numbers
%
% Examples:
% {\bf \sqrt{-16}} = \sqrt{16\times-1} = \sqrt{16}\times \sqrt{-1} = {\bf \pm 4i}
% {\bf \sqrt{-3}} = \sqrt{3 \times -1} = \sqrt{3}\times \sqrt{-1} = {\bf \pm i\sqrt{3}}
% {\bf (-121)^{\frac{1}{2}}} = \sqrt{-121} = \sqrt{123 \times -1} = \sqrt{121}\times \sqrt{-1} = {\bf \pm 11i}
%%
%% Addition and Subtraction
%
% Imaginary numbers can be added to or subtracted only from other  imaginary numbers.
%
% Examples:
% 7 i -2i = 5i
% 4 i + \sqrt{3}i = (4 + \sqrt{3}) i
% Note: i simply behaves like a special algebraic variable.
%
%% Multiplication
%
% When imaginary numbers are multiplied together the result is a real number.
% Example: 2i \times 5i = 10\times i^2
% but we know i = \sqrt{-1} , and therefore i^2 = -1
% Hence 10 \times i^2 = 10 \times -1 = {\bf -10}
%%
%% Division
%
% Imaginary numbers when divided give a real number result, e.g.:
% \frac{6i}{3i}= 2
%
%% Powers (Multiplications/Divisions by i)
% Powers of i can always be simplified
% Examples:
% i^3 = i^2\times i = -1\times i = -i
% i^{-1} = \frac{1}{i} = \frac{1}{\sqrt{-1}} = \frac{1}{\sqrt{-1}}\times \frac{\sqrt{-1}}{\sqrt{-1}} = \frac{\sqrt{-1}}{-1} = -\sqrt{-1}= -i
%
%
%% 2 Complex Numbers
%% Case 1: The need for Complex Numbers
% Consider the quadratic equation x^2 + 2 x + 2 = 0.
% Using the quadratic formula we get:
% x = \frac{-b \pm \sqrt{b^2 -4ac}}{2a}= \frac{-2 \pm \sqrt{2^2 - 4(2)}}{2} = \frac{-2 \pm \sqrt{-4}}{2} =  \frac{-2 \pm 2i}{2} = -1 \pm i
% So x = -1 + i or -1 -i
% x is now a number with a real number part (1) and an imaginary number part    (\pm i).
% x is an example of a complex number.
% Recall: If b^2 - 4ac < 0 then the equation has complex roots.
%% CM2104 RECAP: The 3 possible discriminant cases visualised
%
% (i) Two equal roots               (ii) Two distinct roots                  (iii) No real roots
%
%
% The above diagrams of example instances of a x^2 + bx + c (a \neq 0)   show the three possible cases:
%
% In (i), the curve touches the x-axis, i.e.  for y = 0 there are  two equal values  of x -  \mathbf{b^2 - 4ac = 0}.
% poly2_3.m:
%

figure;

p = [1, 0, 0]; % Create a polynomial f(x) = x^2


% Create some x values to evaluate f(x)
xmin = -5.0;
xmax = 5.0;
xstep = 0.2;

yoffset = 0.0; % extend axes beoynd range of y for plot

x=xmin:xstep:xmax;  % Make x

y = polyval(p,x); %evaluate polynomial

plot(x,y); % plot it

%%%  Plot X and Y axes and label

title('f(x) = x^2')

hold on
plot([xmin xmax],[0 0],'-k'); % X Axis
plot([0 0],[min(y)-yoffset max(y)+yoffset],'-k'); % Y Axis


axis tight % force concise display of plot


roots_p = roots(p)  % Solve the Equation  


% Plot Roots

plot(roots_p(1),0,'o','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',10);



plot(roots_p(2), 0,'o','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',10);
hold off;

%
%
% In (ii), the curve cuts the x-axis, i.e.  for y=0 there are two real distinct values of x,   i.e. real roots -  \mathbf{b^2 - 4ac > 0}
% .
% poly2.m:

figure;

p1 = [1, 0, -4]; % Create a polynomial f(x) = x^2 -4


% Create some x values to evaluate f(x)
xmin = -5.0;
xmax = 5.0;
xstep = 0.2;

yoffset = 1.0; % extend axes beoynd range of y for plot

x=xmin:xstep:xmax;  % Make x

y = polyval(p1,x); %evaluate polynomial

plot(x,y); % plot it

%%%  Plot X and Y axes and label

title('f(x) = x^2 - 4')

hold on
plot([xmin xmax],[0 0],'-k'); % X Axis
plot([0 0],[min(y)-yoffset max(y)+yoffset],'-k'); % Y Axis


axis tight % force concise display of plot


roots_p1 = roots(p1)% Solve the Equation  


% Plot Roots

plot(roots_p1(1),0,'o','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',10);



plot(roots_p1(2), 0,'o','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',10);
hold off;

%
% In (iii), the does not cut the x-axis, i.e.  for y = 0 there is  no real values of x, i.e.  complex roots - \mathbf{b^2 - 4ac < 0}.
% poly2_2.m:

figure
p = [3, 1, 2]; % Create a polynomial f(x) = 3x^2 + x + 2


% Create some x values to evaluate f(x)
xmin = -5.0;
xmax = 5.0;
xstep = 0.2;

yoffset = 0.0; % extend axes beoynd range of y for plot

x=xmin:xstep:xmax;  % Make x


y = polyval(p,x); %evaluate polynomial

plot(x,y); % plot it


%%%  Plot X and Y axes and label

title('f(x) = 3x^2 + x + 2')

hold on
plot([xmin xmax],[0 0],'-k'); % X Axis
plot([0 0],[min(y)-yoffset max(y)+yoffset],'-k'); % Y Axis


axis tight % force concise display of plot


roots_p = roots(p)  % Solve the Equation  


% Plot Roots
% In general roots of polynomial may be comlex.
hold on;

[n, m] = size(roots_p); % n = how many roots

for i = 1:n

    if isreal(roots_p(i)) % NOTE in this case no REAL roots

        plot(roots_p(i),0,'o','LineWidth',2,...
                'MarkerEdgeColor','k',...
                'MarkerFaceColor','r',...
                'MarkerSize',10);
    end



end
hold off;

%
%% Case 2: The need for Complex Numbers: Applications
%
% Very Useful Mathematical Representation, to name a few:
% Widely used in many branches of Mathematics, Engineering, Physics and other scientific disciplines
%                      Control theory
%                      Advanced calculus: Improper integrals, Differential equations, Dynamic equations
%                      Fluid dynamics --- potential flow, flow fields
%                      Electromagnetism and electrical engineering: Alternating current, phase induced in systems
%                      Quantum mechanics
%                      Relativity
%                      Geometry: Fractals ( e.g. the Mandelbrot set and Julia sets), Triangles --- Steiner inellipse
%                      Algebraic number theory
%                        Analytic number theory
% Signal analysis: Essential for digital signal and image processing (Phasors) — studied later.
%
%% Definition of Complex Number
%
% A complex number is a number of the form z = a + bi
% that is a number which has a real and an imaginary part.
% a and b can have any real value including 0. ( a,b \in \Re )
% E.g.
%             3 + 2i,
%             6 - 3i,
%             -2 + 4i.
% Note: the real term is always written first, even when negative.
% Note: This means that
% when a = 0 we have numbers of the form bi i.e. only imaginary numbers
% when b = 0 we have numbers of the form a i.e. real numbers.
% The set of all complex numbers is denoted by \mathbb{C}.
%% Recap: Notation – A summary
%
% Mathematical notation we have used so far:
% The  set of all real numbers is denoted by \mathbb{R}
% The set of all complex numbers is denoted by \mathbb{C}
%
% Further useful notation:
% The real part of a complex number  z is denoted by {\rm Re}(z) or \Re(z)
% The imaginary part of a complex number  z is denoted by {\rm Im}(z) or \Im(z)
%% Examples
% Find the real and imaginary parts of:
% z =1+ 7i — real part \Re(z)= {\bf 1}, imaginary part \Im(z) = 7
% z =2 - 4i — real part \Re(z)= {\bf 2}, imaginary part \Im(z) = \mathbf{-4}
% z =-3 — real part \Re(z)= {\bf -3}, imaginary part \Im(z) = 0
% z =i\sqrt{3} — real part \Re(z)= {\bf 0},  imaginary part \Im(z)= {\bf \sqrt{3}}
%%
%
%% Complex Number Arithmetic
%
% Complex Number Addition and Subtraction
% Complex Numbers can be added (or subtracted) by adding (or subtracting) their real and imaginary parts separately.
% Examples:
% (2 + 3i) + (4 - i) = 6 + 2i
% (4-2i) -(3 + 5i) =1- 7i
%
% Complex Number Multiplication:
% Follows the basic laws of polynomial multiplication and imaginary number multiplication (recall i^2 = -1)
% Then gather real and imaginary terms to simplify the expression.
% Examples:
% 2(5- 3i) = 10 - 6i
% (2 + 3i)(4 - i) = 8 - 2i + 12i - 3i^2 = 8 + 10i - 3(-1) = 8+10i + 3 =11+10i
% (-3- 5i)(2 + 3i) = -6 - 9i -10i -15i^2 = -6 -19i +15 = 9 -19i
% (2 + 3i)(2 -3i) = 4 - 6i + 6i - 9i^2 = 4 + 9 =13
% Note that in the last example the product of the two complex numbers is a real number.
%
%
%% The Complex Conjugate
%
% In general (a + bi)(a - bi) = a^2 + b^2
% A pair of complex numbers of this form are said to be conjugate.
% Examples:
% 4 + 5i and 4 - 5i are conjugate complex numbers.
% 7 - 3i is the conjugate of 7 + 3i
% If z is a complex number ( z \in \Im) the notation for its conjugate is \overline{z} or z^*.
% Example:
% z= 7 - 3i then \overline{z} = 7 + 3i
%
%% Complex Number Division
% A frequent problem in complex number arithmentic is how to evaluate/simplify:
%    z = \frac{a+bi}{\bf {c+di}},    ~~~~~a,b,c,d \in \Re
% It turns out that we can  always express z in the normal complex number form: {\bf z = e + fi},~~~~ e,f \in \Re?
% Direct division by a complex number (that is not either purely real or  imaginary) cannot be carried out: We have to follow the basic laws of algebraic division
% The real and imaginary part of the complex number {\bf c+di}
% The denominator is made up of two independent terms c and d.
% The complex conjugate comes to the rescue.
%
% Problem: Express z (below) in the form z = e + fi, a,b \in \Re:
% z = \frac{a+bi}{\bf {c+di}},\:\: a,b,c,d \in \Re
% We need to deal with the denominator, z_d. Here  z_d = c+di.
% We can readily obtain the complex conjugate of z_d, \overline{z_d} = c -di
% We have already observed that any complex number \times its conjugate is a real number, z_d\times \overline{z_d} \in \Re: c^2 + d^2
% So to remove i from the denominator we can multiply both numerator and denominator by \overline{z_d}
% This process is known as realising the denominator.
% Example:
% Express z (below) in the form z = a + bi, a,b \in \Re:
% z= \frac{2 + 9i}{5- 2i}
% We need to deal with the denominator, z_d. Here  z_d = 5-2i.
% Obtain complex conjugate of z_d, \overline{z_d} = 5 + 2i.
% Multiply both numerator and denominator by \overline{z_d}:
%  \frac{2 + 9i}{5- 2i} \times \frac{5 + 2i}{5+ 2i}\\ = \frac{10+4i+45i+18i^2}{25-4i^2} \\= \frac{-8 +49i}{29} \\= {\bf \frac{-8}{29} + \frac{49}{29}i}
%
%% Comparing Two Complex Numbers
%% Equality
% Two complex numbers, z_1 = a + bi and z_2 = c + di , are equal if and only if
% the real parts of each are equal
% AND
% the imaginary parts are equal.
% That is to say:
% \Re(z_1) = \Re(z_2) or  a = c,
% AND
% \Im(z_1) = \Im(z_2) or b = d
%
% Example:
% If x + iy = (3- 2i)(5 + i) what are the values of xand y?
% x + iy = (3- 2i)(5 + i)\\ = 15 + 3i -10i + 2i^2\\ = 13 - 7i
% So x = 13 and y = -7.
%
%% Corollary: The complex number zero
% A complex number is zero if and only if the real part and the imaginary part are both zero i.e.
% {a + bi = 0} \leftrightarrow {a = 0} and { b = 0}
%
%
%% 3. Visualising Complex Numbers: The Complex Plane
% So as we have seen, a complex number, z = a +ib, is made up of two parts,
% The real part, a and,
% The imaginary part, b
% So a complex number is essentially a 2D entity.
% Therefore, one way we may visualise this is by plotting these on a 2D graph: an argand diagram:
% The x-axis represents the real numbers, and
% The y-axis represents the imaginary numbers.
%% Argand Diagrams
% The complex number z = a + ib may then be represented in the complex plane by
% the point \mathbf{P} whose co-ordinates are (a, b)
%
% or,
% the vector \mathbf{OP}, where \mathbf{O} is the point at the origin, (0,0)
%
% This representation is known as the Argand diagram.
%% Exercise
% Given z_1 = 3 -2i and z_2 = 5 + 2i draw an Argand diagram for:
% z_1
% z_2
% z_1 + z_2
% z_1 - z_2
% Adding Two Complex Numbers: Vector Addition: Generally, given z_1 = a_1 + b_1 i and z_2 = a_2 + b_2 i then: z_1 + z_2 = (a_1 + a_2) + (b_1 + b_2)i
%
%
% If we plot two complex numbers on an Argand diagram then we see
% that they form two  adjacent sides of a parallelogram
% their sum forms the diagonal.
% So Complex Numbers folllow the Basic Laws of Vector Algebra
%%
%% Polar form of complex numbers
% Polar Coordinates: An alternative system of coordinates in which the position of any point \mathbf{P} can be described in terms of
% The distance, r, of \mathbf{P} from the origin, \mathbf{O}, and
% The angle/direction, \phi, that the line \mathbf{OP} makes with the positive real \Re-axis (or, more generally x-axis)
%
%
% This is the polar form of complex numbers
% In relation to complex numbers, we call the polar coordinate terms:
% The modulus, r,r = |z| = \sqrt{a^2 + b^2}
%             (Note this is a simple application of Pythagoras' theorem.)
% The argument or phase, \phi,(Simply):
%                 \phi = \arg z = argument z = \arctan(\frac{b}{a}) = \tan^{-1} (\frac{b}{a})
%             (Note: This is a simple application of basic trigonometry)
% to make up what is known as the polar coordinates of a point.
%% Measuring the Argument or Phase of Complex Number
% We can measure the argument is two ways: Both depend on which quadrant of complex plane the point resides in:
% \phi \in [0,2\pi) — All angles, \phi, were measured anticlockwise from the +ive real axis: therefore \phi must be in the range 0 to 2\pi
%
% Quadrant 1                                                         Quadrant 2
%
% Quadrant 3                                                Quadrant 4
% All angles given in radians here
% Alternatively:
% \phi \in (-\pi,\pi] — (not illustrated) measure smallest spanned angle from +ive real axis: \phi measured in range -\pito \pi.
% \[\phi = \arg z = \left\{
% \begin{array}{ll}
% \arctan(\frac{b}{a}) & \mbox{if $a>0$}\\
% \arctan(\frac{b}{a})+\pi & \mbox{if $a<0$} \mbox{ and $b \geq 0$}\\
% \arctan(\frac{b}{a})-\pi & \mbox{if $a<0$} \mbox{ and $b < 0$}\\
% \frac{\pi}{2}  & \mbox{if $a=0$} \mbox{ and $b > 0$}\\
% -\frac{\pi}{2} & \mbox{if $a=0$} \mbox{ and $b < 0$}\\
% \mbox{indeterminate} & \mbox{if $a=0$} \mbox{ and $b = 0$}
% \end{array}
% \right.\]
% The polar angle for the complex number 0 is undefined, but the usual arbitrary choice is the angle 0.
%% Example
% Find the modulus and argument of each of the following:
% 1+ i
%                Modulus r = |1+i| = \sqrt{1^2 +1^2} = \sqrt{2}
%                 Sketching the Argand diagram indicates that we are in the first quadrant, therefore positive angle, \phi between 0 and 90.
% \frac{1}{\sqrt{2}} - i \frac{1}{\sqrt{2}}
%                 Modulus r = |\frac{1}{\sqrt{2}} - i \frac{1}{\sqrt{2}}| = \sqrt{(\frac{1}{\sqrt{2}})^2 + (- \frac{1}{\sqrt{2}})^2} = \sqrt{\frac{1}{2} + \frac{1}{2}} = 1
%                 Sketching the Argand diagram indicates that we are in the fourth quadrant, therefore angle is negative between 0 and 90 (or between 270 and 360) degrees.(Radians sim.)
%%
%% Exercise
% Find the modulus and argument of each of the following:
% -1.35 + 2.56i
% \frac{1}{4} + \frac{\sqrt{3}}{4}i
%%
%% Converting between the Cartesian and Polar Form
% The form of a complex number in the polar co-ordinates system are the pairs [r,\phi] or [modulus,argument] and cartesian is the pair (a,b).
%
%% Cartesian to Polar Conversion
% We have already seen how to convert from Cartesian (a,b)to Polar [r,\phi] via:
% r = |z| = \sqrt{a^2 + b^2}
% \phi = \arg z = \arctan(\frac{b}{a})
%
%% Polar to Cartesian Conversion
%
% Can we convert from  Polar [r,\phi] to Cartesian (a,b)?
% Simple trigonometry gives us the solution:
%
% a = r \cos \phi
% b = r \sin \phi
% Giving  z = { a} + { b}{ i}
%% Exercise
% Find the Cartesian Co-ordinates of the Complex Point P[4, 30^\circ].
%
%% Trigonometric form
% From above:
% a = r \cos \phi
% b = r \sin \phi
% Giving
% So if we substitute for a and b we get:
% z =  r \cos \phi + r \sin \phi \times i\\  ~=  r(\cos \phi + i \sin \phi)
%
% This is known as the trigonometric form of a complex number
%
%% 4 MATLAB and Complex Numbers
%
% MATLAB already natively knows about complex numbers, for example:
%
% Via sqrt():

sqrt(-1)

%
%
% Solving equations, symbolically:


% Symbolic Eqns Soln
 syms x;
 f = x^2 + 1;
 solve(f)

 

%
% Finding polynomial roots:

% Polynomial Roots
p = [1 0 1];
roots(p)

%  So in any programming context (not necessarily involving the direct use or the need of Complex numbers) care needs to be taken to check is real and/or complex numbers (especially as solutions to equation solvers or roots of a polynomial) have been returned.
%% Checking whether is a variable is complex or real
% As we have seen in the polynomial root polynomial plotting examples: poly2_2.m above we can check if the MATLAB variable is real ot complex:
% isreal()

% see 

help isreal


%or doc ireal

%
%
%% Declaring Complex Numbers in MATLAB
%
% You can  type i (or j)  in an expression or the complex() function to declare a complex number.
% Use i or j with care!
% The formal way to use i or j to declare a complex number is NOT to use multiplication sign when declaring the complex number as in:

z = 1 +2i

% or

z = 1 + 3j

%  Run the declarations above an note the output. Note MATLAB converts j to i automatically.
% MATLAB is a mathemetician rather than and engineer!
%
% So what is the issue:
%     Well i (or j) could need to be declared as simple 1i (or 1j), so you may think you can type simply i (or j) in fact you can.
%     But this is bad practice!  Why? Consider the following code:

% It is possible (although bad programming practice) that some loop 
% (or general) variables i and j have been declared in teh code somewhere

for i = 1:3       % Quite often we may use i,j as loop counters
    for j= 1:3
        val = i*j;  % some computataion --- not important for this lessos
    end
end

    ...   % some other code
    ...
    ...
    ...
    
%Decalare a complex number or three

z1 = 1 + i
z2 = 3 + j
z3 = 1 + 3*i

% Run the code above and note the output – we have NO COMPLEX NUMBERS!
% the declaration of the variables i and j have taken precedence over the complex declarations.
% Note that use of multiplication sign at the imaginary part declaration (3*i) is allowed but is bad practice unless this is truly complex multiplication, which we show below, this causes problems.
%
% The solution is
% better declaration of complex variables, and
% use better counting variable names, do not use i and j

% It is possible (although bad programming practice) that some loop 
% (or general) variables i and j have been declared in teh code somewhere

for i = 1:3       % Quite often we may use i,j as loop counters
    for j= 1:3
        val = i*j;  % some computataion --- not important for this lessos
    end
end

    ...   % some other code
    ...
    ...
    ...
    
%Decalare a complex number or three

z1 = 1 + 1i
z2 = 3 + 1j
z3 = 1 + 3i

% Run the abvove and it's all fixed. Note:
% We declare i and j as 1i and 1j – No mutiplication sign to avoid same porblems as:
% Declare 3*i as 3i.
%
% Corollary:
% When using functions you have to use mutliplication:

z = cos(0.7)+sin(0.7)*1i

% % z = cos(0.7)+sin(0.7)i     – Causes a syntax error!
%
%
%% Using the complex() function:
% A more programmatic declaration of a complex number can be achieved via the complex() function:

z = complex (1 ,2)

% There is never any variable confusion this way but it may not read as well mathematically speaking.
%
% For more details see:

help complex

% op doc complex

%
%% MATLAB: real, imaginary, magnitude and phase
%
% MATLAB provides functions to obtain real, imaginary, magnitude and phase parts of a complex number:
%
% Real part,  Imaginary part,  and  Absolute value:

c = 4+3i;            

             
%  Real part,  Imaginary part,  and  Absolute value
[real(c),  imag(c), abs(c)]

%
%
%  Magnitude and phase:

% A complex number of magnitude 11 and phase angle 0.7 radians 
z = 11*(cos(0.7)+sin(0.7)*1i);

% Recover the magnitude and phase of ”z” 
[abs(z), angle(z)]

%
% Note: from last example, you can also declare a complex number  in trigonmetric form but  MATLAB coverts to normal representation:

z = 11*(cos(0.7)+sin(0.7)*1i)

% So Need to use abs() and angle() to

% Recover the magnitude and phase of ”z” 
% [abs(z), angle(z)]

%
%% MATLAB Complex Arithmetic
% This behaves as one would expect
%

c1 = 3 + 4i;
c2 = 2 + 4i;

% Let's add them
c1 + c2

% Now Subtract  
c1 - c2


%Power
1i^2

  
% Mutliply
c1*c2

%Divide
c1/c2

%
%% Plotting Complex Numbers in MATLAB
% MATLAB provide useful plotting functions for general Polar Coordinates
% This is not exclusively for Complex Numbers.
% The MATLAB function polarplot() achieves this:
% polarplot() — Polar coordinate plot
% polarplot(Theta , Radius ) makes a plot using polar coordinates of the angle Theta , in radians, versus the radius Radius.
%         polarplot(Theta, Radius, S) uses the linestyle specified in string S.
%                  similar to plot() in terms of styles
% Note the order!  Theta first then Radius!

figure;

z = 11*(cos(0.7)+sin(0.7)*1i)

polarplot([0 angle(z)],[0 abs(z)],'-r');

%
% For more information see:

help polarplot

%or doc polarplot

%
%% The compass() function
% The compass() function knows how to plot a complex number more directly:

figure;

c1 = 3 + 4i;
compass(c1);

% Note: c1 automatically converted to polar form within the function and a slightly nicer arrowhead plot.