GSHD1234
GSHD1234

Reputation: 1

Gauss-seidel method algorithm code giving inf numbers in solution

I'm not sure why I'm getting inf values when plugging in a 3 equation answer, another set of eyes on the equation loop would be useful, when I use a single equation the number of iterations is correct but when it's more than that the numbers multiply to inf.

Inputs for the code I used

Enter the Total Number of Equations:    3
Enter Allowed Error:    0.5

Enter the Co-Efficients
Matrix[1][1] = 1
Matrix[1][2] = 2
Matrix[1][3] = 3
Matrix[1][4] = 4
Matrix[2][1] = 2
Matrix[2][2] = 3
Matrix[2][3] = 4
Matrix[2][4] = 5
Matrix[3][1] = 3
Matrix[3][2] = 4
Matrix[3][3] = 5
Matrix[3][4] = 6
#include<stdio.h>
#include<math.h>
 
int main()
{
      int count, t, limit;
      float temp, error, a, sum = 0; 
      float matrix[10][10], y[10], allowed_error;
      printf("\nEnter the Total Number of Equations:\t");
      scanf("%d", &limit);
      printf("Enter Allowed Error:\t");
      scanf("%f", &allowed_error);
      printf("\nEnter the Co-Efficients\n");
      for(count = 1; count <= limit; count++)
      {
            for(t = 1; t <= limit + 1; t++)
            {
                  printf("Matrix[%d][%d] = ", count, t);
                  scanf("%f", &matrix[count][t]);
            }
      }
      for(count = 1; count <= limit; count++)
      {
            y[count] = 0;
      }
      do
      {
            a = 0;
            for(count = 1; count <= limit; count++)
            {
                  sum = 0;
                  for(t = 1; t <= limit; t++)
                  {
                        if(t != count)
                        {
                              sum = sum + matrix[count][t] * y[t];
                        }
                  }
                  temp = (matrix[count][limit + 1] - sum) / matrix[count][count];
                  error = fabs(y[count] - temp);
                  if(error > a)
                  {
                        a = error;
                  }
                  y[count] = temp;
                  printf("\nY[%d]=\t%f", count, y[count]);
            }
            printf("\n");
      }
      while(a >= allowed_error);
      printf("\n\nSolution\n\n");
      for(count = 1; count <= limit; count++)
      {
            printf("\nY[%d]:\t%f", count, y[count]);
      }
      return 0;
}

Upvotes: 0

Views: 123

Answers (1)

Aziz
Aziz

Reputation: 20705

Your code is correct. The issue is that Gauss-Seidel method does not always converge. The convergence criteria is that the matrix A must be either:

  • symmetric positive-definite
  • strictly or irreducibly diagonally dominant

The input matrix you used is neither symmetric, nor diagonally-dominant. Hence, the method fails to converge to a solution.

Upvotes: 2

Related Questions