Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.45 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.PrintWriter;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws FileNotFoundException {
  11.         StringBuilder bufor = new StringBuilder("Raport z badania\n");
  12.         System.out.println("Podaj tryb: ");
  13.         Scanner input = new Scanner(System.in);
  14.         int m = input.nextInt();
  15.         System.out.println("Podaj parametr 1:");
  16.         double input0 = input.nextDouble();
  17.         System.out.println("Podaj parametr 2:");
  18.         double input1 = input.nextDouble();
  19.         System.out.println("Podaj parametr 3:");
  20.         double input2 = input.nextDouble();
  21.         double[] inputMatrix = new double[] {input0, input1, input2};
  22.         double[][] w = {
  23.                 {0,-1,-3},
  24.                 {-1,0,2},
  25.                 {-3,2,0}};
  26.  
  27.  
  28.             synchronizedMode(bufor, w, inputMatrix);
  29.     }
  30.  
  31.     private static void synchronizedMode(StringBuilder bufor, double[][] w, double[] v0) throws FileNotFoundException {
  32.  
  33.         Matrix matrixW = new Matrix(w);
  34.         Matrix matrixV0 = new Matrix(v0);
  35.  
  36.         Matrix matrixVT, matrixVTm1, matrixUT;
  37.         int step = 0;
  38.         boolean stopCondition = true;
  39.  
  40.         double energyOld = 0;
  41.  
  42.         System.out.println(String.format("Macierz wag:\n%s", matrixW.ToString("%.1f", "\t", "\n")));
  43.         System.out.println(String.format("Badanie wektora V0:\n%s", matrixV0.ToString("%.1f", "\t", "\n")));
  44.         bufor.append(String.format("Macierz wag:\n%s\n", matrixW.ToString("%.1f", "\t", "\n")));
  45.         bufor.append(String.format("Badanie wektora V0:\n%s\n", matrixV0.ToString("%.1f", "\t", "\n")));
  46.         matrixV0 = Matrix.Transpose(matrixV0);
  47.         //matrixVTm1 = new Matrix(matrixV0.ToArray(), true);
  48.         while (true) {
  49.             step++;
  50.             System.out.println(String.format("Krok nr %d-------------", step));
  51.             bufor.append(String.format("Krok nr %d-------------\n", step));
  52.  
  53.             matrixUT = Matrix.Multiply(matrixW, matrixV0);
  54.             System.out.println(String.format("Potencjał wejściowy U(%d):\n%s", step, matrixUT.ToString("%.1f", "\t", "\n")));
  55.             bufor.append(String.format("Potencjał wejściowy U(%d):\n%s\n", step, matrixUT.ToString("%.1f", "\t", "\n")));
  56.  
  57.             matrixVT = new Matrix(matrixUT.ToBiPolar().ToArray(), true);
  58.             System.out.println(String.format("Potencjał wyjściowy V(%d):\n%s", step, matrixVT.ToString("%.1f", "\t", "\n")));
  59.             bufor.append(String.format("Potencjał wyjściowy V(%d):\n%s\n", step, matrixVT.ToString("%.1f", "\t", "\n")));
  60.  
  61.             double energy = 0;
  62.             for (int j = 0; j < 3; j++) {
  63.                 for (int i = 0; i < 3; i++) {
  64.                     energy += matrixW.GetElement(i, j)
  65.                             * matrixV0.GetElement(i, 0)
  66.                             * matrixVT.GetElement(j, 0);
  67.  
  68.  
  69.                 }
  70.  
  71.             }
  72.             energy *= -1;
  73.  
  74.             System.out.println(String.format("Energia(%d)=%f\n", step, energy));
  75.             bufor.append(String.format("Energia(%d)=%f\n", step, energy));
  76.  
  77.             if (step == 3)
  78.                 break;
  79.             if (matrixV0.Equals(matrixVT))
  80.                 break;
  81.             if (energyOld == energy)
  82.                 break;
  83.  
  84.             energyOld=energy;
  85.  
  86.  
  87.             matrixVTm1 = new Matrix(matrixVT.ToArray(), true);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement