[MATLAB代码]超松弛迭代算法求解齐次线性方程组
%定义sor迭代函数
function [x, n] = sor(A, b, x0, w, eps)
%计算迭代矩阵
D = diag(diag(A));
L = -tril(A,-1);
U = -triu(A,1);
BS = (D-w*L)\((1-w)*D+w*U); %inv(M)*A=M\A
f = w*(D-w*L)\b;
%判断收敛性
if w>=2 || w<=0
disp('sor迭代不收敛');
return
else
n = 1;
x = BS*x0 + f;
while norm(x-x0,inf)>=eps
x0 = x;
x = BS*x0 + f;
n = n+1;
end
end
A=[4 3 0; 3 4 -1; 0 -1 4];
b=[24; 30; -24];
x0=[0; 0; 0];
eps=1.0e-6;
[x, n]=sor(A,b,x0,w,eps);