1. Definition
In this part, we show how to solve the two and three equations
systems with two and three variables. For the first kind, we use
determinant and for the second kind, we also use determinant, plus,
we invert a matrix (adjunct of cofactors_matrix divided by the related
determinant) and use vectors components as : v A = w, then v = A- W.
2. Example in C language:
2.1. Two equations Systems with two variables
#include<stdio.h>
#include<math.h>
int main()
{
printf ("n A linear system with two equations and two variables is written as: n");
printf ("t a x + b = m n");
printf ("t c x + d = n n");
float val_a, val_b, val_m;
float val_c, val_d, val_n;
printf ("n Enter values for the coefficients: a, b, m : --> :n");
scanf("%f %f %f", &val_a, &val_b, &val_m);
printf ("n And for the coefficients: c, d, n : --> :n");
scanf("%f %f %f", &val_c, &val_d, &val_n);
float result1;
float result2;
float delta = val_c*val_b - val_a*val_d;
float beta_x = val_n*val_b - val_m*val_d;
float beta_y = val_c*val_m - val_a*val_n;
if (((delta == 0) && (beta_x == 0)) || ((delta == 0) && (beta_y == 0))) {
printf ("n The system is indeterminate ..n");
}
else if (((delta == 0) && (beta_x != 0)) || ((delta == 0) && (beta_y != 0))) {
printf ("n The system has no solutions ..n");
}
else
{
result1 = beta_x/delta;
result1 = round(result1*100)/100 ;
result2= beta_y/delta ;
result2 = round(result2*100)/100 ;
printf ("n The solutions are :n");
printf (" x = %3.3fn", result1);
printf (" y = %3.3fn", result2);
}
return 0;
}
2.2. Three equations Systems with three variables>
#include <stdio.h>
#include<math.h>
int main()
{
float A[3][3];
float B[3][3];
float C[3][3];
float X[3][3];
float D[3];
printf("n Enter the 9 elements of the matrix A(3x3):n");
int h,g;
for(h=1; h<4; h++)
{
printf("n");
for(g=1; g<4; g++)
{
printf(" A[%d][%d]= ",h,g);
scanf("%f", &A[h][g]);
}
}
int p;
printf("n Enter the elements of the vector D --> : n");
for(p=1; p<4; p++)
{
printf("n");
printf(" D[%d] = ",p);
scanf("%f", &D[p]);
}
printf("n n");
// Calculating the determinant of the matrix A:
float m = 0;
m = A[1][1]*A[2][2]*A[3][3] + A[1][2]*A[2][3]*A[3][1] +
A[1][3]*A[2][1]*A[3][2] - A[1][3]*A[2][2]*A[3][1] -
A[1][1]*A[2][3]*A[3][2] - A[1][2]*A[2][1]*A[3][3];
// Displaying the entered matrix :
int v,w;
printf("n The matrix you entered has the following elements:n");
for(v=1; v <4; v++)
{
printf("n");
for(w=1; w <4;w++)
{
printf(" A[%d][%d]= %.2f ",v,w,A[v][w]);
}
}
printf("n n");
// Displaying the entered vector:
int z;
printf("n The vector you entered has the following elements:n");
for(z=1; z<4; z++)
{
printf("n");
printf(" D[%d]= %.2f ",z,D[z]);
}
//------------
printf("n");
printf("n The matrix is written as : n");
printf("n");
for(w=1; w <4;w++)
{
printf(" %.2f ", A[1][w]);
}
printf("n");
for(w=1; w <4;w++)
{
printf(" %.2f ", A[2][w]);
}
printf("n");
for(w=1; w <4;w++)
{
printf(" %.2f ", A[3][w]);
}
//-------------------------------
printf("n n");
printf(" The determinant of the matrix A is %.2f .n",m);
// The matrix of cofactors has the following elements:
B[1][1]=A[2][2]*A[3][3]-(A[3][2]*A[2][3]);
B[1][2]=(-1)*(A[2][1]*A[3][3]-(A[3][1]*A[2][3]));
B[1][3]=A[2][1]*A[3][2]-(A[3][1]*A[2][2]);
B[2][1]=(-1)*(A[1][2]*A[3][3]-A[3][2]*A[1][3]);
B[2][2]=A[1][1]*A[3][3]-A[3][1]*A[1][3];
B[2][3]=(-1)*(A[1][1]*A[3][2]-A[3][1]*A[1][2]);
B[3][1]=A[1][2]*A[2][3]-A[2][2]*A[1][3];
B[3][2]=(-1)*(A[1][1]*A[2][3]-A[2][1]*A[1][3]);
B[3][3]=A[1][1]*A[2][2]-A[2][1]*A[1][2];
// ---------------------------
// Transpose this matrix. The transpose of B is C
/*
# write the rows of C as the columns of AT
# write the columns of A as the rows of AT
*/
//Adjugate of matrix: A (B) has the following elements:
/*The adjugate or adjunct of a matrix is is the transpose of the matrix of cofactors.
To transpose amatrix B , write its rows as the columns of C, and its columns
as the rows of C.
*/
// The transpose of B is C:
printf("n The adjunct matrix C of A has the following elements: n");
for(v=1; v<4; v++)
{
printf("n");
for(w =1;w <4;w++)
{
C[v][w]=B[w][v];
printf("C[%d][%d]= %.2f n",v,w,C[v][w]);
}
}
//-------------------------------
// Inverse matrix
/*The inverse matrix X of a matrix A is equal to its adjugate divided by its
determinant. The adjugate or adjunct of a matrix is is the transpose of the
matrix of cofactors
*/
float inv_det = 1.0/m;
if (inv_det ==0)
{
printf("n The determinant is null, the related inverse matrix could not exist .. ");
}
else
{
for(v=1;v<4;v++)
{
for(w=1;w<4;w++)
{
X[v][w]=C[v][w]*inv_det;
}
}
}
printf("n The inverse matrix of the matrix you entered has the folowing elements:n");
for(v=1; v<4; v++)
{ printf("n");
for(w=1; w<4; w++)
{
printf(" X[%d][%d]= %.2f ",v,w,X[v][w]);
}
}
printf("nn");
// ---------------
/*
If Ax = y, we can write:
x = (inverse A).y
xi = sum (X[i][p] * D[p]) over p
as we had:
vector_x =(inverse A) vector_y
*/
float sum1 =0;
for(p=1; p <4;p++)
{
sum1 += X[1][p] * D[p];
}
float sum2 =0;
for(p=1; p <4;p++)
{
sum2 += X[2][p] * D[p];
}
float sum3 =0;
for(p=1; p <4;p++)
{
sum3 += X[3][p] * D[p];
}
printf(" n");
printf(" The solutions for the system are: n");
printf(" x = %.2f n", sum1);
printf(" y = %.2f n", sum2);
printf(" z = %.2f n ", sum3);
printf(" n");
return 0;
}