mathajax

Solve Linear Equation 2x2 matrix

System of Linear Equation

The two are more linear equations are called system of linear equation.
            
              ax1 + bx2  = b1 
cx1 + dx2 = b2

System of Linear Equation in matrix representation

system of linear equation matrix size of  2x2

solution of system of Linear Equation - Algorithm

The following steps are carried out for solving system linear equations by inverse method.
  1. Read Matrix A 2x2
  2. D = determinant (Matrix A)
    • D==0, then print matrix A is singular
    • return no solution

  3. cA = cofactor_matrix (A)
  4. Adjoint-Matrix Adj = transpose (Matrix cA)
  5. A -1 = Adj / D
  6. solution S = A -1 b
  7. print solution S

Java program Solve Linear Equation

The Java program LinearEquation has one constructor function and following four member functions in order to finds solution of system of two linear equations.

constructor LinearEquation - It has to input the two arguments, one is 2D array (Matrix A size of 2x2) and other 1D array (vector b size of 2x1).

  1. determinant - the method used to find determinant of 2x2 matrix A.

  2. cofactor - the method used to find cofactor matrix of 2x2 matrix A.

  3. multiply - the method used to carry out matrix multiplication with adjoint matrix 2x2 and a column vector.

  4. solve - the method used to find a solution vector of matrix A and a column vector b.
 
import java.util.Arrays;

public class LinearEquation extends Matrix {

 double b[];
 public LinearEquation(double A[][],double b[]) {
  super(A);
  this.b=b;
 }
 
 public double determinant() 
 {
  double det = this.getElement(0, 0) * this.getElement(1, 1) 
   - this.getElement(0, 1) * this.getElement(1, 0);
  return det;
 }
 
 public Matrix cofactor() 
 {
  Matrix ajmat=new Matrix(2,2);  
  ajmat.setElement(0, 0, this.getElement(1,1));
  ajmat.setElement(0, 1, -this.getElement(1,0));
  ajmat.setElement(1, 0, -this.getElement(0,1));
  ajmat.setElement(1, 1, this.getElement(0,0));  
  return ajmat;
  
 }
 
 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" );
   System.out.println(this.toString());

  double det=this.determinant();  
  if ( det==0) {
   System.out.println("Determinant =" + det);
   System.out.println("Matrix is sinular");
   return null;   
  }
  
  System.out.println("Determinant =" + det);
  
  Matrix cfmat=this.cofactor();
  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[][]= { {1,-3},{1,1}};
  double b[]= {6,2};
  
  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 2x2 Linear Equation output


Input Matrix A

1.0  -3.0  
1.0  1.0  

Determinant of matrix A =4.0


Cofactor matrix of A
1.0  -1.0  
3.0  1.0  

Adjoint matrix of A
1.0  3.0  
-1.0  1.0  

Inverse of matrix A
0.25  0.75  
-0.25  0.25  

Solution of matrix A
[3.0, -1.0]

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Matrix Determinant by Upper Triangular Matrix

Chebyshev distance between two points