# MATLAB Programming/Complex Numbers

## Complex NumbersEdit

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 or .

i or j returns the basic imaginary unit. __i or j is equivalent to square root of -1 __ where the formula is ( ).

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 MATLABEdit

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:

```
>> compnum = 1 + i
compnum = 1.000 + 1.000i
>>%Note:If you use j MATLAB still displays i on the screen
>> compnum = 1 + j
compnum = 1.000 + 1.000i
```

**Note 1:** Even if you use * j* to indicate complex number , MATLAB will still displays

*on the screen.*

**i****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

*is a variable.*

**i**```
>> i = 1; %bad practise to use i as variable
>> a = 1 + i
a = 2
```

However, since implicit multiplication is not normally allowed in MATLAB, it is still possible to declare a complex number like this:

```
>> i = 3;
>> a = 1i + 1
a = 1.000 + 1.000i
```

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** functionsEdit

However, __the best practice to declare a complex number is by using function complex__.

```
>>%Best practise to declare complex number in MATLAB
>> complex(2,6)
ans =
2.0000 + 6.0000i
```

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

```
>> sqrt(-49)
ans =
0.0000 + 7.0000i
```

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

```
>> %create a vector consiots of real number
>> RE = [1,2,3]
RE =
1 2 3
>> %create a vector consiots of imaginary number
>> IM = [4,5,6]
IM =
4 5 6
>> %create 3 complex number
>> complex(RE,IM)
ans =
1.0000 + 4.0000i 2.0000 + 5.0000i 3.0000 + 6.0000i
```

### Arithmetic operations that create complex numbersEdit

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.

## Manipulate complex numbersEdit

### Finding real and imaginary numberEdit

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.

```
>> A=[complex(2,3),complex(4,0)]
A =
2.0000 + 3.0000i 4.0000 + 0.0000i
>> %returns 1 if complex number does not have an imaginary part and 0 if otherwise.
>> %A denotes the whole vectors
>> isreal(A)
ans =
logical
0
>> %A(2) denotes second complex number in the vector (4+0i)
>> isreal(A(2))
ans =
logical
1
```

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.

```
>>%Extract real number from the complex number vector A
>> real(A)
ans =
2 4
>>%Extract imaginary number from the complex number vector A
>> imag(A)
ans =
3 0
```

### Complex conjugateEdit

To find complex conjugate , we can use **conj** function. If complex number, Z is , then the conjugate, Ẑ is

```
>> conj(A)
ans =
2.0000 - 3.0000i 4.0000 + 0.0000i
```

### Phase AngleEdit

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

```
>> angle(A)
ans =
0.9828 0
```

## ReferencesEdit

^{[1]}