Use `gallery`

to create a symmetric positive
definite matrix.

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`

. The
result is a column vector.

e =
0.2078
0.4078
0.8482
2.5362

Alternatively, use `eigvalOption`

to
return the eigenvalues in a diagonal matrix.

D =
0.2078 0 0 0
0 0.4078 0 0
0 0 0.8482 0
0 0 0 2.5362

Use `gallery`

to create a circulant matrix.

Calculate the eigenvalues and right eigenvectors of `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 the results satisfy `A*V = V*D`

.

ans =
1.0e-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`

.

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

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

Verify that the results satisfy `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 `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`

.

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

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`

.

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.

Create a 3-by-3 matrix.

Calculate the right eigenvectors, `V`

,
the eigenvalues, `D`

, and the left eigenvectors, `W`

.

V =
-0.2610 -0.9734 0.1891
-0.5870 0.2281 -0.5816
-0.7663 -0.0198 0.7912
D =
25.5548 0 0
0 -0.5789 0
0 0 -7.9759
W =
-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'`

.

ans =
1.0e-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`

.

Create a 3-by-3 matrix.

Calculate the eigenvalues and right eigenvectors of `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.

ans =
1.0e-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`

.

Create two matrices, `A`

and `B`

,
then solve the generalized eigenvalue problem for the eigenvalues
and right eigenvectors of the pair `(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`

.

The residual error `A*V - B*V*D`

is exactly
zero.

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

A =
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 =
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
right eigenvectors using the `'qz'`

algorithm.

V2 =
1 0
0 1
D2 =
1 0
0 1

Check how well the `'chol'`

result satisfies ```
A*V1
= A*V1*D1
```

.

ans =
1.0e-23 *
0.1654 0
0 -0.6617

Now, check how well the `'qz'`

result
satisfies `A*V2 = A*V2*D2`

.

When both matrices are symmetric, `eig`

uses
the `'chol'`

algorithm by default. In this case,
the QZ algorithm returns more accurate results.

Create a `2`

-by-`2`

identity
matrix, `A`

, and a singular matrix, `B`

.

Try to calculate the generalized eigenvalues of the matrix, *B*^{-1}*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 right eigenvectors
by passing both matrices to the `eig`

function.

V =
-0.7500 -1.0000
-1.0000 0.5000
D =
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)`

returned
a set of eigenvectors and at least one real eigenvalue, even though `B`

is
not invertible.

Verify *A**v* = *λ**B**v* for
the first eigenvalue and the first eigenvector.

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.