Gauss-Seidel method with Successive Over Relaxation
23 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
William Rose
le 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Partial Differential Equation Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!