# MATLAB Programming/Complex Numbers

## 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:

```>> compnum = 1 + i
compnum = 1.000 + 1.000i
>> compnum = 1 + j
compnum = 1.000 + 1.000i
```

Note that if you use j MATLAB still displays i on the screen.

Since i is used as the complex number indicator it is not recommended to use it as a variable, since it will assume it's a variable if given a choice.

```>> i = 3; %bad idea
>> a = 1 + i
a = 4
```

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

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

## MATLAB functions to manipulate complex values

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 = [1 + i, 3];
>> isreal(A)
ans = 0
>> isreal(A(2))
ans = 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.

```>> real(A)
ans = 1    3
>> imag(A)
ans = 1    0
```

One thing you may need to do is perform an operation on the real values of an array but not the complex values. MATLAB does not have a function to directly do this, but the following pair of commands lets you put only the real values into another array:

```>> RealIndex = (imag(A) == 0); %if imaginary part is zero then the number is real)
>> RealOnly = A(RealIndex)
RealOnly = 3
```