MATLAB Lesson 1 - Arithmetic

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

Enter the complex number 3 + 2i

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

>>  3 + 2*i
>>  3 + 2i
>>  3 + 2j

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.

Use the MATLAB function complex to create the complex number 3 + 2i

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

>>  complex(3, 2)

 

Use the MATLAB function real to get the real part of the complex number 3 + 2i

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

>>  real(3 + 2*i)

 

Use the MATLAB function imag to get the imaginary part of the complex number 3 + 2i

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

>>  imag(3 + 2*i)

 

Use the MATLAB function abs to get the modulus of the complex number 3 + 2i

The modulus of 3 + 2i is the square root of (32 + 22) = 13, so |3 + 2i| = 131/2.

>>  abs(3 + 2*i)

 

Use the MATLAB function angle to get the argument of the complex number -1-i

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

>>  angle(-1-i)

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.

 

In MATLAB 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.

>>  pi

 

In MATLAB the function exp(x) gives the value of the exponential function ex. Find the value of e.

e = e1 = exp(1). MATLAB does not use the symbol e for the mathematical constant e = 2.718281828459046.

>>  exp(1)

 

In MATLAB the function sqrt(x) gives the value of the square root of x. Find the square root of -9.

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.

>>  sqrt(-9)
>>  (-9)^(1/2)

 

MATLAB functions like exp(x) can be used either with real or complex arguments. Find the complex number 21/2 ei π/4.

Euler's formula ei t = cos(t) + i sin(t) for real t, gives the answer 1 + i

>>  sqrt(2)*exp(i*pi/4)

 

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 ei π = -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.