Explain Gauss Elimination Method with algorithm


GAUSS ELIMINATION METHOD
Algorithm:
1.       Read n
2.       for i=1 to n
3.       for j=1 to n+1
4.       Read aij
5.       next j
6.       next i
Reduce to upper triangular matrix:
7.       for k=1 to n-1
8.       for i=k+1 to n
9.       ratio=aik/akk
10.   for j=1 to n+1
11.   aij=aij-ratio*akj
12.   next j
13.   next i
14.   next k
Using Back Substitution:
15.   x0= an, n+1/ann
16.   for k= n-1 to 1
17.   xk= ak, n+1
18.   for j= k+1 to n
19.   ak= xk-akj*xj
20.   next j
21.   xk=xk/akk
22.   next k
Printing the Result:’
23.   for i=1 to n
24.   print xi
25.   next i
26.   END
Source code:
#include<stdio.h>
void main()
{
/*Reading Matrix*/
int i, j, k, n;
float a[10][10], x[10];
float ratio;
printf(“enter the order of matrix\n”);
scanf(“%d”, &n);
printf(“enter the elements of the matrix\n”);
for(i=1; i<=n; i++)
{
for(j=1; j<=n+1; j++)
{
scanf(“%f”, &a[i][j]);
}
}
/*Reducing to upper triangular matrix*/
for(k=1; k<=n-1; k++)
{
for(i=k+1; i<=n; i++)
{
ratio=a[i][k]/a[k][k];
for(j=1; j<=n+1; j++)
{
a[i][j]=a[i][j]-(ratio*a[k][j]);
}
}
}
/*Using backsubstitution method*/
x[n]=a[n][n+1]/a[n][n];
for(k=n-1; k>=1; k--)
{
a[k]=a[k][n+1];
for(j=k+1; j<=n; j++)
{
x[k]=x[k]-(a[k][j]*x[j]);
}
x[k]=x[k]/a[k][k];
}
/*Printing the Answer*/
for(i=1; i<=n; i++)
printf(“The answer is %f\n”, x[i]);
getch();
}

0 comments:

Feel free to contact the admin for any suggestions and help.