Gauss-Seidel method with Successive Over Relaxation

61 views (last 30 days)
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);
Anybody can tell me the issue
Thanks in advance!

Answers (1)

William Rose
William Rose on 23 Mar 2021
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.

Categories

Find more on Partial Differential Equation Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!