THIS IS THE CODE TO CREATE K,T,B,C AS SPARSE MATRICES

Then Matlab will not operate on all the zeros!

function K=KTBC(type,n,sparse)
% KTBC Create finite difference model matrix.
% K=KTBC(TYPE,N,SPARSE) creates model matrix TYPE of size N-by-N.
% TYPE is one of the characters 'K', 'T', 'B', or 'C'.
% The command K = KTBC('K', 100, 1) gives a sparse representation
% K=KTBC uses the defaults TYPE='K', N=10, and SPARSE=false.
% Change the 3rd argument from 1 to 0 for dense representation!
% If no 3rd argument is given, the default is dense
% If no argument at all, KTBC will give 10 by 10 matrix K

if nargin<1, type='K'; end
if nargin<2, n=10; end

e=ones(n,1);
K=spdiags([-e,2*e,-e],-1:1,n,n);
switch type
  case 'K'
  case 'T'
   K(1,1)=1;
  case 'B'
   K(1,1)=1;
   K(n,n)=1;
  case 'C'
   K(1,n)=-1;
   K(n,1)=-1;
  otherwise
   error('Unknown matrix type.');
end

if nargin<3 | ~sparse
  K=full(K);
end

Back to 18.085 page