GAUSS ELIMINATION
July 22, 2022
Solve The Given Linear Equations Systems By Using Gauss Elimination Method
Source code :-
#include <stdio.h>int main(){ int i, j, k, n; float a[20][20], x[20], r; printf("Enter the number of equations: "); scanf("%d", &n); // Input the matrix. printf("\nEnter co-efficient matrix [A] row wise:\n"); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("\na[%d][%d]= ", i + 1, j + 1); scanf("%f", &a[i][j]); } printf("\n"); } printf("\n\nEnter column matrix [B] row wise:\n"); for (i = 0, j = n; i < n; i++) { printf("\nb[%d]= ", i + 1); scanf("%f", &a[i][j]); } printf("\n"); // Upper triangular matrix conversion for (k = 0; k < n - 1; k++) { for (i = k + 1; i < n; i++) { r = a[i][k] / a[k][k]; for (j = 0; j <= n; j++) { a[i][j] = a[i][j] - r * a[k][j]; } } // Steps display start. [optional] printf("\nStep %d:-\n\n", k + 1); for (i = 0; i < n; i++) { printf("|"); for (j = 0; j < n; j++) { printf("%5.2f", a[i][j]); } printf(" |\t| %5.2f |\n", a[i][j]); } printf("\n"); // Steps display stop. } // Back tracking x[n - 1] = a[n - 1][n] / a[n - 1][n - 1]; for (j = n - 2; j >= 0; j--) { x[j] = a[j][n]; for (i = n - 1; i > j; i--) { x[j] = x[j] - a[j][i] * x[i]; } x[j] = x[j] / a[j][j]; } printf("\nThe solution is:\n"); for (i = 0; i < n; i++) { printf("x[%d]=%5.2f\n", i + 1, x[i]); } return 1;}
Input-Output :-
abhi@hp-15q-laptop:~$ gcc gauss_eli.cabhi@hp-15q-laptop:~$ ./a.out
Enter the number of equations: 3
Enter co-efficient matrix [A] row wise:
a[1][1]= 2
a[1][2]= -1
a[1][3]= 3
a[2][1]= 1
a[2][2]= 1
a[2][3]= 1
a[3][1]= 1
a[3][2]= -1
a[3][3]= 1
Enter column matrix [B] row wise:
b[1]= 9
b[2]= 6
b[3]= 2
Step 1:-
| 2.00-1.00 3.00 | | 9.00 || 0.00 1.50-0.50 | | 1.50 || 0.00-0.50-0.50 | | -2.50 |
Step 2:-
| 2.00-1.00 3.00 | | 9.00 || 0.00 1.50-0.50 | | 1.50 || 0.00 0.00-0.67 | | -2.00 |
The solution is:x[1]= 1.00x[2]= 2.00x[3]= 3.00