function[x,n]=gauseidel(A,b,x0,eps,M)A=hilb(4)b=[1111]'x0=[0000]'M=100000eps=0.001ifnargin==3eps=1.0e-6;M=200;elseifnargin==4M=200;errorreturnreturn;endD=diag(diag(A));%求A的对角矩阵L=-tril(A,-1);%求A的下三角阵U=-triu(A,1);%求A的上三角阵G=(D-L)\U;f=(D-L)\b;x=G*x0+f;n=1;whilenorm(x-x0)=epsx0=x;x=G*x0+f;n=n+1;if(n=M)disp('Warning:迭代次数太多,可能不收敛!');return;endend