how to write c program to solve a system of linear equations
DESCRIPTION
In these slides, I will explain you how to write a C Program to solve a system of linear equations by matrix method. You will require some basic concepts of C programming, matrices and determinants. I hope you'll like it. Thanks.TRANSCRIPT
C-PROGRAM TO SOLVE L INEAR EQUATIONS
Prepared by:
Sendash Pangambam
Dept. of Computer Science
Banaras Hindu University
7/21/2014 PREPARED BY SENDASH PANGAMBAM 1
MATHEMATICAL CONCEPTS:
Given equations:
1. a11x+a12y+a13z=c1
2. a21x+a22y+a23z=c2
3. a31x+a32y+a33z=c3
Three equations in three variables x, y and z.
7/21/2014 PREPARED BY SENDASH PANGAMBAM 2
MATHEMATICAL CONCEPTS:
In matrix form:
a11 a12 a13 x c1
a21 a22 a23 y = c2
a31 a32 a33 z c3
=>AX = C
7/21/2014 PREPARED BY SENDASH PANGAMBAM 3
MATHEMATICAL CONCEPTS:
AX = C
= (A-1A)X = A-1C
=> IX = A-1C
=> X = A-1C
Thus, one can find the values of elements of X i.e. x, y
and z by comparing the two matrices X and A-1C.
7/21/2014 PREPARED BY SENDASH PANGAMBAM 4
BASIC STEPS:
Find the determinant of A.
• If det.A≠0, system of eqns. has a unique solution.
• If det.A=0, no solution. A is singular.
Find Inverse of A.
Solve by using matrix method.
7/21/2014 PREPARED BY SENDASH PANGAMBAM 5
NOW LETS GO TO
7/21/2014 PREPARED BY SENDASH PANGAMBAM 6
VARIABLES USED IN MY PROGRAM
Co-efficients of X,Y and Z in the three eqns. are stored in an array a[3][3] and constant
terms in the array c[3].
A[3][3] is first used to store the co-factors, and later is used to store the Adjoint of the
matrix a[3][3]
det is used to store the value of determinant of a[3][3].
X[3] is used to store the value of the three variables X,Y and Z.
In the swap() function, arguments are *a and *b, temp is a temporary variable used
while swapping.
7/21/2014 PREPARED BY SENDASH PANGAMBAM 7
PROGR AM WIT H E X PLANAT IONS(1 )
/*SOLVE A SYSTEM OF LINEAR EQUATION WITH 3 VARIABLES BY MATRIX METHOD*/
#include<stdio.h>
#include<conio.h> Header files are included here.
#include<stdlib.h>
/*FUNCTION TO SWAP*/
void swap(float *a,float *b)
{
float temp; Function to swap two numbers
temp=*a; To be used while calculating Adjoint/Transpose
*a=*b;
*b=temp;
}
7/21/2014 PREPARED BY SENDASH PANGAMBAM 8
PROGRAM WITH EXPLANATIONS(2)
void main() main() function starts here.
{
int i,j; Variable declaration.
float a[3][3],c[3],A[3][3],INV[3][3],det=0.0,X[3];
clrscr();
/*ENTER THE CO-EFFICIENTS AND CONSTANT TERMS*/
for(i=0;i<3;i++) Equation
{ taken as
printf("\nEnter the X,Y,Z co-efficients and the constant term of equation no. input
%d: ",i+1); here.
scanf("%f%f%f%f",&a[i][0],&a[i][1],&a[i][2],&c[i]);
}
clrscr();
7/21/2014 PREPARED BY SENDASH PANGAMBAM 9
PROGRAM WITH EXPLANATIONS(3)
/*PRINTING THE EQUATIONS*/
printf("\nThe equations are: \n"); Printing
for(i=0;i<3;i++) the eqns.
printf("%.2f X + %.2f Y + %.2f Z = %.2f \n",a[i][0],a[i][1],a[i][2],c[i]); for surety.
/*FINDING CO-FACTORS*/
for(i=0;i<3;i++) Finding cofactors
{ to use while calculating
for(j=0;j<3;j++) determinant, adjoint,
A[i][j]=(a[(i+1)%3][(j+1)%3]*a[(i+2)%3][(j+2)%3]) - inverse, etc.
(a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]);
}
7/21/2014 PREPARED BY SENDASH PANGAMBAM 10
PROGRAM WITH EXPLANATIONS(4)
/*FINDING DETERMINANT OF THE MATRIX*/
for(i=0;i<3;i++) Finding determinant using 1stcolumn
det+=a[i][0]*A[i][0];
printf("\n\n\n");
if(det==0)
{
printf("SINGULAR MATRIX !\nCANNOT FIND THE Check whether the
SOLUTION\n\n\tPress any key to exit."); matrix is singular or not.
getch();
exit(1);
}
7/21/2014 PREPARED BY SENDASH PANGAMBAM 11
PROGRAM WITH EXPLANATIONS(5)
/*FINDING ADJOINT MATRIX*/
for(i=0;i<2;i++) Calculation of adjoint.
{ Adjoint=Transpose of the matrix formed
for(j=i+1;j<3;j++) by co-factors of corresponding element.
swap(&A[i][j], &A[j][i]);
}
/*FINDING INVERSE*/
for(i=0;i<3;i++) Calculation of inverse.
{
for(j=0;j<3;j++) Inverse of A=Adjoint of A/det. A
INV[i][j]=A[i][j]/det;
}
7/21/2014 PREPARED BY SENDASH PANGAMBAM 12
PROGRAM WITH EXPLANATIONS(6)
/*SOLVING MATRIX EQUATION*/
for(i=0;i<3;i++)
{
X[i]=0.0; Finding the value of X = A-1C
for(j=0;j<3;j++) And find the values of elements of X
{ by comparing corresponding elements
X[i]+=INV[i][j]*c[j]; of X and A-1C
}
}
7/21/2014 PREPARED BY SENDASH PANGAMBAM 13
PROGRAM WITH EXPLANATIONS(7)
/*PRINTING THE SOLUTIONS*/
printf("Solution of the equation:
\n\tX=%.2f\n\tY=%.2f\n\tZ=%.2f",X[0],X[1],X[2]); Printing the solutions
getch();
} End of main()
7/21/2014 PREPARED BY SENDASH PANGAMBAM 14
RUN THE PROGRAM
7/21/2014 PREPARED BY SENDASH PANGAMBAM 15
THUS THE PROBLEM IS SOLVED
7/21/2014 PREPARED BY SENDASH PANGAMBAM 16