Monday, February 2, 2015

Augmented Matrix Solution By reducing it to Row Echelon Form in Java

Read Also:  


Augmented Matrix Solution By reducing it to Row Echelon Form And then Calculate the result by backward substitution (Code in Java) 



package echolon;
import java.util.Scanner;
/**
 *
 * @author usman
 */
public class Echolon {
     Scanner usman=new Scanner(System.in);
    int i,j,k,n,a;
    float [][] A=new float[20][20];
    float  sum;
    float c;
    float [] x=new float[10];
    
    public  Echolon(){
    sum=0;
    System.out.println("\nEnter the order of matrix: ");
    n=usman.nextInt();
    a=n;
    System.out.println("\nEnter the elements of augmented matrix row-wise:\n\n");
    for(i=1; i<=n; i++)
    {
for(j=1; j<=(n+1); j++)

            if(a<j){System.out.println("\nEnter the Value of B: (1st row):- \n ");}
   System.out.println("A["+i+"]["+j+"] : ");
            
   A[i][j]=usman.nextFloat();
}
    }
    }

    void operation() {
    for(j=1; j<=n; j++)
    {
for(i=1; i<=n; i++)
{
   if(i>j)
   {
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
   A[i][k]=A[i][k]-c*A[j][k];
}
   }
}
    }

    x[n]=A[n][n+1]/A[n][n];
     for(i=n-1; i>=1; i--)
    {
sum=0;
for(j=i+1; j<=n; j++)
{
   sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
    }
    }

    void result(){
    System.out.println("\nThe solution is: \n");
    for(i=1; i<=n; i++)
    {
System.out.println("\nx"+i+" =  "+x[i]+"\t");
    }
    }

void display()
{
System.out.println("\n The Matrix is :-\n ");
for(int i=1;i<=n;i++){
System.out.println("\n");
for(int j=1;j<=n+1;j++){
System.out.println(A[i][j]+"     ");
}}
}


public static void main(String args[]){
Echolon obj=new Echolon();
obj.operation();
obj.display();
obj.result();
}
}



Share this

0 Comment to "Augmented Matrix Solution By reducing it to Row Echelon Form in Java"