You are on page 1of 5

// NUMERICAL RESOLUTION OF LINEAR SYSTEMS USING GAUSS ELIMINATION METHOD

#include <stdio.h>

#include <stdlib.h>

// FUNCTION PROTOTYPES

void elimination(int p, double mat1[][p],double coef1[]);

void find_pivot(int t, int v, double mat2[][v],double coef2[]);

void back_substitution(int y, double mat3[][y], double coef3[]);

// BEGINNING OF THE MAIN PROGRAM

int main()

int i,j,n;

printf("NUMERICAL RESOLUTION OF LINEAR SYSTEMS\n\n");

printf("Enter the size of the matrix of the system\n\n");

scanf("%d",&n);

double mat[n][n]; // DECLARING MATRIX OF THE SYSTERM

double coef[n]; // DECLARING THE VECTOR ON THE RHS OF THE SYSTEM

printf("Enter the coefficients of the matrix\nEnter them in row form separating each coefficient
with a space\n\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

scanf("%lf",&mat[i][j]);

printf("Enter the coefficients of the vector on the RHS of the system in colon form\n\n");

for(i=0;i<n;i++)

scanf("%lf",&coef[i]);
}

elimination(n,mat,coef); // ELIMINATION STAGE

//CHECK FOR CONSISTENCY OF THE SYSTEM

for(i=0;i<n;i++)

if(mat[i][i]==0)

printf("No possible solution\n\n");

exit(1);

if(mat[n-1][n-1]==0 && coef[n-1]!=0)

printf("The system is inconsistent");

back_substitution(n,mat,coef);

return 0;

void elimination(int p, double mat1[][p],double coef1[])

double multi;

int r,q,w;

for(r=0;r<(p-1);r++)

if(mat1[r][r]==0)

find_pivot(r,p,mat1,coef1);

}
for(q=(r+1);q<p;q++)

multi=(mat1[q][r])/(mat1[r][r]);

coef1[q]=coef1[q]-(multi*coef1[r]);

for(w=0;w<p;w++)

mat1[q][w]=mat1[q][w]-(multi*mat1[r][w]);

printf("The reduced matrix is \n\n");

for(r=0;r<p;r++)

for(q=0;q<p;q++)

printf("\t%.2f",mat1[r][q]);

printf("\n");

printf("The modified RHS vector is\n\n");

for(r=0;r<p;r++)

printf("%.2f\n",coef1[r]);

void find_pivot(int t, int v, double mat2[][v],double coef2[])

double inter;
int e=0;

int s;

while(mat2[t][t]==0)

for(s=0;s<v;s++)

inter=mat2[t][t+s];

mat2[t][t+s]=mat2[t+e+1][t+s];

mat2[t+1+e][t+s]=inter;

inter=coef2[t];

coef2[t]=coef2[t+e+1];

coef2[t+e+1]=inter;

e++;

if(e==v)

printf("System can't be solved by Gauss elimination\n\n");

exit(0);

return;

void back_substitution(int y, double mat3[][y], double coef3[])

int b,c;

double x[y];

double sum;

for(b=0;b<y;b++)
{

x[b]=0;

for(b=y-1;b>=0;b--)

sum=coef3[b];

for(c=b+1;c<y;c++)

sum=sum -(mat3[b][c] * x[c]);

x[b]=sum/(mat3[b][b]);

printf("The solution is \n\n");

for(b=0;b<y;b++)

printf("x%d = %.2f\n",(b+1),x[b]);

You might also like