15.3. THE QR ALGORITHM 437

Example 15.3.10 Let A =

1 2 3 42 −2 −3 33 −3 5 14 3 1 −3

 a symmetric matrix. Thus it has real

eigenvalues and can be diagonalized. Find its eigenvalues.

As explained above, there is an upper Hessenberg matrix. Matlab can find it using thetechniques given above pretty quickly. The syntax is as follows.

A=[2 1 3;-5,3,-2;1,2,3];[P,H]=hess(A)

Then the Hessenberg matrix similar to A is

H =

−1.4476 −4.9048 0 0−4.9048 3.2553 −2.0479 0

0 −2.0479 2.1923 −5.09900 0 −5.0990 −3

Note how it is symmetric also. This will always happen when you begin with a symmetricmatrix. Now use the QR algorithm on this matrix. The syntax is as follows in Matlab.

H=[enter H here]hold onfor k=1:100[Q,R]=qr(H);H=R*Q;endQRH

You already have H and matlab knows about it so you don’t need to enter H again. Thisyields the following matrix similar to the original one.

7.4618 0 0 00 −6.3804 0 00 0 −4.419 −.36790 0 −.3679 4.3376

The eigenvalues of this matrix are

7.4618,−6.3804,4.353,−4.4344

You might want to check that the product of these equals the determinant of the matrixand that the sum equals the trace of the matrix. In fact, this works out very well. To findeigenvectors, you could use the shifted inverse power method. They will be different forthe Hessenberg matrix than for the original matrix A.