Azzera filtri
Azzera filtri

Gauss-Seidel Method in Matlab

25 visualizzazioni (ultimi 30 giorni)
Zhukun Wang
Zhukun Wang il 16 Mar 2021
Risposto: Pavan Guntha il 1 Apr 2021
function [x,info,relres] = myGaussSeidel(A,b,MaxIter,TOL)
%Gaussseidel Method
n=2;
x0=zeros(n,1);
x=zeros(n,1);
k=1;
relres=0;
while k<=MaxIter
for i=1:n
x(i)=(b(i)-A(i,1:i-1)*x(1:i-1)-A(i,i-1:n)*x0(i+1:n))/A(i,i); % HOW TO DISTINGUISH UPDATED AND UNUPDATED TERMS OF X?
x0=x;
if norm(A*x-b)/norm(b)<=TOL
disp(x);
info=info+1;
end
end
info=-1;
k=k+1;
end
disp(x);
end
This code is my implementation for Gauss-Seidel Method, In line "x(i)=(b(i)-A(i,1:i-1)*x(1:i-1)-A(i,i-1:n)*x0(i+1:n))/A(i,i); " How to show the updated and unupdated values? I do not think my current code gives the correct answer.

Risposte (1)

Pavan Guntha
Pavan Guntha il 1 Apr 2021
Hi Zhukun,
You could use another vector y to store the values in the vector x and compare the updated x vector to the vector y in each iteration to determine the updated and unupdated values. This idea is illustrated below:
while k<=MaxIter
y = x; % y contains the values of vector x which were updated in the previous iteration.
for i=1:n
x(i)=(b(i)-A(i,1:i-1)*x(1:i-1)-A(i,i-1:n)*x0(i+1:n))/A(i,i);
x0=x;
if norm(A*x-b)/norm(b)<=TOL
disp(x);
info=info+1;
end
end
% At this point, x contains the values updated in the current iteration whereas y
% has the values updated in the previous iteration.
info=-1;
k=k+1;
end
You can also have a look at a similar case: Link
Hope this helps!

Categorie

Scopri di più su WLAN Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by