Subscribe to A Kid's scribblings        RSS Feed
-----

Matrix-Method of solving simultaneous linear equations

Icon Leave Comment
After the implementation of crammer's rule came up with something new again. Matrix method of solving simultaneous linear equations.

import java.io.*;
public class matrix_method
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("No of variables to be dealt with ::");
        int n=Integer.parseInt(br.readLine());
        int v[][]=new int[n][n];
        int R[]=new int[n]; 
        int i,j;
        for(i=0;i<n;i++)
        {
            System.out.println("Equation ::"+(i+1)+"\n");
            for(j=0;j<n;j++)
            {
                System.out.println("Enter the coefficient of variable ::"+(j+1));
                v[i][j]=Integer.parseInt(br.readLine());
            }
            System.out.println("\nEnter the value of the right hand side of equation ::"+(i+1));
            R[i]=Integer.parseInt(br.readLine());
        }
        process(v,R,n);
    }
    public static double[][] transpose(double det[][],int n)
    {
        int i=0,j=0;
        double temp=0;
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                temp=det[i][j];
                det[i][j]=det[j][i];
                det[j][i]=temp;
            }
        }
        return det;
    }
    public static void process(int v[][],int R[],int n)
 {
     int i=0,j=0;
     double res=0;
     double D[][]=new double[n][n];
     res=(double)det(v,n);
     if(res==0.0)
     {
         System.out.println("The above system of equations is inconsistent");
         return;
     }
     res=(1.0)/(res);
     for(i=0;i<n;i++)
         for(j=0;j<n;j++)
         {
             D[i][j]=(Math.pow((-1),(i+j+2)))*res*calculate(v,n,i,j);
         }
     D=transpose(D,n);
     System.out.println();
     for(i=0;i<n;i++)
     {
         res=0.0;
         for(j=0;j<n;j++)
         {
          res=res+((double)(D[i][j]*R[j]));   
         }
         System.out.println("The value of term "+(i+1)+" is "+res);
     }
 }
     public static double calculate(int D[][],int n,int r,int c)
 {
     int i=0,j=0,x=0,y=0;
     double res=0.0;
     if(n==2)
     {
         if(r==0 && c==0)
             res=(double)D[1][1];
         else if(r==0 && c==1)
             res=(double)D[1][0];
         else if(r==1 && c==0)
             res=(double)D[0][1];
         else if(r==1 && c==1)
             res=(double)D[0][0];
     }
     else
     {
     int t[][]=new int[n-1][n-1];
     for(i=0;i<n;i++)
     {
         if(i==r)
             x--;
         else
         {
             y=0;
         for(j=0;j<n;j++)
         {
             if(j==c)
             {
             y--;
             }
             else
             {
                 t[x][y]=D[i][j];
             }
             y++;
         }
         }
         x++;
     }
     res=(double)det(t,n-1);
     }
     return res;
 }
     
 public static int det(int v[][],int n)
 {
    int i=0,t=0,sign=1,sum=0;
    if(n==2){
        t=(v[0][0]*v[1][1])-(v[0][1]*v[1][0]);
        return t;
    }
    else
    {
        for(i=0;i<n;i++)
        {
            sum+=v[0][i]*sign*matrix(v,i,n);
            sign=sign*(-1);
        }
    }
    return sum;
 }
 public static int matrix(int v[][],int c,int n)
 {
     int i=0,j=0,a=0,b=0;
     int t[][]=new int[n-1][n-1];
     for(i=1;i<n;i++)
     {
         b=0;j=0;
         while(j<n)
         {
          if(j==c)
          j++;
          if(j<n)
          t[a][b]=v[i][j];
          j++;
          b++;          
         }
         a++;
     }
     return det(t,n-1);
 }
}

0 Comments On This Entry

 

Recent Comments

April 2014

S M T W T F S
  12345
6789101112
13141516171819
20 212223242526
27282930   

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)