top of page

Solution of linear equation in n variables.

  • Feb 2, 2017
  • 3 min read

We are going to solve the system of linear equations using inverse matrix method, the mathematics behind the concept is explained below :

Any system of equations can be written in the form of matrix:

Where A is the coefficient matrix and B is the constant matrix, size of matrix A is K*K and size of matrix B is K*1 where K is the number of variables in the system of linear equations and X is the variable matrix.

Let's call it as equation 1.

For developing the solution, we must be familiar of some basic properties like:

(i) For a non-singular matrix A, there exist a unique matrix which on matrix multiplication with A gives the identity matrix, such matrix is called an Inverse matrix.

where A(-1) is the inverse matrix of A.

(ii) Any matrix multiplied with the identity matrix gives the same matrix.

where n * n is the size of matrix A.

We will work out some examples and see how things work:

Example : Linear equation in 2 variables

Now we can write the code for solving the above problem

Note : I am using recursion to calculate the inverse you can read about how to read how to write the program for the inverse and then how to multiply two matrices, I am attaching the link.

Also I have rounded the decimal digits to 2, however if you want more accurate value you can omit ".2" from "%.2f" , used in the end while printing the value.

Now all you need is to use the above two concept and get the matrix A(-1) * B and you will get the desired result:

(i) Code for determinant :

(ii) Code for co-factor:

(iii) Code for adjoint, inverse and multiplication:

That's all folks and if you want to get better understanding about recursion and how determinant and inverse are calculated using recursion do visit the links provided above,

Thanks for reading have a nice day!

Full C Implementation/Code :

#include<stdio.h> #include<math.h>

float determinent(float matrix[25][25], float size) { int c; float det=0,s=1; float b[25][25]; int i,j; int m,n; if(size == 1) { return (matrix[0][0]); }

else { det=0;

for(c=0; c<size; c++) { m=0; n=0; for(i=0; i<size; i++) { for(j=0; j<size; j++) { b[i][j] = 0; if(i!=0 && j!=c) { b[m][n] = matrix[i][j]; if(n<(size-2)) { n++; } else { n=0; m++; } } } }

det = det + s*(matrix[0][c]*determinent(b,size-1)); s = -1*s; } } return (det);

}

int main() { int k; printf("Enter the number of variables "); scanf("%d",&k);

int i,j; float matrix[25][25]; for(i=0; i<k; i++) { for(j=0; j<k; j++) { printf("Coefficient no. %d of eq%d ",j+1,i+1); scanf("%f", &matrix[i][j]); } } int a1; float matrixc[k][1]; for(a1=0; a1<k; a1++) { printf("Enter the constant c%d ",a1+1); scanf("%f",&matrixc[a1][0]); }

float result = determinent(matrix,k);

float cofactor[25][25];

if(result == 0) printf("\nSolution does not exist\n");

else { int c,d,p,q; int m,n; int size = k; float b[25][25]; for(c=0; c<size; c++) { for(d=0; d<size; d++) { m=0; n=0; for(p=0; p<size; p++) { for(q=0; q<size; q++) { if(p!=c && q!=d) { b[m][n] = matrix[p][q]; if(n<(size-2)) { n++; } else { n=0; m++; } } cofactor[c][d] = pow(-1,c+d)*determinent(b,k-1); } } } } float Adjoint[25][25]; int s,t; for(s=0; s<k; s++) { for(t=0; t<k; t++) { Adjoint[s][t] = cofactor[t][s]; } }

float Inverse[25][25]; int l,z; for(l=0; l<k; l++) { for(z=0; z<k; z++) { Inverse[l][z] = (Adjoint[l][z])/result; } }

float resultantMatrix[k][1]; int c1,d1; for(c1=0; c1<k; c1++) { for(d1=0; d1<1; d1++) { int p; float temp=0; for(p=0; p<k; p++) { temp = temp + Inverse[c1][p]*matrixc[p][d1]; } resultantMatrix[c1][d1] = temp;

} } int x,y; printf("\nThe solution is ("); for(x=0; x<k; x++) { if(x==k-1) printf("%.2f",resultantMatrix[x][0]); else printf("%.2f, ",resultantMatrix[x][0]); } printf(")\n");

}

return 0;

}

Sample INPUT/OUTPUT :

Linear equation in 2 variables :

Linear equation in 3 variables :

Linear equation in 4 variables :

Linear equation in 5 variables :

 
 
 

Comments


Tel. +917010984129  I  Whatsapp: +919003740631  I  info@mysite.com

© 2016-2017 

  • Facebook App Icon
  • Twitter App Icon
  • Google+ App Icon
  • Instagram App Icon
  • LinkedIn App Icon
bottom of page