I found the following algorithm for the Gauss Seidel method in this link: Gauss Seidel method – File Exchange – MATLAB Central (mathworks.com)
I decide to make minor changes to it, expecting it to work with a 4×4 matrix, but the algorithm doesn't output the answer correctly. Please can someone tell where the error is to make it run a 4×4 matrix?
Code:
A = [1 2 -1 1; -1 -2 -3 2; 2 1 -1 -5; 1 1 1 1]; % values for augmented matrix A
B = [5; 7; -1; 10]; % Coefficient values of matrix B.
X=[0;0;0;0]; % Initial approximation of solutions.
C=[0;0;0;0]; % A dummy matrix.
Err=[0;0;0;0]; % Error matrix.
P= [ A B ]; % constructing a new augmented matrix called P, using matrix A & B.
[ row col ] = size( P); % Calculating the size of augmented matrix, P
for i = 1:row % checking strictly diagonally dominant matrix
if 2*abs(A(i,i))<= sum(abs(A(i,:))) disp('Rearrange the equations to make diagonally dominant matrix!!!') return end end merr = 1;while merr > 0.0001 % Finding the final result.
for i=1:1:row C(i,1)=X(i,1); X(i,1)=(1/P(i,i))*(P(i,col)-sum(A(i,:)*X(:,1))+A(i,i)*X(i,1)); Err(i,1)= abs(C(i,1)-X(i,1)); C(i,1)=X(i,1); end merr=max(Err);enddisp(' The required solution is:')X(:,1)
Rearrange the equations to make diagonally dominant matrix!!!
The required solution is:
ans =
NaN
NaN
NaN
NaN
Best Answer