Hi, I managed to fix the sparse algorithm, however it is slower than simple iterative method. Have I done something wrong with the sparse method? Can it be optimized further? Is there a way to substitute for for loop ?
wo=w;
I=eye(n);
e=ones(n,1);
T=spdiags([e -4*e e],[-1 0 1],n,n);
S=spdiags([e e],[-1 1],n,n);
A=(kron(I,T)+kron(S,I))*c1;
for tt=0:dt:200
wn=2*w+A*w-wo;
for i=1:n
wm(i,:)=w((i-1)*n+1:i*n); % Convert back to matrix
end
wo=w; % curent become old
w=wn; % new become current
end