Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Gauss-siedel method to solve system of equations (http://www.go4expert.com/articles/gauss-siedel-method-solve-equations-t17554/)

Nadr 16May2009 10:47

Gauss-siedel method to solve system of equations
 

Introduction



Gauss-siedel method to solve system of equations :undecided

The code



Code: Cpp

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define N 100
#define ACC 0.000001

/************************************************************/
/***** Gauss-siedel method to solve system of equations *****/
/************************************************************/

void main()
{
    float coeff[N][N+1]={{4,2,1,11},{3,5,3,22},{2,3,4,20},};
    /* Co-efficient inputing variables */
    float x[N]={0,0,0}/* values of the variables i.e. x's */
    int n=3;            /* Number of equations */
    int i,j;            /*  Loop variables */
    float acc;      /* Accuracy determining variable */
    char ch;            /* choice inputing variable */
    do
    {
        printf("\nEnter the number of variables\n");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            for(j=0;j<n+1;j++)
            {
                printf("\nEnter %d row %d col element\n",i+1,j+1);
                scanf("%f",&arr[i][j]);
            }
        }
        do
        {
            acc=coeff[0][n];
            for(i=0;i<n;i++)
            {
                x[i]=coeff[i][n];
                for(j=0;j<n;j++)
                {
                    if(i==j)
                        continue;
                    x[i]=x[i]-coeff[i][j]*x[j];
                }
                x[i]=x[i]/coeff[i][i];
            }
            for(i=0;i<n;i++)
                acc=acc-coeff[0][i]*x[i];
        }while(fabs(acc)>=ACC);
        for(i=0;i<n;i++)
            printf("%g\t",x[i]);
        printf("\nDo you wish to continue[y/n]\n");
        fflush(stdin);
        scanf("%c",&ch);
    }while(ch=='Y' || ch=='Y');
    getch();
}


SaswatPadhi 17May2009 21:10

Re: Gauss-siedel method to solve system of equations
 
Hi Nadr,

You forgot to remove the background, it contains the default text :p

But anyway, the article was very useful, thanx for that. :)

shabbir 18May2009 07:39

Re: Gauss-siedel method to solve system of equations
 
Done that for him. Thx for pointing it out

shabbir 3Jun2009 09:39

Re: Gauss-siedel method to solve system of equations
 
Nomination this Article for Article of the month - May 2009


All times are GMT +5.5. The time now is 09:53.