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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by