## Complex numbers

MATLAB, like Maple and other mathematical software but in contrast to spreadsheets like Excel, automatically allows and works with complex numbers. All arithmetic with complex numbers works in the usual way.

In MATLAB, both **i** and **j** denote the square root of -1.
This is because MATLAB is used widely in both mathematics (where **i** is
most commonly used for the square root of -1) and (electrical) Engineering (where **j**
is more commonly used for the square root of -1).

**3 + 2i**

Any of these three expression works, but 3 + 2 i with a space between the 2 and i does not.

## Special functions for complex numbers

There are a number of special functions for working with complex numbers. These include creating complex numbers from real and imaginary parts, finding the real or imaginary part, calculating the modulus and the argument of a complex number.

**3 + 2i**

The first argument 3 is the real part, the second argument 2 is the imaginary part.

**3 + 2i**

The real part of the complex number 3 + 2i is 3.

**3 + 2i**

The imaginary part of the complex number 3 + 2i is 2.

**3 + 2i**

The modulus of 3 + 2i is the square root of (3^{2} + 2^{2}) = 13,
so |3 + 2i| = 13^{1/2}.

**-1-i**

The argument of -1-i is -3 π / 4 radians, as the argument is in (-π, π]

## Looking forward

Although we will not see MATLAB constants until Lesson 2, and MATLAB functions in detail until lesson 4, some examples are so commonly used in connection with complex numbers that they are worth mentioning now. These are the constant pi and the square root and exponential functions.

**pi**gives the value of the mathematical constant π = 3.1415926535897....

MATLAB's value of π (lower case pi) is correct to around 15 decimal digits. Use the **format** command to display all digits.

**exp(x)**gives the value of the exponential function e

^{x}. Find the value of e.

e = e^{1} = exp(1). MATLAB does **not** use the symbol **e** for
the mathematical constant e = 2.718281828459046.

MATLAB works with complex numbers, so the square root of -9 is 3 i = 0 + 3 i

The sqrt function is clearer and more efficient than using the power 1/2.

^{1/2}e

^{i π/4}.

Euler's formula e^{i t} = cos(t) + i sin(t) for real t, gives the answer 1 + i

## Warning

As MATLAB works with complex numbers, expressions like sqrt(-3) or (-3)^(1/2)
that produce complex numbers will **not** produce an error as in Excel.

## Self-test Exercise

Use MATLAB to verify the well known identity e^{i π} = -1,
where π represents the mathematical constant pi.

Answer: exp(i*pi)

Use the mouse to select the text between the word "Answer" and here to see the answer.

## Summary

MATLAB understands complex numbers. There are special functions for working with complex numbers and MATLAB's functions can take real or complex arguments.