Accelerating the pace of engineering and science

Documentation Center

• Trial Software
• Product Updates

eig

Eigenvalues and eigenvectors

Description

example

lambda = eig(A) returns a vector containing the eigenvalues, with multiplicity, that satisfy the equation,

Av = λv

where A is an n-by-n matrix, v is a column vector of length n, and λ is a scalar. The values of λ that satisfy the equation are the eigenvalues. The corresponding values of v that satisfy the equation are right eigenvectors.

example

lambda = eig(A,balanceOption) specifies a balancing option as one of two strings: 'balance', which enables a preliminary balancing step, or 'nobalance' which disables it.

example

lambda = eig(A,B) returns a vector containing the generalized eigenvalues of the pair, (A,B), that satisfy the equation,

Av = λBv

where A and B are n-by-n matrices, v is a column vector of length n, and λ is a scalar. The values of λ that satisfy the equation are the generalized eigenvalues. The corresponding values of v are generalized right eigenvectors.

example

lambda = eig(A,B,algorithm) specifies the generalized eigenvalue algorithm as one of two strings: 'qz', which uses the QZ algorithm, or 'chol', which uses the Cholesky factorization of B.

example

[V,D] = eig(___) returns two optional outputs for any of the previous syntaxes. V is a matrix whose columns are eigenvectors. D is a diagonal matrix containing the eigenvalues along the main diagonal. The form and scaling of V depends on the combination of input arguments:

• [V,D] = eig(A) returns matrix V, whose columns are eigenvectors of A that satisfy A*V = V*D. The eigenvectors in V are scaled so that the 2-norm of each is 1.

• [V,D] = eig(A,'nobalance') also returns matrix V. However, the 2-norm of each eigenvector is not necessarily 1.

• [V,D] = eig(A,B) and [V,D] = eig(A,B,algorithm) return V as a matrix whose columns are generalized eigenvectors that satisfy A*V = B*V*D. The 2-norm of each eigenvector is not necessarily 1. In this case, D contains the generalized eigenvalues of the pair, (A,B), along the main diagonal.

Examples

expand all

Eigenvalues of Real Symmetric Matrix

Use the gallery function to create symmetric positive definite matrix.

`A = gallery('lehmer',4)`
```A =

1.0000    0.5000    0.3333    0.2500
0.5000    1.0000    0.6667    0.5000
0.3333    0.6667    1.0000    0.7500
0.2500    0.5000    0.7500    1.0000
```

Calculate the eigenvalues of A.

`lambda = eig(A)`
```lambda =

0.2078
0.4078
0.8482
2.5362
```

Eigenvalues and Eigenvectors of Nonsymmetric Matrix

Use the gallery function to create a circulant matrix.

`A = gallery('circul',3)`
```A =

1     2     3
3     1     2
2     3     1
```

Calculate the eigenvalues and eigenvectors of A.

`[V,D] = eig(A)`
```V =

-0.5774 + 0.0000i   0.2887 - 0.5000i   0.2887 + 0.5000i
-0.5774 + 0.0000i  -0.5774 + 0.0000i  -0.5774 + 0.0000i
-0.5774 + 0.0000i   0.2887 + 0.5000i   0.2887 - 0.5000i

D =

6.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i  -1.5000 + 0.8660i   0.0000 + 0.0000i
0.0000 + 0.0000i   0.0000 + 0.0000i  -1.5000 - 0.8660i
```

Verify that matrix V diagonalizes A, since the eigenvalues are distinct and the eigenvectors are independent.

`V\A*V`
```ans =

6.0000 + 0.0000i   0.0000 - 0.0000i  -0.0000 + 0.0000i
-0.0000 - 0.0000i  -1.5000 + 0.8660i  -0.0000 - 0.0000i
-0.0000 + 0.0000i  -0.0000 + 0.0000i  -1.5000 - 0.8660i```

Eigenvalues and Eigenvectors of a Matrix Whose Elements Differ Dramatically in Scale

```A = [ 3.0     -2.0      -0.9     2*eps;
-2.0      4.0       1.0    -eps;
-eps/4    eps/2    -1.0     0;
-0.5     -0.5       0.1     1.0];```

Calculate the eigenvalues and eigenvectors using the default (balancing) behavior.

`[VB,DB] = eig(A)`
```VB =

0.6153   -0.4176   -0.0000   -0.1437
-0.7881   -0.3261   -0.0000    0.1264
-0.0000   -0.0000   -0.0000   -0.9196
0.0189    0.8481    1.0000    0.3432

DB =

5.5616         0         0         0
0    1.4384         0         0
0         0    1.0000         0
0         0         0   -1.0000```

Check to see if the results satisfy A*VB = VB*DB.

`A*VB - VB*DB`
```ans =

0.0000    0.0000   -0.0000    0.0000
0   -0.0000    0.0000   -0.0000
0.0000   -0.0000    0.0000    0.0000
0    0.0000    0.0000    0.6031```

This result does not satisfy A*VB = VB*DB. Ideally, the eigenvalue decomposition satisfies this relationship. Since MATLAB® performs the decomposition using floating-point computations, then A*V can, at best, approach V*D. In other words, A*V - V*D is close to, but not exactly, 0.

Now, try calculating the eigenvalues and eigenvectors without the balancing step.

`[VN,DN] = eig(A,'nobalance')`
```VN =

0.6153   -0.4176   -0.0000   -0.1528
-0.7881   -0.3261         0    0.1345
-0.0000   -0.0000   -0.0000   -0.9781
0.0189    0.8481   -1.0000    0.0443

DN =

5.5616         0         0         0
0    1.4384         0         0
0         0    1.0000         0
0         0         0   -1.0000```

Verify that the results satisfy A*VN = VN*DN.

`A*VN - VN*DN`
```ans =

1.0e-14 *

-0.1776   -0.0111   -0.0559   -0.0167
0.3553    0.1055    0.0336   -0.0194
0.0017    0.0002    0.0007         0
0.0264   -0.0222    0.0222    0.0097```

A*VN - VN*DN is much closer to 0, so the 'nobalance' option produces more accurate results in this case.

Left Eigenvectors

` A = [1 7 3; 2 9 12; 5 22 7];`

Pass the transpose of A to the eig function.

`[VL,D] = eig(A')`
```VL =

-0.1791   -0.9587   -0.1881
-0.8127    0.0649   -0.7477
-0.5545    0.2768    0.6368

D =

25.5548         0         0
0   -0.5789         0
0         0   -7.9759```

Matrix VL contains the transpose of the left eigenvectors.

Transpose VL to get the left eigenvectors.

`VL = VL';`

Verify the results satisfy VL*A = D*VL.

`VL*A - D*VL`
```ans =

1.0e-13 *

0.1243    0.2132    0.0178
-0.0167   -0.0644   -0.0339
-0.0289   -0.0444    0.0089```

Ideally, the eigenvalue decomposition satisfies the relationship. Since MATLAB performs the decomposition using floating-point computations, then VL*A can, at best, approach D*VL. In other words, VL*A - D*VL is close to, but not exactly, 0.

Eigenvalues and Eigenvectors of Nondiagonalizable (Defective) Matrix

`A = [3 1 0; 0 3 1; 0 0 3];`

Calculate the eigenvalues and eigenvectors of A.

`[V,D] = eig(A)`
```V =

1.0000   -1.0000    1.0000
0    0.0000   -0.0000
0         0    0.0000

D =

3     0     0
0     3     0
0     0     3```

A has repeated eigenvalues and the eigenvectors are not independent. This means that A is not diagonalizable and is, therefore, defective.

Verify that V and D satisfy the equation, A*V = V*D, even though A is defective.

`A*V - V*D`
```ans =

1.0e-15 *

0    0.8882   -0.8882
0         0    0.0000
0         0         0```

Ideally, the eigenvalue decomposition satisfies the relationship. Since MATLAB performs the decomposition using floating-point computations, then A*V can, at best, approach V*D. In other words, A*V - V*D is close to, but not exactly, 0.

Generalized Eigenvalues and Eigenvectors

```A = [1/sqrt(2) 0; 0 1];
B = [0 1; -1/sqrt(2) 0];
[V,D]=eig(A,B)```
```V =

1.0000 + 0.0000i   1.0000 + 0.0000i
0.0000 - 0.7071i   0.0000 + 0.7071i

D =

0.0000 + 1.0000i   0.0000 + 0.0000i
0.0000 + 0.0000i   0.0000 - 1.0000i```

Verify that the results satisfy A*V = B*V*D.

`A*V - B*V*D`
```ans =

0     0
0     0```

Generalized Eigenvalues Using QZ Algorithm for Badly Conditioned Matrices

Create a badly conditioned symmetric matrix containing values close to machine precision.

```format long e
A = diag([10^-16, 10^-15])```
```A =

1.000000000000000e-16                         0
0     1.000000000000000e-15```

Calculate the generalized eigenvalues and a set of eigenvectors using the default algorithm. In this case, the default algorithm is 'chol'.

`[V1,D1] = eig(A,A)`
```V1 =

1.000000000000000e+08                         0
0     3.162277660168380e+07

D1 =

9.999999999999999e-01                         0
0     1.000000000000000e+00```

Now, calculate the generalized eigenvalues and a set of eigenvectors using the 'qz' algorithm.

`[V2,D2] = eig(A,A,'qz')`
```V2 =

1     0
0     1

D2 =

1     0
0     1
```

Check to see how well the 'chol' result satisfies A*V1 = A*V1*D1.

```format
A*V1 - A*V1*D1```
```ans =

1.0e-23 *

0.1654         0
0   -0.6617```

Now, check to see how well the 'qz' result satisfies A*V2 = A*V2*D2.

`A*V2 - A*V2*D2`
```ans =

0     0
0     0```

When both matrices are Hermitian, eig uses the 'chol' algorithm by default. In this case, the QZ algorithm returns more accurate results.

Generalized Eigenvalues and Eigenvectors of a Pair in Which One Matrix is Singular

Create a 2-by-2 identity matrix, A, and a singular matrix, B.

```A = eye(2);
B = [3 6; 4 8];```

Try to calculate the generalized eigenvalues of the matrix, B-1A.

```[V,D] = eig(B\A)
```
```Warning: Matrix is singular to working precision.
Error using eig
Input to EIG must not contain NaN or Inf.```

Now calculate the generalized eigenvalues and eigenvectors by passing both matrices to the eig function.

```[V,D] = eig(A,B)
```
```V =

-0.7500   -1.0000
-1.0000    0.5000

D =

0.0909         0
0       Inf```

It's better to pass both matrices separately, and let eig choose the best algorithm to solve the problem. In this case, eig(A,B) returned a set of eigenvectors and at least one real eigenvalue, even though B is not invertible.

Verify Av = λBv for the first eigenvalue and the first eigenvector.

```eigval = D(1,1);
eigvec = V(:,1);
A*eigvec - eigval*B*eigvec```
```ans =

1.0e-15 *

0.1110
0.2220```

Ideally, the eigenvalue decomposition satisfies the relationship. Since the decomposition is performed using floating-point computations, then A*eigvec can, at best, approach eigval*B*eigvec, as it does in this case.

Input Arguments

expand all

A — Input matrixsquare matrix

Input matrix, specified as a real or complex square matrix.

Data Types: double | single
Complex Number Support: Yes

B — Generalized eigenvalue problem input matrixsquare matrix

Generalized eigenvalue problem input matrix, specified as a square matrix of real or complex values. B must be the same size as A.

Data Types: double | single
Complex Number Support: Yes

balanceOption — Balance option'balance' (default) | 'nobalance'

Balance option, specified as one two strings: 'balance', which enables a preliminary balancing step, or 'nobalance' which disables it. In most cases, the balancing step improves the conditioning of A to produce more accurate results. However, there are cases in which balancing produces incorrect results. Specify 'nobalance' when A contains values whose scale differs dramatically. For example, if A contains nonzero integers, as well as very small (near zero) values, then the balancing step might scale the small values to make them as significant as the integers and produce inaccurate results.

'balance' is the default behavior. The eig function ignores balanceOption when A is symmetric.

Data Types: char

algorithm — Generalized eigenvalue algorithm'chol' | 'qz'

Generalized eigenvalue algorithm, specified as 'chol' or 'qz', which selects the algorithm to use for calculating the generalized eigenvalues of a pair.

algorithmDescription
'chol'Computes the generalized eigenvalues of A and B using the Cholesky factorization of B.
'qz'Uses the QZ algorithm, also known as the generalized Schur decomposition. This algorithm ignores the symmetry of A and B.

In general, the two algorithms return the same result. The QZ algorithm can be more stable for certain problems, such as those involving badly conditioned matrices.

When you omit the algorithm argument, the eig function selects an algorithm based on the properties of A and B. It uses the 'chol' algorithm for symmetric (Hermitian) A and symmetric (Hermitian) positive definite B. Otherwise, it uses the 'qz' algorithm.

Regardless of the algorithm you specify, the eig function always uses the QZ algorithm when A or B are not symmetric.

Output Arguments

expand all

lambda — Eigenvaluescolumn vector

Eigenvalues, returned as a column vector containing the eigenvalues (or generalized eigenvalues of a pair) with multiplicity.

The values of lambda that satisfy Av = λv are always real and sorted in descending order whenever A is:

• real and symmetric

• or complex Hermitian

V — Eigenvectorssquare matrix

Eigenvectors, returned as a square matrix whose columns are eigenvectors of A or generalized eigenvectors of the pair, (A,B).

D — Diagonal eigenvalues matrixdiagonal matrix

Diagonal eigenvalues matrix, returned a matrix containing the eigenvalues of A or the generalized eigenvalues of the pair, (A,B), with multiplicity.

When the eigenvalues in D satisfy the equation, Av = λv, the eigenvalues are always real. In this case, MATLAB sorts them in descending order (from upper-left to lower-right) as long as A is real and symmetric, or complex Hermitian.

More About

expand all

Tips

• The eig function returns the eigenvalues of a real, symmetric, sparse matrix for the syntax, lambda = eig(A). Use the eigs function for all other sparse eigenvalue and eigenvector problems.

See Also

Was this topic helpful?