MATLAB Lesson 10 - Plotting complex numbers

In Matlab complex numbers can be created using x = 3 - 2i or x = complex(3, -2). The real part of a complex number is obtained by real(x) and the imaginary part by imag(x).

The complex plane has a real axis (in place of the x-axis) and an imaginary axis (in place of the y-axis). It is useful to plot complex numbers as points in the complex plane and also to plot function of complex variables using either contour or surface plots.

Plotting complex numbers

If the input to the Matlab plot command is a vector of complex numbers, the real parts are used as the x-coordinates and the imaginary parts as the y-coordinates.

Define the complex number z = 3 + 2i and plot it as a point on the complex plane.

The first argument is the real part, the second the imaginary part

Plotting the real and imaginary parts separately will work.

Matlab recognises the input is a complex number and does some of the work for you.

>>  z = complex(3, 2)

>>  plot(real(z), imag(z), '*')

>>  plot(z, 'o')


Some points to remember are
Define the complex numbers 3 + 2i, -2 + i, -2 - i, 1 - 2i and plot them all on the complex plane, as in the figure below.

Define the vector z of the complex numbers. Note that -2+1i is equivalent to complex(-2,1), even if the variable i is defined to have a value other than sqrt(-1).

Plot the complex numbers in z as points.

You still need to add a grid and adjust the axes.

>>  z = [3+2i, -2+1i, -2-1i, 1-2i]


>>  plot(z, '*');





Complex numbers

Eigenvalues of a real matrix

A real n by n matrix A has n eigenvalues (counting multiplicities) which are either real or occur in complex conjugate pairs.

For n = 100, generate an n by n real matrix with elements Aij which are samples from a standard normal distribution (Hint: MATLAB randn), calculate the eigenvalues using the MATLAB function eig and plot all n eigenvalues as points on an Argand diagram.

Define a variable for the size of the matrix, and hence the number of eigenvalues.

Define an n by n matrix with elements taken from standard normal distribution.

Calculate the vector of n eigenvalues.

Plot the eigenvalues as points on the complex plane. Which eigenvalues are real and which are in a complex conjugate pair?

>>  n = 100;
>>  A = randn(n);
>>  ev = eig(A);
>>  plot(ev, '*');
Eigenvalues of a real matrix

The MATLAB M-file used to create this plot is evplot.m.

By default MATLAB joints each point (complex number) in the plot by a line segment, which for a fine grid of points gives a smooth curve.

Plot the curve z = t ei t for t in [0, 4 pi].

Define a vector of 401 equally spaced points on the interval.

Calculate z using the MATLAB functions complex and exp.

Plot the elements of the vector z and join by line segments.

Ensure the real and imaginary axes have the same scaling.

Fix the axis limits.

>>  t = linspace(0, 4*pi, 401);
>>  z = t .* exp( complex(0, t) );
>>  plot(z);
>>  axis equal;
>>  axis([-13 13 -13 13]);
Complex spiral

 

Warnings

 

Self-test Exercise

Plot the complex curve z = e i t for t in [-4, 4]. What should the curve look like?

Answer:
t = linspace(-4,4,401);
z = exp(complex(0,t));
plot(z);
The plot looks like an ellipse, but should be a circle with centre the origin and radius 1, as |z| = 1 for all real t. To get a plot that looks like a circle, you need to make the axes equal.

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

Summary

MATLAB can plot points or curves on an Argand diagram, using real and imaginary axes. You do not need to explicitly calculate the real and imaginary parts.