Making Matrices

n=5;
A=Table[0,{n},{n}];
Do[A[[i,i]]=-2,{i,n}];
Do[A[[i+1,i]]=1; A[[i,i+1]]=1,{i,n-1}];
A[[-1,1]]=1;
A[[1,-1]]=1;
MatrixForm[A]

Solving Equations, NullSpace, and Det

Inconsistent! No Solutions!

Det[A]
NullSpace[A]
b1=Table[i,{i,1,n}];
b2 = Table[N[i],{i,1,n}];
LinearSolve[A,b1]
LinearSolve[A,b2]

Consistent!  Lots of Solutions!

b3=b2;
b3[[-1]]=-Sum[b2[[i]],{i,1,n-1}];
b4=b1;
b4[[-1]]=-Sum[b1[[i]],{i,1,n-1}];
LinearSolve[A,b3]
LinearSolve[A,b4]

NullSpace[A]

Making Sparse Matrices

n=3;
m=3;
A=SparseArray[
Join[
Table[ 
{i,i}->-4,
{i, m*n}],
Table[{i,i+1}->1,{i,m*n-1}],
Table[{i+1,i}->1,{i,m*n-1}],
Table[{i+m,i}->1,{i,m*(n-1)}],
Table[{i,i+m}->1,{i,m*(n-1)}]
]
]
MatrixPlot[A]