Gauss-Seidel iterative method with no relaxation factor

16 次查看(过去 30 天)
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 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by