KAHIBARO Bookshelves
Discord Login Register

7.4. 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$:

  • real(z) returns $a$
  • imag(z) returns $b$

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: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Getting Started with MATLAB
    • ☰ 1.1. What Is MATLAB and What Is It Used For
      • ☰ 1.2. Installing MATLAB and Setting Up the Environment
        • ☰ 1.3. Understanding the MATLAB Desktop Interface
          • ☰ 1.4. Command Window, Workspace, and Current Folder
            • ☰ 1.5. Basic Workflow and Running Simple Commands
              • ☰ 1.6. Getting Help and Documentation
              • ☰ 2. MATLAB Language Basics
                • ☰ 2.1. MATLAB Syntax and Expressions
                  • ☰ 2.2. Variables and Assignment
                    • ☰ 2.3. Data Types and Type Conversion
                      • ☰ 2.4. Vectors and Matrices Fundamentals
                        • ☰ 2.5. Creating and Indexing Arrays
                          • ☰ 2.6. Basic Arithmetic and Elementwise Operations
                            • ☰ 2.7. Using Built-In Constants and Functions
                            • ☰ 3. Working with Variables and the Workspace
                              • ☰ 3.1. Creating, Viewing, and Editing Variables
                                • ☰ 3.2. Workspace Browser and Variable Inspector
                                  • ☰ 3.3. Saving and Loading Data Files
                                    • ☰ 3.4. MAT-Files and Importing External Data
                                      • ☰ 3.5. Clearing Variables and Managing the Workspace
                                      • ☰ 4. Vectors, Matrices, and Arrays
                                        • ☰ 4.1. Row and Column Vectors
                                          • ☰ 4.2. Matrix Creation and Concatenation
                                            • ☰ 4.3. Indexing, Slicing, and Logical Indexing
                                              • ☰ 4.4. Matrix Operations vs Elementwise Operations
                                                • ☰ 4.5. Reshaping and Rearranging Arrays
                                                  • ☰ 4.6. Special Matrices and Functions
                                                  • ☰ 5. Basic Programming Constructs
                                                    • ☰ 5.1. Scripts vs Functions
                                                      • ☰ 5.2. Control Flow with if, elseif, else
                                                        • ☰ 5.3. Loops with for and while
                                                          • ☰ 5.4. break, continue, and return
                                                            • ☰ 5.5. Logical Expressions and Relational Operators
                                                              • ☰ 5.6. Best Practices for Clear and Readable Code
                                                              • ☰ 6. Functions in MATLAB
                                                                • ☰ 6.1. Creating Simple Functions
                                                                  • ☰ 6.2. Function Inputs and Outputs
                                                                    • ☰ 6.3. Function Files and the Path
                                                                      • ☰ 6.4. Local vs Global Variables
                                                                        • ☰ 6.5. Anonymous Functions
                                                                          • ☰ 6.6. Organizing Code into Multiple Functions
                                                                          • ☰ 7. Working with Numbers and Math
                                                                            • ☰ 7.1. Scalars and Vectorized Computations
                                                                              • ☰ 7.2. Basic Mathematical Functions
                                                                                • ☰ 7.3. Trigonometric and Exponential Functions
                                                                                  • ☰ 7.4. Complex Numbers in MATLAB
                                                                                    • ☰ 7.5. Rounding, Modulo, and Special Values
                                                                                      • ☰ 7.6. Numerical Precision and Floating Point Considerations
                                                                                      • ☰ 8. Strings, Characters, and Categorical Data
                                                                                        • ☰ 8.1. Character Arrays and String Arrays
                                                                                          • ☰ 8.2. Creating and Manipulating Strings
                                                                                            • ☰ 8.3. Converting Between Data Types and Strings
                                                                                              • ☰ 8.4. Comparing and Searching Strings
                                                                                                • ☰ 8.5. Categorical Arrays Basics
                                                                                                • ☰ 9. Tables, Structures, and Cell Arrays
                                                                                                  • ☰ 9.1. Introduction to Tables
                                                                                                    • ☰ 9.2. Creating Tables and Accessing Data
                                                                                                      • ☰ 9.3. Modifying and Joining Tables
                                                                                                        • ☰ 9.4. Structures and Nested Structures
                                                                                                          • ☰ 9.5. Cell Arrays and Cell Indexing
                                                                                                            • ☰ 9.6. Choosing the Right Container Type
                                                                                                            • ☰ 10. Importing and Exporting Data
                                                                                                              • ☰ 10.1. Reading Text and CSV Files
                                                                                                                • ☰ 10.2. Importing Excel Spreadsheets
                                                                                                                  • ☰ 10.3. Importing Data with the Import Tool
                                                                                                                    • ☰ 10.4. Exporting Data to Files
                                                                                                                      • ☰ 10.5. Working with Image and Audio Files
                                                                                                                        • ☰ 10.6. Basic File Management from MATLAB
                                                                                                                        • ☰ 11. Plotting and Visualization
                                                                                                                          • ☰ 11.1. Introduction to 2D Plotting
                                                                                                                            • ☰ 11.2. Customizing Plots and Axes
                                                                                                                              • ☰ 11.3. Multiple Plots in One Figure
                                                                                                                                • ☰ 11.4. Labels, Titles, Legends, and Annotations
                                                                                                                                  • ☰ 11.5. Plotting with Different Line and Marker Styles
                                                                                                                                    • ☰ 11.6. Saving and Exporting Figures
                                                                                                                                    • ☰ 12. Advanced Plotting for Beginners
                                                                                                                                      • ☰ 12.1. Subplots and Tiled Layouts
                                                                                                                                        • ☰ 12.2. Bar, Scatter, and Stem Plots
                                                                                                                                          • ☰ 12.3. Histograms and Density Plots
                                                                                                                                            • ☰ 12.4. 3D Plots and Surface Plots
                                                                                                                                              • ☰ 12.5. Using plottools and Interactive Editing
                                                                                                                                                • ☰ 12.6. Basics of Handle Graphics
                                                                                                                                                • ☰ 13. Working with Scripts and Live Scripts
                                                                                                                                                  • ☰ 13.1. Creating and Running Script Files
                                                                                                                                                    • ☰ 13.2. Sectioning Code and Running Sections
                                                                                                                                                      • ☰ 13.3. Live Scripts and Rich Text
                                                                                                                                                        • ☰ 13.4. Adding Equations, Images, and Outputs
                                                                                                                                                          • ☰ 13.5. Sharing and Exporting Live Scripts
                                                                                                                                                          • ☰ 14. Debugging and Code Quality
                                                                                                                                                            • ☰ 14.1. Finding and Fixing Syntax Errors
                                                                                                                                                              • ☰ 14.2. Setting Breakpoints and Stepping Through Code
                                                                                                                                                                • ☰ 14.3. Inspecting Variable Values During Execution
                                                                                                                                                                  • ☰ 14.4. Using the Editor Debugging Tools
                                                                                                                                                                    • ☰ 14.5. Using Code Analyzer and Warnings
                                                                                                                                                                      • ☰ 14.6. Writing Clean and Maintainable MATLAB Code
                                                                                                                                                                      • ☰ 15. Basic Linear Algebra with MATLAB
                                                                                                                                                                        • ☰ 15.1. Vectors and Norms
                                                                                                                                                                          • ☰ 15.2. Matrix Addition and Multiplication
                                                                                                                                                                            • ☰ 15.3. Solving Linear Systems with Backslash
                                                                                                                                                                              • ☰ 15.4. Inverses, Determinants, and Rank
                                                                                                                                                                                • ☰ 15.5. Eigenvalues and Eigenvectors
                                                                                                                                                                                  • ☰ 15.6. Using Linear Algebra Functions Safely
                                                                                                                                                                                  • ☰ 16. Data Analysis and Statistics Fundamentals
                                                                                                                                                                                    • ☰ 16.1. Descriptive Statistics and Summary Measures
                                                                                                                                                                                      • ☰ 16.2. Sorting, Filtering, and Grouping Data
                                                                                                                                                                                        • ☰ 16.3. Basic Correlation and Regression
                                                                                                                                                                                          • ☰ 16.4. Simple Data Cleaning Techniques
                                                                                                                                                                                            • ☰ 16.5. Basic Time Series Handling
                                                                                                                                                                                            • ☰ 17. Working with Time and Dates
                                                                                                                                                                                              • ☰ 17.1. Representing Dates and Times
                                                                                                                                                                                                • ☰ 17.2. Creating and Formatting datetime Arrays
                                                                                                                                                                                                  • ☰ 17.3. Date Calculations and Differences
                                                                                                                                                                                                    • ☰ 17.4. Plotting Data over Time
                                                                                                                                                                                                    • ☰ 18. Introduction to Signal and Image Handling
                                                                                                                                                                                                      • ☰ 18.1. Basic Signal Representation in MATLAB
                                                                                                                                                                                                        • ☰ 18.2. Simple Filtering and Transformations
                                                                                                                                                                                                          • ☰ 18.3. Reading, Displaying, and Writing Images
                                                                                                                                                                                                            • ☰ 18.4. Basic Image Transformations
                                                                                                                                                                                                            • ☰ 19. Automation and Scripting Tasks
                                                                                                                                                                                                              • ☰ 19.1. Batch Processing of Files
                                                                                                                                                                                                                • ☰ 19.2. Writing Reusable Utility Scripts
                                                                                                                                                                                                                  • ☰ 19.3. Using Relative Paths and the MATLAB Path
                                                                                                                                                                                                                    • ☰ 19.4. Simple User Interaction with input and disp
                                                                                                                                                                                                                    • ☰ 20. Introduction to Toolboxes
                                                                                                                                                                                                                      • ☰ 20.1. What Are Toolboxes and How to Use Them
                                                                                                                                                                                                                        • ☰ 20.2. Exploring Installed Toolboxes
                                                                                                                                                                                                                          • ☰ 20.3. Finding Functions in Toolboxes
                                                                                                                                                                                                                            • ☰ 20.4. Add-Ons and MATLAB File Exchange
                                                                                                                                                                                                                            • ☰ 21. Best Practices and Next Steps
                                                                                                                                                                                                                              • ☰ 21.1. Organizing Projects and Folders
                                                                                                                                                                                                                                • ☰ 21.2. Version Control Basics with MATLAB
                                                                                                                                                                                                                                  • ☰ 21.3. Performance Tips for Beginners
                                                                                                                                                                                                                                    • ☰ 21.4. Learning Resources and Documentation Path
                                                                                                                                                                                                                                      • ☰ 21.5. Suggested Practice Projects for Beginners