%{ Jacobi, using full 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); % Sparse to full FA = full(SA); 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 on all elements of "FA", including zeroes x = (b-(tril(FA,-1)+triu(FA,1))*x)./diag(FA); res = b-FA*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