function ShowBandLU()
% Displays the the structure of L and U in PA = LU when
% A is a banded matrix.
close all
n = 20;

p = 3; % Lower bandwidth
q = 2; % Upper bandwidth
A = randBand(n,p,q);
[L,U,P] =lu(A);
subplot(1,3,1), spy(A), title(sprintf('A p = %1d q = %1d',p,q),'fontsize',16)
subplot(1,3,2), spy(L), title('L','fontsize',16)
subplot(1,3,3), spy(U), title('U','fontsize',16)
shg

function A = randBand(n,p,q)
% Generates a random nxn matrix that has lower bandwidth p and upper
% bandwidth q.
A = tril(triu(randn(n,n),-p),q);