Eigenvalues and Eigenvectors - MATLAB Eig - MathWorks India
Eigenvalues and Eigenvectors - MATLAB Eig - MathWorks India
eig
Eigenvalues and eigenvectors
Syntax
e = eig(A)
[V,D] = eig(A)
[V,D,W] = eig(A)
e = eig(A,B)
[V,D] = eig(A,B)
[V,D,W] = eig(A,B)
[ ___ ] = eig(A,balanceOption)
[ ___ ] = eig(A,B,algorithm)
Description
e = eig(A) returns a column vector containing the eigenvalues of square matrix A. example
example
[V,D] = eig(A) returns diagonal matrix D of eigenvalues and matrix V whose columns are the
corresponding right eigenvectors, so that A*V = V*D.
example
[V,D,W] = eig(A) also returns full matrix 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’.
e = eig(A,B) returns a column vector containing the generalized eigenvalues of square matrices A and example
B.
example
[V,D] = eig(A,B) returns diagonal matrix D of generalized eigenvalues and full matrix V whose columns
are the corresponding right eigenvectors, so that A*V = B*V*D.
[V,D,W] = eig(A,B) also returns full matrix 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.
https://in.mathworks.com/help/matlab/ref/eig.html 1/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
[ ___ ] = eig( ___ ,eigvalOption) returns the eigenvalues in the form specified by eigvalOption using example
any of the input or output arguments in previous syntaxes. Specify eigvalOption as 'vector' to return
the eigenvalues in a column vector or as 'matrix' to return the eigenvalues in a diagonal matrix.
Eigenvalues of Matrix
A = gallery('lehmer',4)
A = 4×4
e = eig(A)
e = 4×1
0.2078
0.4078
0.8482
2.5362
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
A = gallery('circul',3)
A = 3×3
https://in.mathworks.com/help/matlab/ref/eig.html 2/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
1 2 3
3 1 2
2 3 1
[V,D] = eig(A)
V = 3×3 complex
D = 3×3 complex
A*V - V*D
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.
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
https://in.mathworks.com/help/matlab/ref/eig.html 3/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
11 18 25 2 9
[V,D] = eig(A)
V = 5×5
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
https://in.mathworks.com/help/matlab/ref/eig.html 4/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
0 0 0 0 65.0000
Vs = V(:,ind)
Vs = 5×5
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 = 1.8933e-29
Left Eigenvectors
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
D = 3×3
25.5548 0 0
0 -0.5789 0
0 0 -7.9759
W = 3×3
W'*A - D*W'
https://in.mathworks.com/help/matlab/ref/eig.html 5/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
ans = 3×3
10-13 ×
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.
A = [3 1 0; 0 3 1; 0 0 3];
[V,D] = eig(A)
V = 3×3
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.
https://in.mathworks.com/help/matlab/ref/eig.html 6/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
Generalized Eigenvalues
A = [1/sqrt(2) 0; 0 1];
B = [0 1; -1/sqrt(2) 0];
[V,D]=eig(A,B)
V = 2×2 complex
D = 2×2 complex
A*V - B*V*D
ans = 2×2
0 0
0 0
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
https://in.mathworks.com/help/matlab/ref/eig.html 7/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
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
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.
A = eye(2);
B = [3 6; 4 8];
https://in.mathworks.com/help/matlab/ref/eig.html 8/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
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 Av = λBv 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.
A — 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.
https://in.mathworks.com/help/matlab/ref/eig.html 9/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
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.
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.
'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.
Eigenvalue option, 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.
https://in.mathworks.com/help/matlab/ref/eig.html 10/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
Eigenvalues, returned as a column vector containing the eigenvalues (or generalized eigenvalues of a pair) with
multiplicity.
When A is real and symmetric or complex Hermitian, the values of e that satisfy Av = λv are real.
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 matrix V, whose columns are the right eigenvectors of A such that A*V = V*D. The
eigenvectors in V are normalized so that the 2-norm of each is 1.
If A is real symmetric, then the right eigenvectors, V, are orthonormal.
• [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) returns V as a matrix whose columns are the
generalized right 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.
When eig uses the 'chol' algorithm with symmetric (Hermitian) A and symmetric (Hermitian) positive definite
B, it normalizes the eigenvectors in V so that the B-norm of each is 1.
Different machines and releases of MATLAB® can produce different eigenvectors that are still numerically accurate:
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.
When A is real and symmetric or complex Hermitian, the values of D that satisfy Av = λv are real.
W — Left eigenvectors
square matrix
https://in.mathworks.com/help/matlab/ref/eig.html 11/12
9/21/2019 Eigenvalues and eigenvectors - MATLAB eig - MathWorks India
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 matrix W, whose columns are the left eigenvectors of A such that W'*A = D*W'. The
eigenvectors in W are normalized so that the 2-norm of each is 1. If A is symmetric, then W is the same as V.
• [V,D,W] = eig(A,'nobalance') also returns matrix W. 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) returns W as a matrix whose columns are the
generalized left eigenvectors that satisfy W'*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 and B are symmetric, then W is the same as V.
Different machines and releases of MATLAB can produce different eigenvectors that are still numerically accurate:
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
the eigs function.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing
Toolbox™.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing
Toolbox™.
See Also
balance | condeig | eigs | hess | qz | schur
https://in.mathworks.com/help/matlab/ref/eig.html 12/12