``Gauss-Seidel迭代法-例3.8
clc;clear;format long
A=[5,-1,-1,-1;-1,10,-1,-1;-1,-1,5,-1;-1,-1,-1,10];
b=[-4;12;8;34];
x=[1,2,3,4]’;
D = diag(diag(A));
B = -(D+tril(A,-1))\triu(A,1);
f= (D+tril(A,-1))\b;
x0 = zeros(length(b),1);
x1 =Bx0+ f;
n= 1;
while norm(x-x1,2)>10^(-4)
x0=x1;
x1= Bx0+f;
n=n+1;
end
n,x1
运行结果: