Ora

How to find eigenvectors in MATLAB?

Published in Linear Algebra MATLAB 5 mins read

In MATLAB, you can effectively find eigenvectors and their corresponding eigenvalues for a square matrix using the eig function. The most common and direct way to obtain both is by assigning the output to two variables: [V, D] = eig(A).

Understanding Eigenvectors and Eigenvalues

Eigenvectors and eigenvalues are fundamental concepts in linear algebra with wide applications in various fields like physics, engineering, data science, and finance.

  • An eigenvector of a square matrix is a non-zero vector that, when multiplied by the matrix, only changes by a scalar factor. It essentially retains its direction (or reverses it).
  • The eigenvalue is that scalar factor. It tells you how much the eigenvector is scaled in its original direction (or reversed direction) after transformation by the matrix.

In simpler terms, if you have a transformation represented by a matrix, eigenvectors are the special vectors whose direction doesn't change, only their magnitude.

Using the eig Function in MATLAB

MATLAB's eig function is your primary tool for computing eigenvalues and eigenvectors.

Basic Syntax for Eigenvectors and Eigenvalues

To find both the eigenvectors and eigenvalues of a square matrix A, you use the following syntax:

[V, D] = eig(A)

Here's what the outputs represent:

  • V: This is a full matrix whose columns are the right eigenvectors of matrix A. Each column of V corresponds to an eigenvector.
  • D: This is a diagonal matrix where the diagonal elements are the eigenvalues corresponding to the eigenvectors in V. The eigenvalue at D(i,i) corresponds to the eigenvector in V(:,i).

A key relationship that defines these components is that A*V = V*D. This equation verifies that applying the transformation matrix A to the eigenvectors V yields the same result as scaling the eigenvectors V by their respective eigenvalues D.

Eigenvalues Only

If you only need the eigenvalues and not the eigenvectors, you can use a simpler syntax:

e = eig(A)

In this case, e will be a column vector containing all the eigenvalues of A.

eig Function Syntax Summary

The table below summarizes the common eig function syntaxes:

Syntax Output e / V, D Description
e = eig(A) A column vector e containing the eigenvalues. Returns only the eigenvalues of matrix A.
[V, D] = eig(A) V is a matrix of right eigenvectors; D is a diagonal matrix of corresponding eigenvalues. Returns both eigenvectors and eigenvalues such that A*V = V*D.

Step-by-Step Example

Let's illustrate how to find eigenvectors in MATLAB with a practical example.

1. Define the Matrix

First, define a square matrix for which you want to find the eigenvectors and eigenvalues.

% Define a 3x3 matrix A
A = [4 -2 1; 2 0 1; 2 -2 3];
disp('Matrix A:');
disp(A);

2. Calculate Eigenvectors and Eigenvalues

Now, use the eig function with two output arguments to get both V and D.

% Calculate eigenvectors (V) and eigenvalues (D)
[V, D] = eig(A);

disp('Eigenvectors (V):');
disp(V);
disp('Eigenvalues (D, diagonal matrix):');
disp(D);

Interpretation of the Results:

  • V (Eigenvectors): Each column of V is an eigenvector. For example, V(:,1) is the first eigenvector, V(:,2) is the second, and so on.
  • D (Eigenvalues): The diagonal elements of D are the eigenvalues. D(1,1) is the eigenvalue corresponding to V(:,1), D(2,2) corresponds to V(:,2), and so forth. You can extract them as a vector using diag(D).
% Extract eigenvalues as a vector
eigenvalues = diag(D);
disp('Eigenvalues (as a vector):');
disp(eigenvalues);

% Access individual eigenvector and eigenvalue
disp('First eigenvector:');
disp(V(:,1));
disp('First eigenvalue:');
disp(eigenvalues(1));

3. Verify the Eigenvalue Equation

You can verify the fundamental relationship A*V = V*D to ensure the correctness of the results. The difference between A*V and V*D should be a matrix of very small numbers, close to zero, due to floating-point arithmetic.

% Verify the relationship A*V = V*D
verification_result = A*V - V*D;
disp('Verification (A*V - V*D):');
disp(verification_result);

The verification_result should show values extremely close to zero (e.g., 1.0e-15 * [...]), confirming that the eigenvectors and eigenvalues satisfy the defining equation.

Important Considerations for Eigenvectors in MATLAB

When working with eigenvectors and eigenvalues in MATLAB, keep the following points in mind:

  • Normalization: MATLAB typically normalizes eigenvectors to have a Euclidean norm (length) of 1. This means norm(V(:,i)) will be 1 for each eigenvector i.
  • Direction and Scale: Eigenvectors are unique only up to a scalar multiple. If v is an eigenvector, then c*v (where c is any non-zero scalar) is also an eigenvector for the same eigenvalue. MATLAB's eig function applies a consistent normalization scheme.
  • Ordering: The eigenvalues in D (and thus the corresponding eigenvectors in V) are not necessarily sorted. If you need them in a specific order (e.g., ascending or descending magnitude), you'll need to sort them manually and rearrange V accordingly.
  • Complex Eigenvectors: For non-symmetric real matrices, or any complex matrix, eigenvalues and eigenvectors can be complex numbers. MATLAB handles these automatically, returning complex outputs if necessary.
  • Numerical Precision: Due to the nature of floating-point arithmetic, results might sometimes contain very small imaginary components or values extremely close to zero (like 1.0e-15), which should be interpreted as zero.