/* The following C program implements Gauss-Seidel iterative method for
finding the solution of a set of linear simultaneous equations. The number of
equations and the coefficient matrix are read as input. The results are printed
properly. */
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define maximum 10
#define minvalue 0.0005
void main()
{
float augmentedmatrix[maximum][maximum] ; /* 2D array declared to store augmented co-efficient matrix */
float result, maxdiff, diff, pivotalrow, pivotalelement, t ;
float solutions[maximum] ; /* 1D array declared to store solutions of the simultaneous equations */
int i, j, k, noofequations;
/* declaring counter variables for loops */
/* declaring counter variables for loops */
clrscr();
printf("\n GAUSS-SEIDEL ITERATIVE METHOD\n\n");
printf("\n Enter the number of total equations to be provided as input : \n");
scanf("%d",&noofequations);
/* storing augmented co-efficient matrix as a matrix of dimension
(noofequations)x(noofequations+1) in 2D array */
printf("\n Enter the augmented co-efficient matrix : \n");
for(i=0; i<noofequations; i++)
for(j=0; j<=noofequations; j++)
scanf("%f",&augmentedmatrix[i][j]) ;
/* pivoting the matrix */
for(i=0; i<noofequations-1; i++)
{
pivotalelement=fabs(augmentedmatrix[i][i]);
pivotalrow=i;
for(j=pivotalrow+1; j<noofequations; j++)
if(pivotalelement<fabs(augmentedmatrix[j][i]))
{
pivotalelement = fabs(augmentedmatrix[j][i]);
pivotalrow=j;
}
for(j=0;j<=noofequations; j++)
{
t=augmentedmatrix[i][j];
augmentedmatrix[i][j]=augmentedmatrix[pivotalrow][j];
augmentedmatrix[pivotalrow][j]=t;
}
}
/* using Gauss-Seidel iterative method */
maxdiff=minvalue ;
k=0;
while(maxdiff>=minvalue)
{
++k;
maxdiff = 0;
/* rowwise scanning, substituting estimates of (nofequations-i) unknowns
to get Xi in the ith row */
for(i=0; i<noofequations; i++)
{
result=augmentedmatrix[i][noofequations] ;
/* columnwise scanning, to get the solution of ith unknown in ith row */
for(j=0; j<noofequations; j++)
if(i!=j)
result-=augmentedmatrix[i][j]*solutions[j];
result/=augmentedmatrix[i][i] ;
diff=solutions[i]-result;
if(fabs(diff)>maxdiff)
maxdiff = fabs(diff) ;
solutions[i]=result;
} /* end of outer for loop */
printf("\n\n %d", k) ; /* gives the no. of iterations performed */
for(j=0; j<noofequations; j++)
printf(" %f",solutions[j]) ; /* current estimated solutions will be printed */
printf(" %f",maxdiff);
} /* end of outermost while loop */
printf("\n\n\n");
/* printing solutions */
for(i=0; i<noofequations; i++)
printf(" X%d = %.3f \n", i, solutions[i]) ;
end: getch() ;
}
SAMPLE OUTPUT:
Solving the following set of linear simultaneous equations by Gauss-Seidel iteration:
3x0 + 2x1 + 6x2 = 17
15x0 + 6x1 + 7x2 = 24
2x0 - 4x1 - 2x2 = 4
ERROR!
ReplyDeleteThis is FAKE! 100% NOT working. Damn this -___-
ReplyDeletepon el correcto por fa!! :)
ReplyDeleteWorks 100% for me.
ReplyDelete