0 -1 0 -1 4 -1 0 0 -1 0 -1 4]; b=[0 5 -2 5 -2 6]';x0=[0 0 0 0 0 0]';
(1) JACOBI迭代;
function [x,j]=JACOBI(A,b,x0) n=length(b); l=zeros(n,n); u=zeros(n,n); x=x0; for i=1:n; for j=1:n; if i>j
l(i,j)=-A(i,j); elseif i==j
d(i,j)=A(i,j); else
u(i,j)=-A(i,j); end end end
disp('增广矩阵B') B=[A,b]
B=inv(d)*(l+u); f=inv(d)*b; for j=1:1000 x1=B*x+f;
x=x1;x1=B*x+f; if abs(x1-x)<0.0001 break; elseif j>1000
disp('已达到迭代设置1000次或者迭代不收敛'); end end
(2)GAUSS-SEIDEL迭代;
function [x,j]=Gauss(A,b,x0) n=length(b); l=zeros(n,n); u=zeros(n,n); x=x0; for i=1:n; for j=1:n; if i>j
l(i,j)=-A(i,j); elseif i==j