eig
Eigenvalues and eigenvectors
Syntax
Description
[
also returns full matrix V
,D
,W
]
= eig(A
)W
whose
columns are the corresponding left eigenvectors, so that W'*A
= D*W'
.
The eigenvalue problem is to determine the solution to 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 the right eigenvectors. The left eigenvectors, w,
satisfy the equation w’A = λw’.
[
also
returns full matrix V
,D
,W
]
= eig(A
,B
)W
whose columns are the corresponding
left eigenvectors, so that W'*A = D*W'*B
.
The generalized eigenvalue problem is to determine the solution
to 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 the generalized right eigenvectors. The
left eigenvectors, w, satisfy the equation w’A = λw’B.
[___] = eig(
,
where A
,balanceOption
)balanceOption
is "nobalance"
,
disables the preliminary balancing step in the algorithm. The default for
balanceOption
is "balance"
, which
enables balancing. The eig
function can return any of the
output arguments in previous syntaxes.
[___] = eig(___,
returns the eigenvalues in the form specified by outputForm
)outputForm
using any of the input or output arguments in previous syntaxes. Specify
outputForm
as "vector"
to return the
eigenvalues in a column vector or as "matrix"
to return the
eigenvalues in a diagonal matrix.
Examples
Eigenvalues of Matrix
Use gallery
to create a symmetric positive definite matrix.
A = gallery("lehmer",4)
A = 4×4
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
. The result is a column vector.
e = eig(A)
e = 4×1
0.2078
0.4078
0.8482
2.5362
Alternatively, use outputForm
to return the eigenvalues in a diagonal matrix.
D = eig(A,"matrix")
D = 4×4
0.2078 0 0 0
0 0.4078 0 0
0 0 0.8482 0
0 0 0 2.5362
Eigenvalues and Eigenvectors of Matrix
Use gallery
to create a circulant matrix.
A = gallery("circul",3)
A = 3×3
1 2 3
3 1 2
2 3 1
Calculate the eigenvalues and right eigenvectors of A
.
[V,D] = eig(A)
V = 3×3 complex
-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 = 3×3 complex
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 the results satisfy A*V = V*D
.
A*V - V*D
ans = 3×3 complex
10-14 ×
-0.2665 + 0.0000i -0.0444 + 0.0222i -0.0444 - 0.0222i
0.0888 + 0.0000i 0.0111 + 0.0777i 0.0111 - 0.0777i
-0.0444 + 0.0000i -0.0111 + 0.0833i -0.0111 - 0.0833i
Ideally, the eigenvalue decomposition satisfies the relationship. Since eig
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
.
Sorted Eigenvalues and Eigenvectors
By default eig
does not always return the eigenvalues and eigenvectors in sorted order. Use the sort
function to put the eigenvalues in ascending order and reorder the corresponding eigenvectors.
Calculate the eigenvalues and eigenvectors of a 5-by-5 magic square matrix.
A = magic(5)
A = 5×5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
[V,D] = eig(A)
V = 5×5
-0.4472 0.0976 -0.6330 0.6780 -0.2619
-0.4472 0.3525 0.5895 0.3223 -0.1732
-0.4472 0.5501 -0.3915 -0.5501 0.3915
-0.4472 -0.3223 0.1732 -0.3525 -0.5895
-0.4472 -0.6780 0.2619 -0.0976 0.6330
D = 5×5
65.0000 0 0 0 0
0 -21.2768 0 0 0
0 0 -13.1263 0 0
0 0 0 21.2768 0
0 0 0 0 13.1263
The eigenvalues of A
are on the diagonal of D
. However, the eigenvalues are unsorted.
Extract the eigenvalues from the diagonal of D
using diag(D)
, then sort the resulting vector in ascending order. The second output from sort
returns a permutation vector of indices.
[d,ind] = sort(diag(D))
d = 5×1
-21.2768
-13.1263
13.1263
21.2768
65.0000
ind = 5×1
2
3
5
4
1
Use ind
to reorder the diagonal elements of D
. Since the eigenvalues in D
correspond to the eigenvectors in the columns of V
, you must also reorder the columns of V
using the same indices.
Ds = D(ind,ind)
Ds = 5×5
-21.2768 0 0 0 0
0 -13.1263 0 0 0
0 0 13.1263 0 0
0 0 0 21.2768 0
0 0 0 0 65.0000
Vs = V(:,ind)
Vs = 5×5
0.0976 -0.6330 -0.2619 0.6780 -0.4472
0.3525 0.5895 -0.1732 0.3223 -0.4472
0.5501 -0.3915 0.3915 -0.5501 -0.4472
-0.3223 0.1732 -0.5895 -0.3525 -0.4472
-0.6780 0.2619 0.6330 -0.0976 -0.4472
Both (V,D)
and (Vs,Ds)
produce the eigenvalue decomposition of A
. The results of A*V-V*D
and A*Vs-Vs*Ds
agree, up to round-off error.
e1 = norm(A*V-V*D); e2 = norm(A*Vs-Vs*Ds); e = abs(e1 - e2)
e = 0
Left Eigenvectors
Create a 3-by-3 matrix.
A = [1 7 3; 2 9 12; 5 22 7];
Calculate the right eigenvectors, V
, the eigenvalues, D
, and the left eigenvectors, W
.
[V,D,W] = eig(A)
V = 3×3
-0.2610 -0.9734 0.1891
-0.5870 0.2281 -0.5816
-0.7663 -0.0198 0.7912
D = 3×3
25.5548 0 0
0 -0.5789 0
0 0 -7.9759
W = 3×3
-0.1791 -0.9587 -0.1881
-0.8127 0.0649 -0.7477
-0.5545 0.2768 0.6368
Verify that the results satisfy W'*A = D*W'
.
W'*A - D*W'
ans = 3×3
10-13 ×
-0.0444 -0.1066 -0.0888
-0.0011 0.0442 0.0333
0 0.0266 0.0178
Ideally, the eigenvalue decomposition satisfies the relationship. Since eig
performs the decomposition using floating-point computations, then W'*A
can, at best, approach D*W'
. In other words, W'*A - D*W'
is close to, but not exactly, 0
.
Eigenvalues of Nondiagonalizable (Defective) Matrix
Create a 3-by-3 matrix.
A = [3 1 0; 0 3 1; 0 0 3];
Calculate the eigenvalues and right eigenvectors of A
.
[V,D] = eig(A)
V = 3×3
1.0000 -1.0000 1.0000
0 0.0000 -0.0000
0 0 0.0000
D = 3×3
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 = 3×3
10-15 ×
0 0.8882 -0.8882
0 0 0.0000
0 0 0
Ideally, the eigenvalue decomposition satisfies the relationship. Since eig
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
Create two matrices, A
and B
, then solve the generalized eigenvalue problem for the eigenvalues and right eigenvectors of the pair (A,B)
.
A = [1/sqrt(2) 0; 0 1]; B = [0 1; -1/sqrt(2) 0]; [V,D]=eig(A,B)
V = 2×2 complex
1.0000 + 0.0000i 1.0000 + 0.0000i
0.0000 - 0.7071i 0.0000 + 0.7071i
D = 2×2 complex
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 = 2×2
0 0
0 0
The residual error A*V - B*V*D
is exactly zero.
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 = 2×2
1.000000000000000e-16 0
0 1.000000000000000e-15
Calculate the generalized eigenvalues and a set of right eigenvectors using the default algorithm. In this case, the default algorithm is "chol"
.
[V1,D1] = eig(A,A)
V1 = 2×2
1.000000000000000e+08 0
0 3.162277660168380e+07
D1 = 2×2
9.999999999999999e-01 0
0 1.000000000000000e+00
Now, calculate the generalized eigenvalues and a set of right eigenvectors using the "qz"
algorithm.
[V2,D2] = eig(A,A,"qz")
V2 = 2×2
1 0
0 1
D2 = 2×2
1 0
0 1
Check how well the "chol"
result satisfies A*V1 = A*V1*D1
.
format short
A*V1 - A*V1*D1
ans = 2×2
10-23 ×
0.1654 0
0 -0.6617
Now, check how well the "qz"
result satisfies A*V2 = A*V2*D2
.
A*V2 - A*V2*D2
ans = 2×2
0 0
0 0
When both matrices are symmetric, eig
uses the "chol"
algorithm by default. In this case, the QZ algorithm returns more accurate results.
Generalized Eigenvalues Where 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];
If you attempt to calculate the generalized eigenvalues of the matrix with the command [V,D] = eig(B\A)
, then MATLAB® returns an error because B\A
produces Inf
values.
Instead, calculate the generalized eigenvalues and right eigenvectors by passing both matrices to the eig
function.
[V,D] = eig(A,B)
V = 2×2
-0.7500 -1.0000
-1.0000 0.5000
D = 2×2
0.0909 0
0 Inf
It is better to pass both matrices separately, and let eig
choose the best algorithm to solve the problem. In this case, eig(A,B)
returns a set of eigenvectors and at least one real eigenvalue, even though B
is not invertible.
Verify for the first eigenvalue and the first eigenvector.
eigval = D(1,1); eigvec = V(:,1); A*eigvec - eigval*B*eigvec
ans = 2×1
10-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
A
— Input matrix
square matrix
Input matrix, specified as a real or complex square matrix.
Data Types: double
| single
Complex Number Support: Yes
B
— Generalized eigenvalue problem input matrix
square 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: "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. For more information about balancing,
see balance
.
algorithm
— Generalized eigenvalue algorithm
"chol"
(default) | "qz"
Generalized eigenvalue algorithm, specified as "chol"
or
"qz"
, which selects the algorithm to use for
calculating the generalized eigenvalues of a pair.
algorithm | Description |
---|---|
"chol" | Computes the generalized eigenvalues of A and B using
the Cholesky factorization of B . If
A is not symmetric (Hermitian) or if
B is not symmetric (Hermitian)
positive definite, eig uses the QZ
algorithm instead. |
"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.
Regardless of the algorithm you specify, the eig
function
always uses the QZ algorithm when A
or B
are
not symmetric.
outputForm
— Output format of eigenvalues
"vector"
| "matrix"
Output format of eigenvalues, specified as "vector"
or
"matrix"
. This option allows you to specify whether
the eigenvalues are returned in a column vector or a diagonal matrix. The
default behavior varies according to the number of outputs specified:
If you specify one output, such as
e = eig(A)
, then the eigenvalues are returned as a column vector by default.If you specify two or three outputs, such as
[V,D] = eig(A)
, then the eigenvalues are returned as a diagonal matrix,D
, by default.
Example: D = eig(A,"matrix")
returns a diagonal matrix
of eigenvalues with the one output syntax.
Output Arguments
e
— Eigenvalues (returned as vector)
column vector
Eigenvalues, returned as a column vector containing the eigenvalues (or generalized
eigenvalues of a pair) with multiplicity. Each eigenvalue
e(k)
corresponds with the right eigenvector
V(:,k)
and the left eigenvector
W(:,k)
.
When
A
is real symmetric or complex Hermitian, the values ofe
that satisfy Av = λv are real.When
A
is real skew-symmetric or complex skew-Hermitian, the values ofe
that satisfy Av = λv are imaginary.
Depending on whether you specify one output or multiple outputs,
eig
can return different eigenvalues that are still
numerically accurate.
V
— Right eigenvectors
square matrix
Right eigenvectors, returned as a square matrix whose columns
are the right eigenvectors of A
or generalized
right eigenvectors of the pair, (A,B)
. The form
and normalization of V
depends on the combination
of input arguments:
[V,D] = eig(A)
returns matrixV
, whose columns are the right eigenvectors ofA
such thatA*V = V*D
. The eigenvectors inV
are normalized so that the 2-norm of each is 1.If
A
is real symmetric, Hermitian, or skew-Hermitian, then the right eigenvectorsV
are orthonormal.[V,D] = eig(A,"nobalance")
also returns matrixV
. However, the 2-norm of each eigenvector is not necessarily 1.[V,D] = eig(A,B)
and[V,D] = eig(A,B,algorithm)
returnV
as a matrix whose columns are the generalized right eigenvectors that satisfyA*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.When
eig
uses the"chol"
algorithm with symmetric (Hermitian)A
and symmetric (Hermitian) positive definiteB
, it normalizes the eigenvectors inV
so that theB
-norm of each is 1.
Different machines and releases of MATLAB® can produce different eigenvectors that are still numerically accurate:
For real eigenvectors, the sign of the eigenvectors can change.
For complex eigenvectors, the eigenvectors can be multiplied by any complex number of magnitude 1.
For a multiple eigenvalue, its eigenvectors can be recombined through linear combinations. For example, if Ax = λx and Ay = λy, then A(x+y) = λ(x+y), so x+y also is an eigenvector of A.
D
— Eigenvalues (returned as matrix)
diagonal matrix
Eigenvalues, returned as a diagonal matrix with the eigenvalues of A
on the
main diagonal or the eigenvalues of the pair, (A,B)
, with
multiplicity, on the main diagonal. Each eigenvalue
D(k,k)
corresponds with the right eigenvector
V(:,k)
and the left eigenvector
W(:,k)
.
When
A
is real symmetric or complex Hermitian, the values ofD
that satisfy Av = λv are real.When
A
is real skew-symmetric or complex skew-Hermitian, the values ofD
that satisfy Av = λv are imaginary.
Depending on whether you specify one output or multiple outputs,
eig
can return different eigenvalues that are still
numerically accurate.
W
— Left eigenvectors
square matrix
Left eigenvectors, returned as a square matrix whose columns
are the left eigenvectors of A
or generalized left
eigenvectors of the pair, (A,B)
. The form and normalization
of W
depends on the combination of input arguments:
[V,D,W] = eig(A)
returns matrixW
, whose columns are the left eigenvectors ofA
such thatW'*A = D*W'
. The eigenvectors inW
are normalized so that the 2-norm of each is 1. IfA
is symmetric, thenW
is the same asV
.[V,D,W] = eig(A,"nobalance")
also returns matrixW
. However, the 2-norm of each eigenvector is not necessarily 1.[V,D,W] = eig(A,B)
and[V,D,W] = eig(A,B,algorithm)
returnsW
as a matrix whose columns are the generalized left eigenvectors that satisfyW'*A = D*W'*B
. 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.If
A
andB
are symmetric, thenW
is the same asV
.
Different machines and releases of MATLAB can produce different eigenvectors that are still numerically accurate:
For real eigenvectors, the sign of the eigenvectors can change.
For complex eigenvectors, the eigenvectors can be multiplied by any complex number of magnitude 1.
For a multiple eigenvalue, its eigenvectors can be recombined through linear combinations. For example, if Ax = λx and Ay = λy, then A(x+y) = λ(x+y), so x+y also is an eigenvector of A.
More About
Symmetric Matrix
A square matrix,
A
, is symmetric if it is equal to its nonconjugate transpose,A = A.'
.In terms of the matrix elements, this means that
Since real matrices are unaffected by complex conjugation, a real matrix that is symmetric is also Hermitian. For example, the matrix
is both symmetric and Hermitian.
Skew-Symmetric Matrix
A square matrix,
A
, is skew-symmetric if it is equal to the negation of its nonconjugate transpose,A = -A.'
.In terms of the matrix elements, this means that
Since real matrices are unaffected by complex conjugation, a real matrix that is skew-symmetric is also skew-Hermitian. For example, the matrix
is both skew-symmetric and skew-Hermitian.
Hermitian Matrix
A square matrix,
A
, is Hermitian if it is equal to its complex conjugate transpose,A = A'
.In terms of the matrix elements,
The entries on the diagonal of a Hermitian matrix are always real. Because real matrices are unaffected by complex conjugation, a real matrix that is symmetric is also Hermitian. For example, this matrix is both symmetric and Hermitian.
The eigenvalues of a Hermitian matrix are real.
Skew-Hermitian Matrix
A square matrix,
A
, is skew-Hermitian if it is equal to the negation of its complex conjugate transpose,A = -A'
.In terms of the matrix elements, this means that
The entries on the diagonal of a skew-Hermitian matrix are always pure imaginary or zero. Since real matrices are unaffected by complex conjugation, a real matrix that is skew-symmetric is also skew-Hermitian. For example, the matrix
is both skew-Hermitian and skew-symmetric.
The eigenvalues of a skew-Hermitian matrix are purely imaginary or zero.
Tips
The
eig
function can calculate the eigenvalues of sparse matrices that are real and symmetric. To calculate the eigenvectors of a sparse matrix, or to calculate the eigenvalues of a sparse matrix that is not real and symmetric, use theeigs
function.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
V
might represent a different basis of eigenvectors. This representation means that the eigenvector calculated by the generated code might be different in C and C++ code than in MATLAB. The eigenvalues inD
might not be in the same order as in MATLAB. You can verify theV
andD
values by using the eigenvalue problem equationA*V = V*D
.If you specify the LAPACK library callback class, then the code generator supports these options:
The computation of left eigenvectors.
Outputs are complex.
Code generation does not support sparse matrix inputs for this function.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The eig
function
supports GPU array input with these usage notes and limitations:
For the generalized case,
eig(A,B)
,A
andB
must be real symmetric or complex Hermitian. Additionally,B
must be positive definite.The QZ algorithm,
eig(A,B,"qz")
, is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
For the generalized case,
eig(A,B)
,A
andB
must be real symmetric or complex Hermitian. Additionally,B
must be positive definite.These syntaxes are not supported for full distributed arrays:
[__] = eig(A,B,"qz")
[V,D,W] = eig(A,B)
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aR2021b: eig
returns NaN
for nonfinite inputs
eig
returns NaN
values when the input
contains nonfinite values (Inf
or NaN
).
Previously, eig
threw an error when the input contained
nonfinite values.
R2021a: Improved algorithm for skew-Hermitian matrices
The algorithm for input matrices that are skew-Hermitian was improved. With the
function call [V,D] = eig(A)
, where A
is
skew-Hermitian, eig
now guarantees that the matrix of
eigenvectors V
is unitary and the diagonal matrix of eigenvalues
D
is purely imaginary.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)