% Script MkDiagMat.m % making some banded matrices close all; n = 10; p = 2; % p is upper bandwidth, with p-1 diagonals above the main one q = 2; % q is lower bandwidth, with q-1 diagonals below the main one % using tril and triu: A = tril(rand(n),p-1); spy(A); title('A after tril command') A = triu(A,1-q); figure; spy(A); title('A after triu command') % really, this should be in one command A = triu(tril(rand(n),p-1),1-q); figure; spy(A) title('banded A in one command') A(1:n+1:end) = p+q-2; [L,U,P] = lu(A); figure; spy(L); title('L when A is diagonally dominant') figure; spy(U); title('U when A is diagonally dominant') P % was there row switching? A(1:n+1:end) = rand(n,1); [L,U,P] = lu(A); figure; spy(U); title('U when A is not diagonally dominant') figure; spy(L); title('L when A is not diagonally dominant') P % was there row switching?