Solving System of Linear Equation by Matrix Inverse
The two or more algebraic equation are called system of equations. The Java program finds solution vector X to a system of three linear equations by matrix inverse method.
a11 x1 + a12 x2 + a13 x3 = b 1a21 x1 + a22 x2 + a23 x3 = b 2
a31 x1 + a32 x2 + a33 x3 = b 3
The three linear equation is represented by matrix format by separating coefficients and unknown variables
| a11 a12 a13 | Coefficients matrix A = | a21 a22 a23 | | a31 a32 a33 | unknown variable X = [ x1 x2 x3 ]' non-homogeneous b = [ b1 b2 b3 ]'
solve given matrix A and vector b (non-homogeneous vector) find value of vector X Matrix representation A X = b multiply both side by A-1 A-1 A X = A-1 b Note - A-1 A = I (Identity matrix) I X = A-1 b Note - I X = X X = A-1 b
Algorithm - Solving System of Linear Equation by Matrix Inverse
The following steps are need to do solving system of linear equation by matrix inverse method.
- Read 3x3 matrix A and 3x1 b vector
- Find determinant value D of matrix A
- if the determinant value D is zero
- then print matrix A is singular, and has inverse matrix
- else goto step 3
- Cofactor matrix - finds cofactor matrix from matrix A.
- Adjoint matrix (adjmat) - finds adjoint matrix by transposing cofactor matrix
- find A-1 = adjmat / D , divide each elements of matrix by D (determinant value) scalar operation over adjoint matrix .
- solution vector X = A-1 b , multiply A-1 by the vector b.
- print solution vector X
Java program - System of Linear Equation by Matrix Inverse
The program Linear-equation has one constructor and two member functions to find a solution vector to system of linear three linear equations.
constructor LinearEquation - accepts two arguments, one is 2D double array having 3x3 elements and other ID double array having 3x1 elements.
- multiply - This method used to carry out a matrix and a vector multiplication operation i.e. matrix A is multiplied by a vector b .
- solve - The method to used to solve the system of three equation by matrix inverse method.
import java.util.Arrays;
public class LinearEquation extends Matrix {
double b[];
public LinearEquation(double A[][],double b[]) {
super(A);
this.b=b;
}
public double[] multiply(Matrix imat) {
double sol[] =new double[imat.getNrow()];
for(int r=0;r<imat.getNrow();r++)
{
double temp=0;
for(int r2=0;r2<b.length;r2++)
temp += imat.getElement(r,r2)*b[r2];
sol[r]= temp;
}
return sol;
}
public double[] solve() {
System.out.println("Input Matrix A 3x3 " );
System.out.println(this.toString());
double det=MatrixOpr2.determinant(this);
if ( det==0) {
System.out.println("Determinant =" + det);
System.out.println("Matrix is sinular");
return null;
}
System.out.println("Determinant =" + det);
Matrix cfmat=MatrixOpr2.cofactor(this);
System.out.println("Cofactor matrix");
System.out.println(cfmat);
Matrix adjmat=cfmat.transpose();
System.out.println("Adjoint matrix");
System.out.println(adjmat);
Matrix imat= Scalar.divide(adjmat, det);
System.out.println("Inverse matrix");
System.out.println(imat);
double sol[]=this.multiply(imat);
return sol;
}
public static void main(String[] args) {
double A[][]= { {4,5,-2},{7,-1,2},{3,1,4}};
double b[]= {-14,42,28};
LinearEquation le=new LinearEquation(A,b);
double solution[]=le.solve();
if ( solution !=null ) {
System.out.println("Solution of Equation :");
System.out.println(Arrays.toString(solution));
}
}
}
Java program Linear Equation by Matrix Inverse Output
Input Matrix A 3x3 4.0 5.0 -2.0 7.0 -1.0 2.0 3.0 1.0 4.0 Determinant of Matrix A =-154.0 Cofactor matrix of A -6.0 -22.0 10.0 -22.0 22.0 11.0 8.0 -22.0 -39.0 Adjoint matrix of A -6.0 -22.0 8.0 -22.0 22.0 -22.0 10.0 11.0 -39.0 Inverse matrix of A 0.03896103896103896 0.14285714285714285 -0.05194805194805195 0.14285714285714285 -0.14285714285714285 0.14285714285714285 -0.06493506493506493 -0.07142857142857142 0.2532467532467532 Solution of Equation [4.0, -4.0, 4.999999999999999]
where can we access Matrix Class ?
ReplyDelete