KAHIBARO Bookshelves
Discord Login Register

15.5. Eigenvalues and Eigenvectors

Introduction

This chapter focuses on how to work with eigenvalues and eigenvectors in MATLAB. You will see how to compute them with built in functions, how to interpret the main outputs, and how to perform a few basic operations that often appear in linear algebra and applications. More advanced theory and applications are beyond the scope here. The emphasis is on practical MATLAB usage.

Basic MATLAB function `eig`

In MATLAB, the central tool for eigenvalue computations is the function eig. At its simplest, you call eig with a square matrix and it returns its eigenvalues.

Suppose A is a square matrix. The mathematical eigenvalue equation is
$$
A v = \lambda v
$$
where $\lambda$ is an eigenvalue and $v$ is a corresponding eigenvector.

In MATLAB, the simplest form is:

matlab
lambda = eig(A);

Here lambda is a column vector that contains the eigenvalues of A. If A is an $n \times n$ matrix, then lambda has n entries.

For example:

matlab
A = [2 1; 0 3];
lambda = eig(A)

returns a 2 by 1 vector with the two eigenvalues of A.

At this stage you only have the eigenvalues, not the eigenvectors. To obtain both, you need a different call format.

Getting eigenvectors and the diagonal form

To compute both eigenvalues and eigenvectors, use two output arguments:

matlab
[V, D] = eig(A);

MATLAB returns a matrix V whose columns are eigenvectors, and a matrix D that is diagonal, whose diagonal entries are the eigenvalues.

If A is $n \times n$, then:

  • V is $n \times n. Column V(:,k)` is an eigenvector.
  • D is $n \times n and diagonal. Entry D(k,k) is the eigenvalue associated with V(:,k)`.

Mathematically, this means
$$
A V = V D.
$$
You can verify this in MATLAB:

matlab
A = [2 1; 0 3];
[V, D] = eig(A);
AV = A*V;
VD = V*D;
difference = AV - VD

The matrix difference should be very close to the zero matrix. Any small nonzero entries are due to floating point rounding.

The eigenvectors in V are not unique. MATLAB may choose a different scaling or sign compared to hand calculations, but they still satisfy the eigenvalue equation.

Interpreting the outputs of `eig`

When you inspect D, you usually only care about its diagonal elements. You can obtain them as a vector with:

matlab
lambda = diag(D);

This gives the same eigenvalues as:

matlab
lambda = eig(A);

although the ordering may differ compared to other software or textbook examples. MATLAB does not guarantee a particular ordering of eigenvalues beyond the internal algorithm it uses.

If an eigenvalue appears multiple times on the diagonal of D, then that is a repeated eigenvalue. The corresponding columns of V are a basis for the associated eigenspace, if the matrix is diagonalizable. In some special cases, such as defective matrices, the geometric multiplicity can be smaller than the algebraic multiplicity, but for basic usage you mostly treat the columns of V as the eigenvectors reported by MATLAB.

You should also be aware that eigenvalues of real matrices can be complex. If A is real but not symmetric (or Hermitian in the complex case), MATLAB may return complex eigenvalues and complex eigenvectors.

Complex eigenvalues and eigenvectors

MATLAB works naturally with complex numbers, so eig can handle matrices that have complex entries, as well as real matrices whose eigenvalues are complex.

Consider:

matlab
A = [0 -1; 1 0];
[V, D] = eig(A);

The eigenvalues in D will be complex. Typical results are eigenvalues $i$ and $-i$, where $i in MATLAB is the imaginary unit. The corresponding columns of V` are complex eigenvectors.

You should treat complex eigenvalues and vectors just like real ones when using eig in MATLAB. They can be inspected, used in calculations, and plotted. The real part of a complex number z is given by real(z) and the imaginary part by imag(z). Its magnitude is given by abs(z) and its angle (argument) by angle(z).

If you expect real eigenvalues and get complex results, it usually indicates that the matrix is not symmetric or has properties that lead to complex eigenvalues.

Eigenvalues of symmetric matrices

When A is real and symmetric, or complex Hermitian, its eigenvalues are real and its eigenvectors can be chosen to be orthonormal. MATLAB takes advantage of this and in many cases returns numerically more stable results.

For a real symmetric matrix:

matlab
A = [2 1; 1 2];
[V, D] = eig(A);

The matrix D contains only real diagonal entries, and the columns of V are orthonormal. This means:
$$
V^T V = I
$$
in the real case. You can check this with:

matlab
check = V.' * V    % transpose for real matrices

check should be very close to the identity matrix.

In practice, if you know your matrix is symmetric, you can often trust the eigenvalues more and use the eigenvectors in orthogonal decompositions or coordinate transforms.

Diagonalization and reconstructing the matrix

If a matrix A is diagonalizable, eig gives you matrices V and D such that:
$$
A = V D V^{-1}.
$$
In MATLAB, you can reconstruct an approximation of A using:

matlab
[V, D] = eig(A);
A_reconstructed = V*D/V;

The result may not be exactly equal to A because of numerical rounding, but it should be very close. You can measure the difference:

matlab
error_matrix = A - A_reconstructed;
error_norm = norm(error_matrix)

The value error_norm should be small compared to the size of A.

This diagonalization is useful in many applications, such as solving certain differential equations, computing matrix functions, or understanding the behavior of repeated multiplication by A. In this beginner chapter, you do not need to master all such applications, but you should recognize that the pair (V, D) gives a representation of A in a basis of eigenvectors.

Checking the eigenvalue equation manually

You can verify the eigenvalue relation $A v = \lambda v$ in MATLAB for a specific eigenpair.

After computing:

matlab
[V, D] = eig(A);

you can select the k-th eigenvalue and its eigenvector:

matlab
k = 1;
lambda_k = D(k,k);
v_k = V(:,k);
left_side = A*v_k;
right_side = lambda_k*v_k;
difference = left_side - right_side

The entries in difference should be close to zero. This confirms that v_k is indeed an eigenvector associated with lambda_k.

Such a check can be useful for learning, or when you are experimenting with small matrices and want to gain intuition about the relationship between A, V, and D.

Eigenvalues of larger matrices and performance notes

For beginners it is common to work with relatively small matrices. However, eig also works on larger ones. The computation cost grows with the size of the matrix, so for very large matrices you need to be more careful, and there are more specialized methods that only find a few eigenvalues. These advanced topics are not treated here.

On moderate sized matrices, you can safely call:

matlab
[V, D] = eig(A);

and directly work with all eigenvalues and eigenvectors. If you only need the eigenvalues, prefer:

matlab
lambda = eig(A);

because it uses less memory and is slightly faster.

Avoid forming extremely large dense matrices without reason, because both memory use and computation time can grow quickly.

Real vs complex output and data types

The result of eig will be of the same numeric type as the input matrix, except for the possibility of complex values. If A is of type double, which is the default in MATLAB for numeric matrices, eig(A) returns double precision outputs.

If A is single precision, such as:

matlab
A = single([2 1; 0 3]);
lambda = eig(A);

then lambda will be single precision as well. This can be important in memory constrained settings, but for beginners the default double precision is usually best.

If any eigenvalue is complex, lambda, D, and V will be complex arrays. You can check this with isreal:

matlab
isreal(D)
isreal(V)

These return logical values indicating whether the arrays contain only real values.

Numerical considerations

Eigenvalue computations are subject to rounding errors from floating point arithmetic. For most well behaved matrices, MATLAB’s eig function gives very accurate results. For certain badly conditioned matrices, small perturbations in the data can cause large changes in the eigenvalues or eigenvectors.

For basic practice, it is enough to understand that:

The relation $A V = V D$ may not hold exactly, but only approximately, due to small numerical errors.

Reconstructed matrices, such as V*D/V, will deviate from A by roundoff.

The function norm is helpful to measure how close two matrices or vectors are. For example:

matlab
residual = norm(A*V - V*D);

A small value of residual indicates that the computation is numerically accurate relative to the size of the inputs.

You should be cautious when comparing eigenvalues directly using ==. Instead, compare differences using tolerances. For example:

matlab
abs(lambda1 - lambda2) < 1e-10

is more robust than lambda1 == lambda2.

Simple example workflow

To consolidate the usage, here is a simple example from start to finish.

Create a small matrix:

matlab
A = [4 1; 2 3];

Compute eigenvalues:

matlab
lambda = eig(A)

Now compute eigenvalues and eigenvectors:

matlab
[V, D] = eig(A);

Extract eigenvalues as a vector:

matlab
lambda_from_D = diag(D);

Verify the relationship:

matlab
residual = norm(A*V - V*D);

Inspect the first eigenpair:

matlab
lambda1 = D(1,1);
v1 = V(:,1);
check1 = norm(A*v1 - lambda1*v1);

Look at the reconstructed matrix:

matlab
A_rec = V*D/V;
reconstruction_error = norm(A - A_rec);

By running these commands and looking at the outputs, you build intuition for how MATLAB represents and uses eigenvalues and eigenvectors.

Important points to remember:
Use eig(A) to get eigenvalues. Use [V, D] = eig(A) to get eigenvectors and the diagonal eigenvalue matrix.
Columns of V are eigenvectors. Diagonal entries of D are eigenvalues, and $A V \approx V D$.
Eigenvalues of real matrices are not always real. Complex outputs from eig are normal for nonsymmetric matrices.
For symmetric real matrices, eig returns real eigenvalues and orthonormal eigenvectors, which are usually more numerically stable.
Reconstruction V*D/V approximates the original matrix. Small differences are due to floating point rounding.
Do not rely on exact equality when comparing eigenvalues or checking relations. Use norms or tolerances instead.

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