Selasa, 27 Mei 2014

Program Invers Matriks Java Netbeans



package inversmatriks;
import java.io.*;
import javax.swing.*;

public class Main {

  public static void main(String[] args) {
   {
                BufferedReader input=new BufferedReader(new InputStreamReader (System.in));
                System.out.println("<<<<<>>>>>>>");
                System.out.println("Input Elemen-Elemen Matriks.");
                int matrik[][]=new int[3][3];
                for (int i=0;i<3;i++)
                {
                                for (int j=0;j<3;j++)
                                {
                                System.out.print("Input Elemen Matriks ke ["+(i+1)+","+(j+1)+"]: ");
                                matrik[i][j]=Integer.parseInt(input.readLine());

                                }
                }
                System.out.println();
                System.out.println("Matriks 3x3 : ");
                for (int i=0;i<3;i++)
                {
                                System.out.print("| ");
                                for (int j=0;j<3;j++)
                                {
                                                System.out.print(matrik[i][j]+" ");
                                }
                                System.out.println("|");
                }






                //adjoin matriks
                int temp[][]=new int[3][3];
                temp[0][0]=matrik[1][1]* matrik[2][2]-matrik[2][1]*matrik [1][2];
                temp[0][1]=matrik[1][0]* matrik[2][2]-matrik[2][0]*matrik [1][2];
                temp[0][2]=matrik[1][0]* matrik[2][1]-matrik[1][1]*matrik [2][0];
                temp[1][0]=matrik[0][1]* matrik[2][2]-matrik[0][2]*matrik [2][1];
                temp[1][1]=matrik[0][0]* matrik[2][2]-matrik[0][2]*matrik [2][0];
                temp[1][2]=matrik[0][0]* matrik[2][1]-matrik[0][1]*matrik [2][0];
                temp[2][0]=matrik[1][1]* matrik[2][2]-matrik[1][2]*matrik [2][1];
                temp[2][1]=matrik[0][0]* matrik[1][2]-matrik[0][2]*matrik [1][0];
                temp[2][2]=matrik[0][0]* matrik[1][1]-matrik[0][1]*matrik [1][0];

                System.out.println ();
                System.out.println ("Adjoin Matriks : ");
                for (int i=0;i<3;i++)
                {
                                System.out.print("| ");
                                for (int j=0;j<3;j++)
                                {
                                                System.out.print(temp[i][j]+" ");
                                }
                                System.out.println("|");
}
                System.out.println();

                int dtr=((matrik[0][0]*matrik[1][1]*matrik[2][2] + (matrik[0][1]*matrik[1][2] * matrik[2][0])+
                                (matrik[0][2]*matrik[1][0]*matrik[2][1]))-(matrik[0][1]*matrik[1][0]*matrik[2][2] +
                                (matrik[0][0] * matrik[1][2] * matrik[2][1])+(matrik[0][2]*matrik[1][1]*matrik[2][0])));
                System.out.println("Determinan Matriks= "+dtr);
                System.out.println();


                if (dtr!=0)
                {
                temp[0][0]=temp[0][0]/dtr;
                temp[0][1]=temp[0][1]/dtr;
                temp[0][2]=temp[0][2]/dtr;
                temp[1][0]=temp[1][0]/dtr;
                temp[1][1]=temp[1][1]/dtr;
                temp[1][2]=temp[1][2]/dtr;
                temp[2][0]=temp[2][0]/dtr;
                temp[2][1]=temp[2][1]/dtr;
                temp[2][2]=temp[2][2]/dtr;

                System.out.println ();
                System.out.println ("Invers Matriks : ");
                for (int i=0;i<3;i++)
                {
                                System.out.print("| ");
                                for (int j=0;j<3;j++)
                                {
                                                System.out.print(temp[i][j]+" ");
                                }
                                System.out.println("|");
                }
                }
                else {
                                                System.out.println("Tidak ada hasil inversnya, karena matriks Singular");
                }


                }
}
}

 

Tidak ada komentar: