【发布时间】:2017-02-02 21:36:49
【问题描述】:
我正在尝试在 MATLAB 中实现 Gauss-Seidel 方法。但是我的代码中有两个主要错误,我无法修复它们:
我的代码在小矩阵上收敛得很好,但在大矩阵上却从不收敛。
代码进行了冗余迭代。如何防止重复迭代?
Gauss-Seidel Method on wikipedia.
N=5;
A=rand(N,N);
b=rand(N,1);
x = zeros(N,1);
sum = 0;
xold = x;
tic
for n_iter=1:1000
for i = 1:N
for j = 1:N
if (j ~= i)
sum = sum + (A(i,j)/A(i,i)) * xold(j);
else
continue;
end
end
x(i) = -sum + b(i)/A(i,i);
sum = 0;
end
if(abs(x(i)-xold(j))<0.001)
break;
end
xold = x;
end
gs_time=toc;
prompt1='Gauss-Seidel Method Time';
prompt2='x Matrix';
disp(prompt2);
disp(x);
disp(prompt1);
disp(gs_time);
【问题讨论】:
标签: matlab numerical-methods gauss