Gauss-Seidel Method in Matlab
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (1)
Pavan Guntha
le 1 Avr 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!
0 commentaires
Voir également
Catégories
En savoir plus sur WLAN 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!