Welcome GuestFriday, 2025-01-31, 0:41 AM

Free Your Voice


Main » 2011 » August » 3 » Jacobi Iterasiton with Rotasi Diagonal Matrix
5:57 PM
Jacobi Iterasiton with Rotasi Diagonal Matrix
INPUTAN
berupa SPL yang dimasukkan dalam array. jumlah array NXN (3x3, 5x5, dll)
mis :

4 -1 1 8
3 6 2 9
2 4 2 7

jika persamaannya liniernya adalah Ax=b
dengan asumsi untuk semua kolom ke 4 (array [jumlah kolom]) adalah b
dan baris == kolom adalah A >>> jika dalam SPL ini baris ==kolom adalah 3x3
data array diatas disimpan pada ("C:/menix.txt" atau disimpan sesuai keinginan user)

ALGORITMA JACOBI

1. index nilai matrix persamaan dalam array
2. rotasi jika persamaan memiliki kemungkinan untuk diagonal dominan
3. masukkan nilai tebakan awal, max_error, max_iterasi
4. masukkan nilai index dalam rumus jacobi
5. output


Program Java code:

/*
------Deklarasi Variable-----
 * baris    : index baris
 * kolom    : index kolom
 * data     : array untuk menampung nilai Matrix
 * max      : array untk menampung nilai max pada kolom/baris
 * temp     : temporari sementara untuk menampung nilai
 */

import java.io.*;

public class Jacobi_Iteration {

    public static int baris = 0, kolom = 0;
    public static int data[][];
    public static int max[];
    public static double zigma;
    public static double sigma[];
    public static double a[];
    public static double b[];
    public static int eoc;

    public static void main(String args[]) throws Exception {

        File file = new File("C:/menik.txt");
        //cek keberadaan file
        if (!file.exists() || !file.canRead()) {
            System.out.println("Tidak bisa terbaca" + file);
        }
        boolean status = true;

        try {
            BufferedReader fin = new BufferedReader(new FileReader(file));
            BufferedReader fin2 = new BufferedReader(new FileReader(file));
            String line, temp7, temp6 = "";
            while ((line = fin.readLine()) != null) {
                //mencari jumlah baris dan kolom
                if (!(line.equalsIgnoreCase(""))) {
                    baris++;
                    for (int y = 0; y < line.length(); y++) {
                        temp7 = line.substring(y, y + 1);
                        if ((temp7.equalsIgnoreCase(" "))) {
                            kolom++;
                            temp6 = "";

                        } else {
                            temp6 += temp7;
                        }
                    }
                    kolom++;
                }
                System.out.println("brs " + baris + " kolom " + kolom / baris);
            }
            temp6 = "";
            //mengubah tipe string ke integer
            data = new int[baris][kolom / baris];
            max = new int[kolom / baris];
            sigma = new double[kolom / baris];
            a = new double[baris];
            b = new double[baris];
            int temp = 0, temp2 = 0;
            try {
                while ((line = fin2.readLine()) != null) {


                    if (!(line.equalsIgnoreCase(""))) {
                        for (int i = 0; i < line.length(); i++) {
                            temp7 = line.substring(i, i + 1);
                            if (temp7.equalsIgnoreCase(" ")) {
                                data[temp][temp2] = Integer.parseInt(temp6);
                                temp2++;
                                temp6 = "";

                            } else {
                                temp6 += temp7;
                            }
                        }
                        data[temp][temp2] = Integer.parseInt(temp6);
                        temp2++;
                        temp6 = "";
                        temp++;

                        temp2 = 0;

                    }
                }
            } catch (Exception e) {
                status = false;
                System.out.println("Sistem persamaan tidak terpenuhi.");
                System.out.println("Jumlah baris & kolom tidak sama. ");

            }

            if (status) {
                System.out.println("jumlah baris : " + baris);
                System.out.println("jumlah kolom : " + ((kolom) / baris));
                print();
                maxposition();

                if (cek_max()) {
                    rotasi();
                } else {
                    System.out.println("SPL tidak bisa diagonal Matrix");
                }
                jacobi();
            }

        } catch (FileNotFoundException e) {
            System.out.println("File tidak ada");

        }

    }
    //cek kesamaan max

    public static boolean cek_max() {
        for (int i = 0; i < (baris - 1); i++) {
            for (int j = (i + 1); j < (baris); j++) {
                if (max[i] == max[j]) {
                    return false;
                }
            }
        }
        return true;
    }
    //plot MATRIX

    public static void print() {
        System.out.println("Bentuk Matrix");
        for (int a = 0; a < baris; a++) {
            for (int b = 0; b < (kolom/baris); b++) {
                System.out.print(data[a][b] + " ");
                //  System.out.print(data[a][kolom/baris] + " ");
            }
            System.out.println();
        }
    }

    public static void swap(int a, int b) {
        for (int i = 0; i < (kolom/baris); i++) {
            int temp = data[a][i];
            data[a][i] = data[b][i];
            data[b][i] = temp;
        }
    }

    public static void rotasi() {
        System.out.println("\nSyarat : \njika temp1 > (xn)+(xn+1)+ (xn+2) dst\nmaka temp1>tempt3");
        System.out.println("Posisi rotasi Diagonal Matrix");
        for (int d = 0; d < baris; d++) {
            System.out.println(d + " :: " + max[d]);
        }

        for (int a = 0; a < (baris);) {
            if (a != max[a]) {
                swap(a, max[a]);
            } else {
                a++;
            }
            maxposition();

        }

        print();

    }
    //mencari nilai max pada tiap persamaan

    public static void maxposition() {

        //mengambil nilai max pada persamaan

        for (int i = 0; i < baris; i++) {
            int temp1 = data[i][0];
            max[i] = 0;
            // zigma[i] = 0;

            int temp3 = 0, temp4 = 0, temp9 = 0;
            boolean cek = false;
            for (int r = 0; r < baris; r++) {
                if (Math.abs(temp1) < Math.abs(data[i][r])) {
                    temp1 = Math.abs(data[i][r]);
                    temp4 = r;
                    max[i] = r;
                }
            }
            //menjumlahkan selain nilai max
            for (int j = 0; j < baris; j++) {
                if (Math.abs(temp4) != j) {
                    temp3 += Math.abs(data[i][j]);
                    temp9 += (data[i][j]);
                }
            }

            //membandingkan nilai max > jumlah nilai yang lainnya
            if (Math.abs(temp1) > Math.abs(temp3)) {
                cek = true;
            }

            if (!cek) {
                System.out.println("Persamaan Tidak Memenuhi Rumus Diagonal Matrix");
                print();
                System.exit(0);
            }

        }
    }

    public static void jacobi() {

        double x = 0;
        boolean status = true;
        double err = 0.0000001;

        double input_awal[] = new double[]{1, 2, 2};
        double input_awal2[] = new double[input_awal.length - 1];
      
        int loop = 0, loopmax = 20;

        while (loop < loopmax && cek(input_awal, input_awal2,err)) {
            System.out.println("\nloop ke-" + (loop+1));
            for (int zz = 0; zz < input_awal.length - 1; zz++) {
                input_awal2[zz] = input_awal[zz];
            }

            for (int i = 0; i < baris; i++) {
                zigma = 0;
                for (int j = 0; j < baris; j++) {
                    if (j != i) {
                        zigma = zigma + (data[i][j]) * input_awal[j];
                        sigma[i] = zigma;
                    }
                }
               // System.out.println("-------------" + sigma[i]);
            }

            for (int k = 0; k < baris; k++) {
                x = (((data[k][baris]) - sigma[k]) / data[k][k]);
                input_awal[k] = x;
               // System.out.println("-----" + input_awal[k] + " = " + (data[k][baris]) + " - " + sigma[k] + " / " + (data[k][k]));
                System.out.println("x"+k+" = "+input_awal[k] );
            }

            loop++;
        }

        if(loop>=loopmax)
            status=false;

        System.out.println("KONVERGEN : " + status);
    }

    public static boolean cek(double[] a, double[] b,double error) {
        for (int i = 0; i < a.length - 1; i++) {
           
            if (Math.abs(a[i] - b[i]) < error) {
                return false;
            }
          //  System.out.println("Error "+Math.abs(a[i] - b[i])+" < "+ error);
        }
        return true;
    }
}

Category: Code pRoGram | Views: 30821 | Added by: Nazk | Rating: 0.0/0
Total comments: 1
1 quisuangeSige  
0
belajar banyak

Name *:
Email *:
Code *:
Category
tiPz & tRikz [4]
KoMpuTer [3]
neTwoRk [2]
Code pRoGram [5]
OuTdooR [0]
Live sTyLe [0]
mY VoiCe [3]
AdvErTiseMenT [0]
cORd muSic [1]
tuGas camPuz [2]
yOur pooL
Rate my site
Total of answers: 3
seArcH
Geographic Photo
LoGin
CaLenDer
«  August 2011  »
SuMoTuWeThFrSa
 123456
78910111213
14151617181920
21222324252627
28293031
CLocK
mp3
GoogLe SerViCe
Classic aRt