Please explain Gaussian Elimination Algorithm Efficiently.

1 Like

Hi, I have tried to explain Gauss Elimination within the code only ( best way I guess).Please let me know if you have any doubt.

The main aim of Gauss elimination is to convert Matrix into upper triangle matrix because once you do so the last variable say z will be nothing but the last element of the last row divided by zās coefficient.

We store this result in a matrix and use it for calculating (in bottom up fashion) other variables.(also known as Backward substitution)

int main()

//For three variables

{

```
int i,j,k,n;
float a[20][20],x[10],sum,c;
cout<< "enter the order of matrix\n";
cin>>n;
cout<<"enter the pivoted matrix\n";
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
cout<<"a["<<i<<"]["<<j<<"]"<<endl;
cin>>a[i][j];
}
}
**//converting into upper triangle matrix**
for(j=1;j<=n;j++) // eliminating x,y,z from each of the equation
{
for(i=1;i<=n;i++) //
{
if(i>j) // because we cant subtract the equation from itself,so we iterate from next row
{
// Now we will iterate over the whole equation eliminating one variable at a time
//decide the value of c that is the coefficient of the variable to eliminated
c=a[i][j]/a[j][j];
for(k=1;k<=n+1;k++)
{
a[i][k]=a[i][k]-c*a[j][k];
}
}
}
}
// BACKWARD SUBSTITUTION
x[n]=a[n][n+1]/a[n][n];//the solution of the nth element (z here)
// finding other elements
for(i=n-1;i>=1;i--)
{
sum=0.0;
for(j=i+1;j<=n;j++)
{
sum=sum+(x[j]*a[i][j]);
}
x[i]=(a[i][n+1]-sum)/a[i][i]; //just a simple mistake may happen its that sum will be minus here
}
cout<<"the solutions\n";
for(i=1;i<=n;i++)
{
cout<<x[i]<<endl;
}
return 0;
```

}