Gauss-Seidel method with Successive Over Relaxation

4 Ansichten (letzte 30 Tage)
Muhammad Asif
Muhammad Asif am 14 Mär. 2021
Beantwortet: William Rose am 23 Mär. 2021
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!

Antworten (1)

William Rose
William Rose am 23 Mär. 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.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by