Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

My code in c for gauss jacobi isn't working? why is it so?

+2 votes
asked Mar 4, 2019 by Harika (180 points)
#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

int a[5][5],b[5],n,i,j,flag=1,key=0;

char c;

float e,sum,xo[10],x1[10],s;

setbuf(stdout,NULL);

printf("This program illustrates Gauss Jacobi Method in C :\n");

printf("Enter the dimension of the coefficient matrix A:");

scanf("%d",&n);

printf("Enter the %d elements of the coefficient matrix A:",n*n);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

printf("Enter the elements of the constant matrix B:");

for(i=0;i<n;i++)

scanf("%d",&b[i]);

printf("The given system of linear equations is\n");

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

if((j==0 || j==1 ) && (a[i][j+1]>0))

c='+';

else if(j==2)

c='=';

printf("%dx%d\t%c",a[i][j],j+1,c);

}

printf("%d",b[i]);

printf("\n");

}

    for(i=0;i<n;i++)

    {

        for(j=0;j<n;j++)

        {

            if(i!=j)

               s=s+a[i][j];

        }

        if(fabs(a[i][i])<fabs(s))

        {

            flag=0;

            break;

        }

    }

if(flag==0)

{

printf("Error!The given system of linear equations is not diagonally dominant.");

exit(0);

}

else

{   

    printf("The given system of linear equations is diagonally dominant.a\n");

printf("Enter the initial approximate solution to the given system:");

for(i=0;i<n;i++)

{

scanf("%f\t",&xo[i]);

}

for(i=0;i<n;i++)

{

printf("x%d=%f\t",i+1,xo[i]);

}

printf("Enter the allowed error:");

scanf("%f",&e);

do{

for(i=0;i<n;i++)

{

sum=b[i];

for(j=0;j<n;j++)

{

if(j!=i)

sum=sum-a[i][j]*xo[j];

}

x1[i]=sum/a[i][i];

if(fabs(x1[i]-xo[i])<e)

{

      key=key+1;

      xo[i]=x1[i];

}

}

}while(key<n);

           

}

if(key==n)

   for(i=0;i<n;i++)

     printf("%f\t",xo[i]);

}

1 Answer

0 votes
answered 6 hours ago by crazy _4633 (200 points)
instead of using gauss jacobi use gauss siedel method to correct.

because ur code has multiple bugs i have an alternative way to solve ur code without siedel method.

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

int main() {
    int a[5][5], b[5], n, i, j, flag = 1;
    float e, sum, x[10], old_x, max_error, s;
    
    setbuf(stdout, NULL);
    printf("This program illustrates Gauss Seidel Method in C :\n");
    printf("Enter the dimension of the coefficient matrix A: ");
    scanf("%d", &n);
    
    printf("Enter the %d elements of the coefficient matrix A:\n", n*n);
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++) {
            scanf("%d", &a[i][j]);
        }
    }
        
    printf("Enter the elements of the constant matrix B: ");
    for(i=0; i<n; i++) {
        scanf("%d", &b[i]);
    }
        
    printf("\nThe given system of linear equations is:\n");
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++) {
            printf("%dx%d", a[i][j], j+1);
            if(j < n-1) printf(" + ");
            else printf(" = ");
        }
        printf("%d\n", b[i]);
    }
    
    // Diagonal Dominance check
    for(i=0; i<n; i++) {
        s = 0;
        for(j=0; j<n; j++) {
            if(i != j) s = s + fabs(a[i][j]);
        }
        if(fabs(a[i][i]) < s) {
            flag = 0;
            break;
        }
    }
    
    if(flag == 0) {
        printf("\nError! The given system of linear equations is not diagonally dominant.\n");
        exit(0);
    }
    
    printf("\nThe given system of linear equations is diagonally dominant.\n");
    printf("Enter the initial approximate solution to the given system:\n");
    for(i=0; i<n; i++) {
        printf("x%d: ", i+1);
        scanf("%f", &x[i]);
    }
    
    printf("Enter the allowed error: ");
    scanf("%f", &e);
    
    int iterations = 0;
    do {
        max_error = 0; // Tracks the largest change among all variables this iteration
        
        for(i=0; i<n; i++) {
            old_x = x[i]; // Store the previous value of this specific variable
            sum = b[i];
            
            for(j=0; j<n; j++) {
                if(j != i) {
                    // Gauss-Seidel change: x[j] always contains the newest calculated value
                    sum = sum - a[i][j] * x[j];
                }
            }
            x[i] = sum / a[i][i]; // Instantly update the array
            
            // Check if the change for this specific variable is the largest so far
            float current_error = fabs(x[i] - old_x);
            if(current_error > max_error) {
                max_error = current_error;
            }
        }
        
        iterations++;
        if(iterations > 1000) { // Safety guard against infinite loops
            printf("\nSolution did not converge within 1000 iterations.\n");
            exit(0);
        }
        
    } while(max_error > e); // Loop until the worst-case error falls below threshold
    
    printf("\nThe final solution after %d iterations is:\n", iterations);
    for(i=0; i<n; i++) {
        printf("x%d = %f\n", i+1, x[i]);
    }
    
    return 0;
}

This is gauss siedel method
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...