Wednesday, 23 April 2014

Program to solve simultaneous equation using by Gauss Seidal Method

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int n,i,j,k,maxit;
float a[10][10],e,big,sum,temp,x[10],relerr,prod=1,prod1=1;
//clrscr();
printf("enter the allowable error :");
scanf("%f",&e);
printf("\nenter the maximum number of iteration :");
scanf("%d",&maxit);
printf("\nenter the number of equations :");
scanf("%d",&n);
printf("\nenter the co-efficients of equations :\n");
for(i=1;i<=n;i++)
 for(j=1;j<=(n+1);j++)
  scanf("%f",&a[i][j]);
printf("\nentered co-efficient matrix is\n");
for(i=1;i<=n;i++)
{
 for(j=1;j<=(n+1);j++)
  printf("%.2f\t",a[i][j]);
 printf("\n");
}

for(i=1,j=n;i<=n;i++,j--)
{
  prod=prod*a[i][i];
  prod1=prod1*a[i][j];
}
if(fabs(prod) < fabs(prod1))
{
  printf("diagonal dominancy");
  getch();
  return(0);
}
else
for(i=1;i<+n;i++)
  x[i]=0;
//printline();
for(i=1;i<=n;i++)
  printf("X%d\t",i);
printf("\n");
//printline();
for(k=1;k<=maxit;k++)
{
  big=0;
  for(i=1;i<=n;i++)
  {
    sum=0;
    for(j=1;j<=n;j++)
    {
      if(i!=j)
                sum=sum+a[i][j]*x[j];
    }//end for j
    temp=(a[i][n+1]-sum)/a[i][i];
    relerr=fabs((x[i]-temp)/temp);
    if(relerr>big)
      big=relerr;
    x[i]=temp;
    printf("%.2f\t",x[i]);
  }//end for i

  printf("\n");
  if(big<=e)
  {
    printf("\nconverges to a solution in %dth iteration",k);
    printf("\nsolution vector is:\n");
    for(i=1;i<=n;i++)
      printf("x[%d]=%.2f\n",i,x[i]);
    getch();
    return(0);
  }

    }//end for k
  printf("dose not converge in %d iterations",maxit);
  getch();

}

No comments:
Write comments

Featured post

List of Universities in Karnataka offering M.Sc Computer Science

The post-graduate programme in Computer Science (M.Sc Computer Science) contains two academic years duration and having a four semesters....

Popular Posts

Copyright @ 2011-2016