# Linear Equation

There are two classes of methods for solving Linear Equations:

1. Direct Methods: Common characteristics of direct methods are that they transform the original equation into equivalent equations that can be solved more easily, means we get solve directly from an equation.
2. Iterative Method: Iterative or Indirect Methods, start with a guess of the solution and then repeatedly refine the solution until a certain convergence criterion is reached. Iterative methods are generally less efficient than direct methods because large number of operations required. Example- Jacobi’s Iteration Method, Gauss-Seidal Iteration Method.

Implementation in C-

```//Implementation of Jacobi's Method
void JacobisMethod(int n, double x[n], double b[n], double a[n][n]){
double Nx[n]; //modified form of variables
int rootFound=0; //flag

int i, j;
while(!rootFound){
for(i=0; i<n; i++){              //calculation
Nx[i]=b[i];

for(j=0; j<n; j++){
if(i!=j) Nx[i] = Nx[i]-a[i][j]*x[j];
}
Nx[i] = Nx[i] / a[i][i];
}

rootFound=1;                    //verification
for(i=0; i<n; i++){
if(!( (Nx[i]-x[i])/x[i] > -0.000001 && (Nx[i]-x[i])/x[i] < 0.000001 )){
rootFound=0;
break;
}
}

for(i=0; i<n; i++){             //evaluation
x[i]=Nx[i];
}
}

return ;
}

//Implementation of Gauss-Seidal Method
void GaussSeidalMethod(int n, double x[n], double b[n], double a[n][n]){
double Nx[n]; //modified form of variables
int rootFound=0; //flag

int i, j;
for(i=0; i<n; i++){                  //initialization
Nx[i]=x[i];
}

while(!rootFound){
for(i=0; i<n; i++){              //calculation
Nx[i]=b[i];

for(j=0; j<n; j++){
if(i!=j) Nx[i] = Nx[i]-a[i][j]*Nx[j];
}
Nx[i] = Nx[i] / a[i][i];
}

rootFound=1;                    //verification
for(i=0; i<n; i++){
if(!( (Nx[i]-x[i])/x[i] > -0.000001 && (Nx[i]-x[i])/x[i] < 0.000001 )){
rootFound=0;
break;
}
}

for(i=0; i<n; i++){             //evaluation
x[i]=Nx[i];
}
}

return ;
}

//Print array with comma separation
void print(int n, double x[n]){
int i;
for(i=0; i<n; i++){
printf("%lf, ", x[i]);
}
printf("\n\n");

return ;
}

int main(){
//equation initialization
int n=3;    //number of variables

double x[n];    //variables

double b[n],    //constants
a[n][n];    //arguments

//assign values
a[0][0]=8; a[0][1]=2; a[0][2]=-2; b[0]=8;    //8x₁+2x₂-2x₃+8=0
a[1][0]=1; a[1][1]=-8; a[1][2]=3; b[1]=-4;   //x₁-8x₂+3x₃-4=0
a[2][0]=2; a[2][1]=1; a[2][2]=9; b[2]=12;    //2x₁+x₂+9x₃+12=0

int i;

for(i=0; i<n; i++){                         //initialization
x[i]=0;
}
JacobisMethod(n, x, b, a);
print(n, x);

for(i=0; i<n; i++){                         //initialization
x[i]=0;
}
GaussSeidalMethod(n, x, b, a);
print(n, x);

return 0;
}```