Gauss-Seidel method with Successive Over Relaxation
8 次查看(过去 30 天)
显示 更早的评论
function [X, k, res] = sor(A, b, x0, w, tol, maxIter)
[row, col] = size(A);
n = length(b);
x = x0;
k = 1;
res(k) = tol;
s = 0;
% Check the size of inputs
if (row ~= n) || (col ~= n) disp('Error'); return; end
% Successive over-relaxation method
while res(k) >= tol || k <= maxIter
for i = 1:n
for j = 1:i
if j < i
s = s + A(i,j) * x(j);
else
s = s + A(i,j) * x(j);
end
end
x(i) = (1 - w) * x(i) + w / A(i,i) * (b(i) - s);
end
% Check the norm of the residual
X(k, :) = x;
k = k + 1;
res(k) = norm(A * x - b);
x = x0;
end
end
A = [-4 3 0; 3 -4 -1; 0 -1 4];
b = [-24; 30; 24];
tol = 1e-8;
maxIter = 100;
x0 = [0; 0; 0];
w = 1.25;
[X, iter, res] = sor(A, x0, b, w, tol, maxIter);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/549383/image.jpeg)
Anybody can tell me the issue
Thanks in advance!
0 个评论
回答(1 个)
William Rose
2021-3-23
Your code includes
if j < i
s = s + A(i,j) * x(j);
else
s = s + A(i,j) * x(j);
end
which does the same thing on both sides of the if. Check that section.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Partial Differential Equation Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!