Senin, 15 Oktober 2012

Program Java Menghitung Determinan dan Invers Matriks 2x2 dan 3x3


Untuk program kali ini, di dalam script-nya, saya berikan pilihan apakah User nanti ingin menghitung determinan dan invers dari ordo 2x2 atau 3x3. Pemilihan ordo dilangsungkan saat menjalankan programnya... kemudian User diminta untuk menginput-kan elemen-elemen matriks-nya satu per satu (bukan random seperti post sebelumnya).



import java.io.*;
import java.util.*;

public class DeterminanInvers {
  
    @SuppressWarnings("empty-statement")
        public static void main(String []args) throws IOException {

    DataInputStream entry = new DataInputStream(System.in);
              try
          {
                 
                Scanner input = new Scanner(System.in);
                System.out.print("\n\n************* PROGRAM MENCARI DETERMINAN DAN INVERS MATRIKS ************* \n");
       
               

                                   //Proses pemilihan ordo 2x2 atau 3x3 oleh user
                System.out.println("Pilih ordo yang anda inginkan:\nA. 2x2 \t B. 3x3");
                System.out.print("Ketik 'A' atau 'B' = ");
                char pilih = input.nextLine().charAt(0);
       
                //Jika memilih A, ordo 2x2
                switch(pilih){
                    case 'a':
                    case 'A':
               
                System.out.print ("\n===== Ordo Matriks = 2 x 2 =====");
               
                int[][] MatriksX = new int [2][2];
                int temp;
               
                //Proses memasukkan elemen-elemen matriksnya
                System.out.println("\nMasukkan elemen-elemen matriksnya : ");
                for (int i=0; i<2 ;i++ )
                {
                    for(int j=0; j<2;j++)
                    {
                        System.out.print("Elemen ["+(i+1)+","+(j+1)+"] = ");
                        MatriksX[i][j]=Integer.parseInt(input.nextLine());
                    }
                }

                //Proses menampilkan matriks
                System.out.print("\nBerikut tampilan matriksnya : \n");
                for (int i=0; i<2 ;i++)
                {
                    for(int j=0;j<2;j++)
                    {
                        System.out.print(MatriksX[i][j]+"\t");
                    }
                    System.out.println();
                }
         
                //Proses perhitungan determinan Matriks
                float dtrm;
                dtrm = (MatriksX[0][0]*MatriksX[1][1])-(MatriksX[1][0]*MatriksX[0][1]);

                //Proses menampilkan determinan Matriks
                System.out.println("\nMaka, determinan = "+dtrm);
               
                //Proses membentuk adjoin
                System.out.print("\nInvers Matriks : \n");
                temp = MatriksX[0][0];
                MatriksX[0][0]=MatriksX[1][1];
                MatriksX[1][1]=temp;
                MatriksX[0][1]=MatriksX[0][1]*-1;
                MatriksX[1][0]=MatriksX[1][0]*-1;
      
                //Proses perhitungan Invers
                for (int i=0;i<2;i++)
                {
                    for (int j=0;j<2;j++)
                    {
                        System.out.print(MatriksX[i][j]/dtrm+"\t");
                    }
                    System.out.println();
                }
                   
                break;
               
                //Jika memilih B, ordo 3x3
                    case 'b':
                    case 'B':
               
               
                System.out.print ("\n===== Ordo Matriks = 3 x 3 =====");
               
                int[][] MatriksY = new int [3][3];

                //Proses memasukkan elemen-elemen matriksnya
                System.out.println("\nMasukkan elemen-elemen matriksnya : ");
                for (int i=0; i<3 ;i++ )
                    {
                    for(int j=0; j<3;j++)
                        {
                        System.out.print("Elemen ["+(i+1)+","+(j+1)+"] = ");
                        MatriksY[i][j]=Integer.parseInt(input.nextLine());
                        }
                    }

                //Proses menampilkan matriks
                System.out.print("\nBerikut tampilan matriksnya : \n");
                for (int i=0; i<3 ;i++ )
                    {
                    for(int j=0;j<3;j++)
                        {
                        System.out.print(MatriksY[i][j]+"\t");
                        }
                    System.out.println();
                    }
       
                //Proses perhitungan determinan matriks
                float dtr;
                dtr = (MatriksY[0][0]*MatriksY[1][1]*MatriksY[2][2])+(MatriksY[0][1]*MatriksY[1][2]*MatriksY[2][0])+(MatriksY[0][2]*MatriksY[1][0]*MatriksY[2][1])-(MatriksY[2][0]*MatriksY[1][1]*MatriksY[0][2])-(MatriksY[2][1]*MatriksY[1][2]*MatriksY[0][0])-(MatriksY[2][2]*MatriksY[1][0]*MatriksY[0][1]);

                //Proses menampilkan determinan matriks
                System.out.println("\nMaka, determinan = "+dtr);
               
                System.out.print("\nInvers Matriks : \n");
               
                //Proses Kofaktor
                int m11, m12, m13, m21, m22, m23, m31, m32, m33;
                m11 = (MatriksY[1][1]*MatriksY[2][2]-MatriksY[1][2]*MatriksY[2][1]);
                m12 = -1*(MatriksY[1][0]*MatriksY[2][2]-MatriksY[1][2]*MatriksY[2][0]);
                m13 = (MatriksY[1][0]*MatriksY[2][1]-MatriksY[1][1]*MatriksY[2][0]);
                m21 = -1*(MatriksY[0][1]*MatriksY[2][2]-MatriksY[0][2]*MatriksY[2][1]);
                m22 = (MatriksY[0][0]*MatriksY[2][2]-MatriksY[0][2]*MatriksY[2][0]);
                m23 = -1*(MatriksY[0][0]*MatriksY[2][1]-MatriksY[0][1]*MatriksY[2][0]);
                m31 = (MatriksY[0][1]*MatriksY[1][2]-MatriksY[0][2]*MatriksY[1][1]);
                m32 = -1*(MatriksY[0][0]*MatriksY[1][2]-MatriksY[0][2]*MatriksY[1][0]);
                m33 = (MatriksY[0][0]*MatriksY[1][1]-MatriksY[0][1]*MatriksY[1][0]);
               
                //Proses Adjoin (Transpose dari Kofaktor)
                MatriksY[0][0] = m11;
                MatriksY[0][1] = m21;
                MatriksY[0][2] = m31;
                MatriksY[1][0] = m12;
                MatriksY[1][1] = m22;
                MatriksY[1][2] = m32;
                MatriksY[2][0] = m13;
                MatriksY[2][1] = m23;
                MatriksY[2][2] = m33;
                       
                //Proses perhitungan Invers
                for (int i=0;i<3;i++)
                {
                    for (int j=0;j<3;j++)
                    {
                        System.out.print(MatriksY[i][j]/dtr+"\t");
                    }
                    System.out.println();
                }
                break;
                default:
                }
          }
          catch (Exception e)
            {
            System.out.println("Anda salah memasukkan data");
            }   
         
System.out.println("\n================================= PROGRAM SELESAI =================================\n");
System.out.println("\nCopyright (c) 2012 Arief Budi Sucianto");
}
}
 

Hasilnya akan seperti ini:

 

4 Responses to “Program Java Menghitung Determinan dan Invers Matriks 2x2 dan 3x3”

fuad naufal mengatakan...

tuh nggk ada cara mencari adjoint matriks nya kah?

Mirza Muammar mengatakan...

Arigatou Senpai ..... izin nyontek

Yuliana Hendra mengatakan...

keren kak.... thank you so much, membantu banget (y)

Fatharani Wafda Arifin mengatakan...

terima kasih banyak.. sangat membantu..

Leave a Reply

Ada kesalahan di dalam gadget ini