Gauss-Seidel Method in Matlab

조회 수: 83 (최근 30일)
Zhukun Wang
Zhukun Wang 2021년 3월 16일
답변: Pavan Guntha 2021년 4월 1일
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.

답변 (1개)

Pavan Guntha
Pavan Guntha 2021년 4월 1일
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!

카테고리

Help CenterFile Exchange에서 WLAN Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by