Gauss-Seidel method with Successive Over Relaxation

9 ビュー (過去 30 日間)
Muhammad Asif
Muhammad Asif 2021 年 3 月 14 日
回答済み: William Rose 2021 年 3 月 23 日
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!

回答 (1 件)

William Rose
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.

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by