Stats

Friday, 7 September 2012

gauss seidel method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.01

void main() {
  int i, j, k, n;
  float a[10][10], x[10];
  float sum, temp, error, big;
  printf("Enter the number of equations: ");
  scanf("%d", & n);
  printf("Enter the co-efficients of the equations: \n");
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n + 1; j++) {
      printf("a[%d][%d]= ", i, j);
      scanf("%f", & a[i][j]);
    }
  }
  for (i = 1; i <= n; i++) {
    x[i] = 0;
  }
  do {
    big = 0;
    for (i = 1; i <= n; i++) {
      sum = 0;
      for (j = 1; j <= n; j++) {
        if (j != i) {
          sum = sum + a[i][j] * x[j];
        }
      }
      temp = (a[i][n + 1] - sum) / a[i][i];
      error = fabs(x[i] - temp);
      if (error > big) {
        big = error;
      }
      x[i] = temp;
      printf("\nx[%d] =%f", i, x[i]);
    }
    printf("\n");
  }
  while (big >= e);
  printf("\n\nconverges to solution");
  for (i = 1; i <= n; i++) {
    printf("\nx[%d]=%f", i, x[i]);
  }
  getch();
}

Enter the number of equations: 3
Enter the co-efficients of the equations:
a[1][1]= 2
a[1][2]= 1
a[1][3]= 1
a[1][4]= 5
a[2][1]= 3
a[2][2]= 5
a[2][3]= 2
a[2][4]= 15
a[3][1]= 2
a[3][2]= 1
a[3][3]= 4
a[3][4]= 8

x[1] =2.500000
x[2] =1.500000
x[3] =0.375000

x[1] =1.562500
x[2] =1.912500
x[3] =0.740625

x[1] =1.173437
x[2] =1.999688
x[3] =0.913359

x[1] =1.043477
x[2] =2.008570
x[3] =0.976119

x[1] =1.007655
x[2] =2.004959
x[3] =0.994933

x[1] =1.000054
x[2] =2.001995
x[3] =0.999474


converges to solution
x[1]=1.000054
x[2]=2.001995
x[3]=0.999474

13 comments:

  1. Hello friend, thank you very much for sharing your code.
    I wanted to ask you a question, which means when you define "# define and 0.01"?
    Tea appreciate your nswer, we are in contact, greetings.

    ReplyDelete
  2. can you please convert this coding in c++ also as while doing so its rhs values are not able to be assigned properly and its showing abnormal termination

    ReplyDelete
  3. thanks....very much....!!!!!!!

    ReplyDelete
  4. thanks....very much....!!!!!!!

    ReplyDelete
  5. how can you print the percent's values of errors?

    ReplyDelete
  6. Run to ho hi nhi raha program sir

    ReplyDelete