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

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

Comments

Please login to add a comment.

Don't have an account? Register now!