高斯赛德尔迭代方法的MATLAB实现如下:首先,将线性方程组Ax = b转化为适合迭代的形式。通过设置初始值并利用高斯赛德尔迭代公式,逐步更新解的值,直到满足设定的收敛条件。以下是实现的代码示例:
function x = gauss_seidel(A, b, x0, tol, maxIter)
n = length(b);
x = x0;
for k = 1:maxIter
x_old = x;
for i = 1:n
sum1 = A(i, 1:i-1) * x(1:i-1);
sum2 = A(i, i+1:n) * x_old(i+1:n);
x(i) = (b(i) - sum1 - sum2) / A(i, i);
end
if norm(x - x_old, inf) < tol>
使用示例:
A = [4, -1, 0, 0; -1, 4, -1, 0; 0, -1, 4, -1; 0, 0, -1, 3];
b = [15; 10; 10; 10];
x0 = zeros(size(b));
tol = 1e-5;
maxIter = 100;
x = gauss_seidel(A, b, x0, tol, maxIter);