MATLAB: How to assemble the Matrix A for any number of nodes

Hey guys, are you able to help me out with this matrix? I need to assemble the A matrix for n number of nodes.

Best Answer

n = 15;
a = ones(n,1);
b = rem((1:n)',3)~=0;
c = [1;b(1:end-1)];
d = [2*ones(6,1);ones(n-6,1)];
A = full(spdiags ([a,b,-4*a,c,d], [-3,-1,0,1,3], n, n))