Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. package com.company;
  2.  
  3. import Jama.Matrix;
  4. import ptolemy.plot.Plot;
  5. import ptolemy.plot.PlotApplication;
  6.  
  7. import java.util.Random;
  8.  
  9. public class Main {
  10.  
  11. static Plot myPlot = new Plot();
  12. static int N = 50;
  13. static int M = 0;
  14. public static double FI = 360/N;
  15. public static double R = 5;
  16. public static double [][] tab = new double [N][];
  17. public static double [][] tab2 = new double [N][];
  18. public static double [][] tab3 = new double [360][];
  19. public static double [][] tab4 = new double [360][];
  20. public static Random generator = new Random();
  21.  
  22. public static void main(String[] args)
  23. {
  24. generujPunkty();
  25. wypiszTablice();
  26. rysujTablice();
  27. Matrix A = getAMacierz();
  28. Matrix B = getBMacierz();
  29.  
  30. Matrix dekomp = A.solve(B);
  31. System.out.println("Dekomp: ");
  32. dekomp.print(5,2);
  33.  
  34. for(int i=0; i<360;i++)
  35. {
  36. double odleglosc = getR(dekomp,i);
  37. double x = odleglosc*Math.cos(Math.toRadians(i));
  38. double y = odleglosc*Math.sin(Math.toRadians(i));
  39. double [] temp = {x,y};
  40. tab3[i] = temp;
  41. }
  42. rysujTablice2();
  43.  
  44. }
  45. public static void generujPunkty()
  46. {
  47. double kat = FI;
  48.  
  49. for(int i=0; i<N;i++)
  50. {
  51. double rad = Math.toRadians(kat);
  52. double r = R + generator.nextDouble();
  53. double x = r * Math.cos(rad);
  54. double y = r * Math.sin(rad);
  55. double temp[] = {x,y};
  56. double temp2[] ={rad,r};
  57. tab[i]= temp;
  58. tab2[i] = temp2;
  59. kat += FI;
  60. System.out.println("KAT "+rad + " r " +r + " x= "+x + " y= "+y);
  61.  
  62. }
  63. }
  64. public static void wypiszTablice()
  65. {
  66. for(int i=0; i<N; i++)
  67. {
  68. System.out.println("x = "+tab[i][0] + " y = "+ tab[i][1]);
  69. }
  70. }
  71. public static void rysujTablice() {
  72.  
  73. myPlot.setTitle("Plot example");
  74. myPlot.setXLabel("x");
  75. myPlot.setYLabel("s(x)");
  76. int dl = tab.length;
  77. myPlot.setMarksStyle("dots", 0);
  78. //myPlot.setImpulses(true, 0);
  79. for (int i = 0; i < dl - 1; i++) myPlot.addPoint(0, tab[i][0], tab[i][1], false);
  80. PlotApplication app = new PlotApplication(myPlot);
  81. app.setSize(800, 400);
  82. app.setLocation(100, 100);
  83. app.setTitle("MyPicture");
  84. }
  85. public static void rysujTablice2() {
  86.  
  87. myPlot.setTitle("Plot example");
  88. myPlot.setXLabel("x");
  89. myPlot.setYLabel("s(x)");
  90. int dl = tab3.length;
  91. myPlot.setMarksStyle("dots", 0);
  92. //myPlot.setImpulses(true, 0);
  93. for (int i = 0; i < dl - 1; i++) myPlot.addPoint(0, tab3[i][0], tab3[i][1], true);
  94. PlotApplication app = new PlotApplication(myPlot);
  95. app.setSize(800, 400);
  96. app.setLocation(100, 100);
  97. app.setTitle("MyPicture");
  98. }
  99. public static Matrix getAMacierz()
  100. {
  101. int m = (N-1)/2;
  102. M = m;
  103. Matrix mat = new Matrix(N,m);
  104. int index =1;
  105. for(int i=0;i<N;i++)
  106. {
  107. for(int k=0;k<m; k++)
  108. {
  109. if(k==0)
  110. mat.set(i,k,1);
  111. else if(k%2==0)
  112. {
  113. double teta = tab2[i][0];
  114. mat.set(i,k,Math.sin(teta*index));//sin
  115. index++;
  116. }
  117. else
  118. {
  119. double teta = tab2[i][0];
  120. mat.set(i,k,Math.cos(teta*index));//cos
  121. }
  122. }
  123. index = 1;
  124. }
  125. mat.print(5,2);
  126. return mat;
  127. }
  128. public static Matrix getBMacierz()
  129. {
  130. Matrix mat = new Matrix(N,1);
  131. for(int i=0; i<mat.getRowDimension();i++)
  132. {
  133. mat.set(i,0,tab2[i][1]);
  134. }
  135. System.out.println("Macierz b");
  136. mat.print(5,2);
  137. return mat;
  138. }
  139.  
  140. public void dekomp(Matrix A)
  141. {
  142. Matrix x = new Matrix(2*M+1,1);
  143. for(int k=0; k<A.getColumnDimension();k++)
  144. {
  145. for(int i=0; i<A.getRowDimension();i++)
  146. {
  147.  
  148. }
  149. }
  150. }
  151. public static double getR(Matrix x, double kat)
  152. {
  153. double wartosc = 0;
  154. for(int i=0;i<x.getRowDimension();i++)
  155. {
  156. if(i==0)
  157. wartosc+=x.get(0,0);
  158. else if(i%2==1)
  159. {
  160. wartosc+=x.get(i,0)*Math.cos(Math.toRadians(kat));
  161. }else
  162. {
  163. wartosc += x.get(i,0)*Math.sin(Math.toRadians(kat));
  164. }
  165. }
  166. return wartosc;
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement