# MATLAB Programming/Complex Numbers

[MATLAB Programming\|/MATLAB Programming]m]
Chapter 1: MATLAB ._.
    Fundamentals of MATLAB
    MATLAB Workspace
    MATLAB Variables
    *.mat files
Chapter 2: MATLAB Concepts
    MATLAB operator
    Data File I/O
Chapter 3: Variable Manipulation
    Numbers and Booleans
    Strings
    Portable Functions
    Complex Numbers
Chapter 4: Vector and matrices
    Vector and Matrices
    Special Matrices
    Operation on Vectors
    Operation on Matrices
    Sparse Matrices
Chapter 5: Array
    Arrays
    Introduction to array operations
    Vectors and Basic Vector Operations
    Mathematics with Vectors and Matrices
    Struct Arrays
    Cell Arrays
Chapter 6: Graphical Plotting
    Basic Graphics Commands
    Plot
    Polar Plot
    Semilogx or Semilogy
    Loglog
    Bode Plot
    Nichols Plot
    Nyquist Plot
Chapter 7: M File Programming
    Scripts
    Comments
    The Input Function
    Control Flow
    Loops and Branches
    Error Messages
    Debugging M Files
Chapter 8: Advanced Topics
    Numerical Manipulation
    Advanced File I/O
    Object Oriented Programming
    Applications and Examples
    Toolboxes and Extensions
Chapter 9: Bonus chapters
    MATLAB Benefits and Caveats
    Alternatives to MATLAB
    [MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
    Octave/MATLAB differences

## 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.
As a consequence of the Euler formula, taking the logarithm of a negative number also results in imaginary answers.
In addition, the roots of functions found with the 'roots' function (for polynomials) or some other rootfinding function will often return complex answers.

## Manipulate complex numbers
### 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+bi$ , 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

## References

* ↑ https://web.archive.org/web/20211006102547/https://www.maths.unsw.edu.au/sites/default/files/MatlabSelfPaced/lesson1/MatlabLesson1_Complex.html