%% Find Determinant of Singular Matrix
% Examine a matrix that is exactly singular, but which has a large nonzero determinant. In theory, the determinant of any singular matrix is zero, but because of the nature of floating-point computation, this ideal is not always achievable.
% Create a 13-by-13 diagonally dominant singular matrix A and view the pattern of nonzero elements.

A = diag([24 46 64 78 88 94 96 94 88 78 64 46 24]);
S = diag([-13 -24 -33 -40 -45 -48 -49 -48 -45 -40 -33 -24],1);
A = A + S + rot90(S,2);
spy(A)

% A is singular because the rows are linearly dependent. For instance, sum(A) produces a vector of zeros.
%
% Calculate the determinant of A.

d = det(A)

% The determinant of A is quite large despite the fact that A is singular. In fact, the determinant of A should be exactly zero! The inaccuracy of d is due to an aggregation of round-off errors in the MATLAB® implementation of the LU decomposition, which det uses to calculate the determinant. This result demonstrates a few important aspects of calculating numeric determinants. See the Limitations section for more details.
% Copyright 2012 The MathWorks, Inc.