% % % %
THIS IS THE CODE FOR SOLVING SYSTEM OF LINEAR EQUATIONS WITH GAUSS-SEIDAL APPROACH. FOR THE CODE TO WORK, THE INPUT MATRIX 'a' SHOULD SATISFY FOLLOWING CONDITIONS:
% % %
1) THE MATRIX SHOULD BE DIAGONALLY DOMINANT. (THE ABSOLUTE VALUE OF DIAGONAL ELEMENT SHOULD BE GREATER THAN THE SUM OF ABSOLUTE VALUES OF REST OF THE ELEMENTS OF THAT ROW)
%
2) MATRIX A SHOUDL BE POSITIVE DEFNITE
clc clear %-------------------------------------------------------------------------% INPUTS %a=input('Enter Matrix starting from 1st row to 1st column') % -------------------------INPUTS ----------------------------------------a = [3.5 0.5 1; 2 -7 -1; -2 5 1] n = length(a); b = [1;2;1]; stopcrit=0.00001; % STOPPING CRITERION divcrit=10000; % STOPPING IN CASE OF DIVERGENCE %-------------------------------------------------------------------------format long count = 1; maxerr=10;
% LONG FORMAT FOR GREATER PRECISION % FOR CALCULATING NUMBER OF ITERATIONS % JUST ANY VALUE FOR INITIATION
%--------VARIFYING CONVERGENCE CRITERION-1 (DIAGONALLY DOMINANT MATRIX A) fail=0; exception=1; for i=1:n crit(i)=0; for j=1:n if j~=i crit(i)=crit(i)+abs(a(i,j)); end end if abs(a(i,i)) stopcrit)&& (maxerr < 1000) count = 1 + count;
for i=1:n xpp = b(i); for j=1:n if j~=i xpp = xpp-a(i,j).*xp(j); end end xn(i)=xpp/a(i,i); err(i)=abs(xp(i)-xn(i)); xp(i)=xn(i); end maxerr = max(err); end count maxerr xn end