import java.io.*;
import java.text.*;
class choleski
{
double M [][] = new double[10][10];
int baris,kolom;
double d [] = new double [10];
double a [] = new double [10];
double b [] = new double [10];
double c [] = new double [10];
double Q [] = new double [10];
double P [] = new double [10];
double x [] = new double [10];
choleski() throws Exception
{
input_matrik();
}
public void input_matrik()throws Exception
{
DataInputStream input = new DataInputStream(System.in);
System.out.print("masukkan banyak persamaan = ");
baris = Integer.parseInt(input.readLine());
do
{
System.out.print("masukkan banyak variabel x = ");
kolom = Integer.parseInt(input.readLine());
}
while (baris!=kolom);
System.out.print("masukkan nilai ");
for(int i=0;i
{
System.out.print("\tx[i]");
}
System.out.println("\td");
System.out.println("masukkan nilai x = ");
int j;
for(int i=0;i
{
System.out.println("persamaan "+(i+1)+" = ");
for(j=0;j<=kolom;j++)
{
if(j==kolom)
{
System.out.print("d = ");
M[i][j] = Double.parseDouble(input.readLine());
}
else
{
System.out.print("x["+(j+1)+"] = ");
M[i][j] = Double.parseDouble(input.readLine());
}
}
System.out.println();
}
}
public void cetak1()
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
System.out.println("bentuk persamaan ");
for(int i=0;i
{
System.out.print("\tx[i]");
}
System.out.println("\td");
for(int i=0;i
{
for(int j=0;j<=kolom;j++)
{
System.out.print("\t"+nf.format(M[i][j]));
}
System.out.println();
}
}
public void cetak()
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
for(int i=0;i
{
for(int j=0;j<=kolom;j++)
{
System.out.print("\t"+nf.format(M[i][j]));
}
System.out.println();
}
}
public void gaus()
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
double op;
for(int i=0;i
{
for(int j=i+2;j
{
op=M[j][i]/M[i+1][i];
for(int h=0;h
M[j][h]=M[j][h]-op*M[i+1][h];
System.out.print("baris ke "+j+" = baris ke "+j+"- "+nf.format(op)+" kali baris ke "+i+"\n");
cetak();
}
}
}
void jordan()
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
double op;
int t=kolom-1;
for(int i=baris-1;i>0;i--)
{
System.out.println("operasi pada baris ke"+i+"\n");
for(int j=t-2;j>=0;j--)
{
op=M[j][i]/M[i-1][i];
for(int h=0;h
M[j][h]=M[j][h]-op*M[i-1][h];
System.out.println("baris ke "+j+" = baris ke"+j+" - "+nf.format(op)+" kali baris ke "+i+"\n");
cetak();
}
t--;
}
}
void inisial()
{
System.out.println("nilai \ta[i]\tb[i]\tc[i]\td");
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
a[0] = 0;
for (int i=0;i
{
for(int j=0;j<=kolom;j++)
{
if(i==j)
{
b[i]=M[i][j];
}
if(i-j==-1)
{
if(i==baris-1)
{
c[i]=0;
}
else
c[i]=M[i][j];
}
if(i-j==1)
{
a[i]=M[i][j];
}
d[i] = M[i][kolom];
}
}
//System.out.println("a[i]\tb[i]\tc[i]\td[i]");
for(int i=0;i
{
System.out.print("\t"+nf.format(a[i])+"\t"+nf.format(b[i])+"\t"+nf.format(c[i])+"\t"+nf.format(d[i]));
System.out.println();
}
System.out.println();
}
void hitung_Q_P()
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
P[0]=0;
Q[0]=0;
System.out.println("P[i]\t\tQ[i]");
for(int i=1;i<=baris;i++)
{
P[i]= (-1*c[i-1])/(a[i-1]*P[i-1]+b[i-1]);
Q[i]= (d[i-1]-a[i-1]*Q[i-1])/(a[i-1]*P[i-1]+b[i-1]);
System.out.print(nf.format(P[i])+"\t\t"+nf.format(Q[i]));
System.out.println();
}
System.out.println();
}
void hitung_x()
{
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
x[baris]=Q[baris];
System.out.println("nilai x yang memenuhi persamaan = ");
System.out.print("x["+baris+"] = "+nf.format(x[baris]));
for(int i=baris-1;i>0;i--)
{
x[i]= P[i]*x[i+1]+Q[i];
System.out.print("\tx["+i+"] = "+nf.format(x[i])+"\t");
}
System.out.println();
}
}
class demo
{
public static void main(String[] args) throws Exception
{
System.out.println("metode choleski");
choleski c = new choleski();
c.cetak1();
//if(c.M[c.baris][1]==0)
//{
c.gaus();
c.jordan();
//}
c.inisial();
c.hitung_Q_P();
c.hitung_x();
}
}
Hasil Eksekusi
Demikian Sekian dan trimakasih...Bila ada pertanyaan Monggo sedoyo comment...Tapi ingat...blajar memahami lebih baik daripada hanya mengcopy-paste saja...So...Pahamilah......
Tidak ada komentar:
Posting Komentar