%{ Jacobi, using sparse coefficient matrix %} clear all; close all; clc; N = 150; % Sparse, tridiagonal matrix SA = spdiags([[-0.5*ones(N-1,1);0],2*ones(N,1),[0.;-0.5*ones(N-1,1)]],[-1,0,1],N,N); b = rand(N,1); % Tentative solution x = zeros(N,1); % Tollerance toll = 1e-12; % Max admissible number of iterations itMax = 1000; % Using explicit loops and working an all elements of A, including zeros hf = figure; nit = 0; while 1 nit = nit+1; % Works only on non-zero elements of "SA" x = (b-(tril(SA,-1)+triu(SA,1))*x)./diag(SA); res = b-SA*x; err = norm(res)/norm(b); figure(hf); h=loglog(nit,err,'ro'); hold on; if (nit==1) grid on; end if (erritMax) break; end end