function ShowSherman()
% Illustrates an application of the Sherman-Morrison formula.
% This result relates the inverse of a matrix to the inverse of
% a rank-1 modification of the matrix

%
% Theorem
% Given A (nxn, nonsingular), u (nx1), v (nx1), if
%
% alfa = 1 + v'*inv(A)*u ~= 0
%
% then A + uv' is nonsingular and
%
% inv(A + u*v') = inv(A) - (1/alfa)*inv(A)*u*v'*inv(A)

n = 6;
A = hilb(n);

% Question: How much can we reduce A(n,n) so that the new matrix is
% singular? I.e., what must tau be so that A - tau en*en' is singular
% where en is the last column of eye(n,n).

% Apply Sherman-Morrison with u = -tau*en and v = en. If A - tau*en*en' is
% singular then 0 = 1 + v'*inv(A)*u = 1 - tau*en'*inv(A)*en.

% Use the Cholesky factorization...
G = chol(A,'lower')
% Want tau = 1/(en'*inv(A)*en)
% = 1/(en'*inv(GG')en)
% = 1/((inv(G)*en)'*(inv(G)*en)
% Solve Gv = en
v = G\[zeros(n-1,1);1];
tau = 1/(v'*v)