0
0
MATLABdata~5 mins

Complex numbers in MATLAB

Choose your learning style9 modes available
Introduction

Complex numbers let us work with numbers that have a real part and an imaginary part. They help solve problems that normal numbers can't handle.

When you need to represent signals in engineering that have both magnitude and phase.
When solving equations that have no real solutions, like square roots of negative numbers.
When working with electrical circuits involving alternating current (AC).
When analyzing waves or oscillations in physics.
When performing Fourier transforms in data processing.
Syntax
MATLAB
z = a + b*i;
% or
z = complex(a, b);

Here, a is the real part and b is the imaginary part.

In MATLAB, i or j represents the imaginary unit (√-1).

Examples
This creates a complex number with real part 3 and imaginary part 4.
MATLAB
z1 = 3 + 4i;
This creates a complex number with real part 5 and imaginary part -2 using the complex function.
MATLAB
z2 = complex(5, -2);
You can also use j instead of i for the imaginary unit.
MATLAB
z3 = 7 - 3j;
Sample Program

This program creates two complex numbers, adds them, multiplies them, and prints the results showing real and imaginary parts.

MATLAB
a = 2 + 3i;
b = complex(1, -1);
sum = a + b;
product = a * b;

fprintf('Sum: %.2f + %.2fi\n', real(sum), imag(sum));
fprintf('Product: %.2f + %.2fi\n', real(product), imag(product));
OutputSuccess
Important Notes

Use real(z) and imag(z) to get the real and imaginary parts of a complex number.

Be careful not to overwrite i or j as variables, or MATLAB will not recognize them as imaginary units.

Complex arithmetic follows special rules different from real numbers, but MATLAB handles them automatically.

Summary

Complex numbers have a real and an imaginary part.

Use i or j to write imaginary parts in MATLAB.

MATLAB provides functions to create and work with complex numbers easily.