% MATLAB Demo (Determinants)
% File: detdemo
%
% Let's calculate the determinants of a sequence of
% tridiagonal matrices with n rows and n columns:
%
% An = [ 2 -1 ]
% [-1 2 -1 ]
% [ -1 2 -1 ]
% [ -1 2 ]
%
% [ 2 -1 ]
% [ -1 2 ]
% Each diagonal entry is a 2;
% each superdiagonal and subdiagonal entry is a -1.
%
>> diary detdemo
>> A1 = [2]
A1 =
2
>> D1 = determ(A1)
D1 =
2
%%%%%%
>> A2 = [2 -1; -1 2]
A2 =
2 -1
-1 2
>> D2 = determ(A2)
D2 =
3
%%%%%%
>> n = 3;
>> e = ones(n-1, 1);
>> A3 = 2*eye(n, n) - diag(e, 1) - diag(e, -1)
A3 =
2 -1 0
-1 2 -1
0 -1 2
>> D3 = determ(A3)
D3 =
4
%%%%%%
%
% Do you see a pattern?: D1 = 2; D2 = 3; D3 = 4; ....
%
>> n = 6;
>> e = ones(n-1, 1);
>> A6 = 2*eye(n, n) - diag(e, 1) - diag(e, -1)
A6 =
2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
0 0 0 -1 2 -1
0 0 0 0 -1 2
>> D6 = determ(A6)
D6 =
7.0000
%
% Let An be a tridiagonal matrix with n rows and n columns.
% Each diagonal entry of An is 2; each superdiagonal and subdiagonal
% entry of An is -1.
%
% RESULT: det(An) = n+1.
% PROOF:
%
% det(An) = 2 * (-1)^(1+1)*det(M11) + -1 * (-1)^(1+2)*det(M12).
% ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
% a11 * C11 + a12 * C12
%
% is the cofactor expansion along row 1 for det(An).
% Recall that the cofactor Cij is (-1)^(i+j) * det(Mij).
%
% Observe that C11 simplifies to det(A_n-1).
%
% det(M12) can be obtained by a cofactor expansion along column 1 of M12.
%
% det(M12) = -1 * (-1)^(1+1)*det(A_n-2) = - det(A_n-2).
% ^^^^^^^^^^^^^^^^^^^^^
% a11 * C11 of the matrix M12
%
% The pattern is det(An) = 2*det(A_n-1) - det(A_n-2) with the
% initial conditions: det(A1) = 2 and det(A2) = 3.
% det(An) = 2*(n)-(n-1) = n+1.
%
% One important consequence is that the symmetric matrix An is
% always INVERTIBLE because its determinant is always nonzero.
%
% Verify that the inverse of A6 is also symmetric.
% Verify that the matrix of cofactors of A6 is symmetric.
%
>> format rat
>> invA6 = inv(A6)
invA6 =
6/7 5/7 4/7 3/7 2/7 1/7
5/7 10/7 8/7 6/7 4/7 2/7
4/7 8/7 12/7 9/7 6/7 3/7
3/7 6/7 9/7 12/7 8/7 4/7
2/7 4/7 6/7 8/7 10/7 5/7
1/7 2/7 3/7 4/7 5/7 6/7
>> cofactA6 = cofactor(A6)
cofactA6 =
6 5 4 3 2 1
5 10 8 6 4 2
4 8 12 9 6 3
3 6 9 12 8 4
2 4 6 8 10 5
1 2 3 4 5 6
%
%%% Verify that inv(A6) = 1/det(A6) * adjoint(A6).
%%% The adjoint is the transpose of the matrix of cofactors.
%
>> adjA6 = cofactA6'
adjA6 =
6 5 4 3 2 1
5 10 8 6 4 2
4 8 12 9 6 3
3 6 9 12 8 4
2 4 6 8 10 5
1 2 3 4 5 6
>> temp = 1/det(A6) * adjA6
temp =
6/7 5/7 4/7 3/7 2/7 1/7
5/7 10/7 8/7 6/7 4/7 2/7
4/7 8/7 12/7 9/7 6/7 3/7
3/7 6/7 9/7 12/7 8/7 4/7
2/7 4/7 6/7 8/7 10/7 5/7
1/7 2/7 3/7 4/7 5/7 6/7
>> A6 * temp
ans =
1 0 0 0 0 0
* 1 0 0 0 0
* * 1 * 0 0
0 0 * 1 0 0
0 0 * 0 1 *
0 0 0 0 0 1
%
%%% Each "*" is presumed to be zero. Evidently some rounding errors
%%% were present in multiplying A and its inverse.
%
>> quit