MATLAB Programming/Complex Numbers

Complex Numbers
Complex numbers are also used in MATLAB. It consists of two parts, one is real number and one is imaginary number. It is in the form of $$z = a + bi $$ or $$ z = a + bj $$. i or j returns the basic imaginary unit. i or j is equivalent to square root of -1 where the formula is ( $$ i/j = \sqrt{-1} $$ ). Note: The symbol i and j are interchangeable for one to another, as MATLAB just convert j into i, if equations is using two different notations as shown below.

Declaring a complex number in MATLAB
Complex numbers in MATLAB are doubles with a real part and an imaginary part. The imaginary part is declared by using the 'i' or 'j' character. For example, to declare a variable as '1 + i' just type as following:

Note 1: Even if you use j to indicate complex number, MATLAB will still displays i on the screen.

Note 2: Since i is used as the complex number indicator it is not recommended to use it as a variable, since it will assume i is a variable. However, since implicit multiplication is not normally allowed in MATLAB, it is still possible to declare a complex number like this:

It's best still not to declare i as a variable, but if you already have a complex program with i as a variable and need to use complex numbers this is probably the best way to get around it.

If you want to do arithmetic operations with complex numbers make sure you put the whole number in parenthesis, or else it likely will not give the intended results.

Complex functions
However, the best practice to declare a complex number is by using function complex.

If you want to declare just the imaginary number, just use the square root of negative numbers, such as followed.

To declare multiple complex numbers, create two row vectors with real numbers and another with imaginary numbers. Combine both of them using complex functions

Arithmetic operations that create complex numbers
There are several operations that create complex numbers in MATLAB. One of them is taking an even root of a negative number, by definition.

>> (-1)^0.5 ans = 0.000 + 1.000i >> (-3)^0.25 ans = 0.9306 + 0.9306i

As a consequence of the Euler formula, taking the logarithm of a negative number also results in imaginary answers.

>> log(-1) ans = 0 + 3.1416i

In addition, the roots of functions found with the 'roots' function (for polynomials) or some other rootfinding function will often return complex answers.

Finding real and imaginary number
First of all, it is helpful to tell whether a given matrix is real or complex when programming, since certain operations can only be done on real numbers.

Since complex numbers don't have their own class, MATLAB comes with another function called "isreal" to determine if a given matrix is real or not. It returns 0 if any of the inputs are complex.

Notice that it is possible to have real and complex numbers in the same array, since both are of class double. The function is set up this way so that you can use this as part of a conditional, so that a block only is executed if all elements of array A are real.

To extract just the real part of a complex variable use the real function. To extract just the complex part use the imag function.

Complex conjugate
To find complex conjugate, we can use conj function. If complex number, Z is $$a+b i$$, then the conjugate, Ẑ is $$a - bi$$

Phase Angle
To find phase angle, we can use the phase angle in the radian for each element of a complex numbers