%% 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])

%
% 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)

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

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

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

format short
A*V1 - A*V1*D1

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

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.
% Copyright 2012 The MathWorks, Inc.