Gauss-Seidel iterative method with no relaxation factor

조회 수: 7 (최근 30일)
Dai Nguyen
Dai Nguyen 2020년 10월 27일
댓글: Dai Nguyen 2020년 11월 9일
Hi I want to create a Matlab code for Gauss-Seidel iterative method with no relaxation factor and use the solution using a termination tolerance=.01% for the relative approximate error. This is what I got so far. For some reason my stigma is zero and it won't able to calculate it
A= [60 -40 0; -40 60 -20; 0 -20 20]
b=[29.4; 39.2; 58.8]
x=[0 0 0 0]'
n=size(x,1);
normVal=Inf;
%%
% * _*Tolerence for method*_
tol=1e-5; itr=0;
%%
while normVal>tol
x_old=x;
for i=1:n
sigma=0;
for j=1:i-1
sigma=sigma+A(i,j)*x(j);
end
for j=i+1:n
sigma=sigma+A(i,j)*x_old(j);
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
itr=itr+1;
normVal=norm(x_old-x);
end
%%
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d iterations',x,itr);

채택된 답변

Sreeranj Jayadevan
Sreeranj Jayadevan 2020년 11월 9일
I have executed the given code in MATLAB and it is giving me the required output. It seems to me that you have accidentally initialized the solution vector "x" as a 4 by 1 array. Since only three equations are involved in the problem, "x" should be a 3 by 1 array i.e
x=[0 0 0]';

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by