% 4-8-2008 % Matrix construction clear all; close all; % Size of matrix needed N=100; % Initialize matrix A1=zeros(N,N); % Loop over every index for i=1:N for j=1:N if (i==j) % Main diagonal A1(i,j) = -2; elseif (i==j-1) % Superdiagonal A1(i,j) = 1; elseif (i==j+1) % Subdiagonal A1(i,j) = 1; end end end % Initialize matrix A2=zeros(N,N); % Only loop over diagonal indices for i=1:N A2(i,i) = -2; % Every diagonal entry should be 2 if (i~=N) % Do not execute if we are at the last column A2(i,i+1) = 1; end if (i~=1) % Do not execture if we are at the first column A2(i,i-1) = 1; end end % Create matrix using matlab functions constructively % Look up the command diag for information on how this works A3 = diag(-2*ones(N,1)) + diag(ones(N-1,1),1) + diag(ones(N-1,1),-1);