Kahibaro
Discord Login Register

Complex Numbers in MATLAB

Why Complex Numbers Matter in MATLAB

Complex numbers appear naturally in many areas such as signal processing, control systems, electromagnetics, and vibration analysis. MATLAB has built-in support for complex arithmetic, so you can work with real and imaginary parts in a very similar way to how you work with real numbers.

In this chapter you learn how MATLAB represents complex numbers, how to create them, inspect their parts, and perform basic complex operations.

Creating Complex Numbers

The basic idea of a complex number is that it has a real part and an imaginary part. In MATLAB, a complex number is usually written as
$$z = a + b\,i$$
where $a$ is the real part and $b$ is the imaginary part. MATLAB uses 1i or 1j to represent the imaginary unit $\sqrt{-1}$.

You can create complex numbers in several equivalent ways. The most direct way is to write them using i or j in an expression.

z1 = 3 + 4i;     % uses i
z2 = 3 + 4j;     % uses j, same value
z3 = -2.5 - 1.2i;

In each case MATLAB creates a complex scalar. Typically you use 1i or 1j when you want to make the imaginary nature very explicit.

z4 = 5 + 1i;     % clearer that 1 is the coefficient
z5 = 5 + 2*1i;   % 5 + 2i

You can also create complex numbers by combining real and imaginary parts with the complex function. This is especially useful when you already have the real and imaginary parts stored in variables or arrays.

a = 3;
b = 4;
z = complex(a, b);    % 3 + 4i

When you use complex, the first argument is the real part and the second argument is the imaginary part.

Complex Arrays and Vectorization

Complex numbers in MATLAB are not only scalars. You can create vectors and matrices of complex values in exactly the same way you work with real arrays.

For example, you can define a complex vector.

zv = [1+2i, 3-4i, -2+0.5i];

Or a complex matrix.

Z = [1+1i, 2-3i; ...
     0-4i, 5+0.5i];

Vectorized operations work naturally with complex arrays. If you apply arithmetic operators such as +, -, .*, or ./ to arrays that contain complex values, MATLAB performs the operation elementwise on the complex numbers.

A = [1+2i, 3+4i];
B = [2-1i, 1+0i];
C = A + B;      % elementwise addition
D = A .* B;     % elementwise multiplication

Many built-in functions also accept complex arrays, and they usually operate elementwise or in a mathematically meaningful way for complex inputs.

Real and Imaginary Parts

Once you have a complex number or array, you often need to access its real or imaginary part separately. MATLAB provides real and imag for this purpose.

Given $z = a + b\,i$:

For example:

z = 3 + 4i;
r = real(z);    % 3
im = imag(z);   % 4

When z is an array, real and imag return arrays of the same size containing only the respective parts.

Z = [1+2i, 3-4i; -1i, 5+0i];
R = real(Z);    % [1, 3; 0, 5]
I = imag(Z);    % [2, -4; -1, 0]

You can reconstruct a complex array from separate real and imaginary parts using complex.

R = [1, 2; 3, 4];
I = [0.5, -1; 2, 0];
Z = complex(R, I);    % Z has entries R + I*i

Magnitude and Phase

Instead of thinking of a complex number by its real and imaginary parts, you can also think of it as a point in the complex plane with a magnitude and an angle, also called phase or argument.

For $z = x + y\,i$, the magnitude is
$$|z| = \sqrt{x^2 + y^2}$$
and the phase is the angle from the positive real axis to the point, measured in radians.

MATLAB provides abs to compute the magnitude and angle to compute the phase of complex numbers.

z = 3 + 4i;
m = abs(z);        % 5, since sqrt(3^2 + 4^2) = 5
phi = angle(z);    % angle in radians

For an array of complex numbers, abs and angle return arrays of the same size.

Z = [1+1i, 1-1i];
M = abs(Z);        % [sqrt(2), sqrt(2)]
P = angle(Z);      % [pi/4, -pi/4]

If you want the phase in degrees instead of radians, you can convert with rad2deg.

phi_deg = rad2deg(angle(z));

Complex Conjugates

The complex conjugate of $z = x + y\,i$ is
$$\overline{z} = x - y\,i.$$

In MATLAB, the conj function returns the complex conjugate.

z = 3 + 4i;
zc = conj(z);    % 3 - 4i

For arrays, conj applies elementwise.

Z = [1+2i, 3-4i];
Zc = conj(Z);    % [1-2i, 3+4i]

Conjugation is frequently used in operations such as computing the magnitude squared of a complex value, or in certain linear algebra and signal processing formulas.

For example, the magnitude squared is
$$|z|^2 = z \cdot \overline{z}.$$

In MATLAB:

z = 3 + 4i;
mag2 = z .* conj(z);    % 25, equal to abs(z).^2

Complex Arithmetic Basics

Most arithmetic operations on complex numbers use the usual mathematical rules. MATLAB applies them directly to complex values.

You can add or subtract complex numbers.

z1 = 2 + 3i;
z2 = 4 - 1i;
s = z1 + z2;     % 6 + 2i
d = z1 - z2;     % -2 + 4i

Multiplication distributes over addition and respects the identity $i^2 = -1$.

p = z1 * z2;     % uses complex multiplication

Division by a complex number is also supported.

q = z1 / z2;     % complex division

The power operator ^ works with complex numbers as well. For integer exponents, it uses repeated multiplication. For noninteger exponents or other special cases, MATLAB uses complex logarithms and exponentials.

z = 1 + 1i;
z2 = z^2;           % (1 + i)^2 = 2i
z05 = z^0.5;        % complex square root

You can also use elementwise operations with .*, ./, and .^ on complex arrays, just as you do with real arrays.

A = [1+2i, 3+4i];
B = [2-1i, 1+1i];
C = A .* B;         % elementwise product
R = A ./ B;         % elementwise division
P = A.^2;           % elementwise square

Addition, subtraction, and multiplication of real and complex numbers together is allowed and produces complex results when needed.

r = 5;            % real
z = 2 + 3i;       % complex
s = r + z;        % 7 + 3i

Interactions with Mathematical Functions

Many mathematical functions in MATLAB, such as sin, cos, exp, and log, accept complex inputs and return complex outputs. These functions are defined using the complex extensions of the corresponding real functions.

For example:

z = 1 + 2i;
ez = exp(z);      % complex exponential
sz = sin(z);      % complex sine
cz = cos(z);      % complex cosine
lz = log(z);      % complex natural logarithm

Because complex functions are often multi-valued in mathematics, MATLAB chooses standard principal values so that every complex input gives a single complex output. This is important when working with roots and logarithms of complex numbers.

When you use trigonometric or hyperbolic functions with complex inputs, remember that the results can be complex even if the input is purely real, depending on the function and the value.

Representing Complex Signals and Data

In many applications, a single complex array can represent a signal with two components, one real and one imaginary. For example, in digital signal processing, complex arrays often store frequency domain data or quadrature signals.

You might create a complex sinusoid by combining real and imaginary parts using exp with imaginary arguments:
$$z(t) = e^{i \omega t}.$$

In MATLAB:

t = 0:0.01:1;
omega = 2*pi*5;          % 5 Hz
z = exp(1i * omega * t); % complex sinusoid

Here z is a complex vector whose samples lie on the unit circle in the complex plane. The real and imaginary parts correspond to cosine and sine components.

You can plot the real and imaginary parts separately using usual plotting functions, or view the magnitude and phase instead.

plot(t, real(z));        % real part

Later, when you work with more advanced plotting and signal processing, complex representations like this will become a standard tool.

Display and Formatting of Complex Numbers

When you display a complex variable in the Command Window, MATLAB shows its real and imaginary parts in a readable form.

z = 3 + 4i;
z

The output shows 3.0000 + 4.0000i or similar, depending on your display format. You can change how many digits are shown using format, which affects complex values just as it affects real numbers.

If the imaginary part is zero, MATLAB may display the number as a real value, but it will still be complex internally if it was created that way. You can verify if a value is purely real or has a nonzero imaginary part using isreal.

z1 = 3 + 0i;
z2 = 3;
isreal(z1)    % returns false
isreal(z2)    % returns true

Avoiding Common Pitfalls with i and j

MATLAB treats i and j like ordinary variables. By default they represent $\sqrt{-1}$, but you can overwrite them accidentally.

For example:

i = 10;         % overwrites the imaginary unit
z = 3 + 4i;     % now interpreted as 3 + 4*10

To avoid this problem, either avoid using i and j as variable names, or use 1i and 1j explicitly to make sure you get the imaginary unit.

clear i j      % restore i and j if needed
z = 3 + 4*1i;  % safe, always complex

The complex function also avoids confusion, since it does not rely on i or j.

z = complex(3, 4);   % 3 + 4i, independent of i and j

Important points to remember:
Use i or j for the imaginary unit, but avoid reusing them as variable names, or use 1i, 1j, or complex to be safe.
Use real, imag, abs, angle, and conj to work with the components, magnitude, phase, and conjugate of complex numbers.
Complex arithmetic and most mathematical functions work directly on complex arrays, and many results will be complex when inputs are complex.
Use isreal if you need to check whether a value has any imaginary part.

Views: 3

Comments

Please login to add a comment.

Don't have an account? Register now!