Program Java Menghitung Determinan dan Invers Matriks 2×2 dan 3×3
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 2×2 atau 3×3 oleh user
System.out.println(“Pilih ordo yang anda inginkan:\nA. 2×2 \t B. 3×3”);
System.out.print(“Ketik ‘A’ atau ‘B’ = “);
char pilih = input.nextLine().charAt(0);
//Jika memilih A, ordo 2×2
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 3×3
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”);
}
}
Recent Comments