Advertisement
YauhenMardan

Untitled

Oct 10th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7.  
  8. public class Algorithm {
  9. //steps
  10. private static double t1=0.05;
  11. private static double t2=0.1;
  12. //bounds
  13. private static final double a=3.0;
  14. private static final double b=4.0;
  15. //initial values
  16. private static final double t0=3.0;
  17. private static final double y10=3.0;
  18. private static final double y20=3.0;
  19. //
  20. private static double p=3.0;
  21.  
  22. //plases for rounding
  23. private static final int PLASES = 10;
  24.  
  25. //step
  26. private double t;
  27.  
  28. //Algorithm
  29. private void doAlgorithm(double t, double num, ArrayList<Double> y1Array, ArrayList<Double> y2Array,ArrayList<Double> tjArray){
  30.  
  31. this.t=t;
  32.  
  33. //temp values
  34. double y1=y10;
  35. double y2=y20;
  36. double tj=t0;
  37. double a,b;
  38.  
  39. //
  40. for(int j=0; j<num; j++, tj+=t){ //j=0,19
  41. tjArray.add(tj);
  42. y1Array.add(y1);
  43. y2Array.add(y2);
  44. //iteration
  45. b=y21(tj,y1,y2);
  46. a=y11(y1,y2,b);
  47. y1=a;
  48. y2=b;
  49. }
  50. }
  51. //////////////////////////////////////// run algorithm
  52. public void run(){
  53. //t=t1=0.05
  54. int num1=(int)((b-a)/t1); //(b-a)/t
  55. ArrayList<Double> y1Array1=new ArrayList<>();
  56. ArrayList<Double> y2Array1=new ArrayList<>();
  57. ArrayList<Double> tjArray1=new ArrayList<>();
  58. doAlgorithm(t1,num1,y1Array1,y2Array1,tjArray1);
  59.  
  60. //t=t2=0.10
  61. int num2=(int)((b-a)/t2); //(b-a)/t
  62. ArrayList<Double> y1Array2=new ArrayList<>();
  63. ArrayList<Double> y2Array2=new ArrayList<>();
  64. ArrayList<Double> tjArray2=new ArrayList<>();
  65. doAlgorithm(t2,num2,y1Array2,y2Array2,tjArray2);
  66.  
  67. //calculate error for t=t1=0.05
  68. ArrayList<Double> y1Error=new ArrayList<>();
  69. ArrayList<Double> y2Error=new ArrayList<>();
  70. calculateError(num1,tjArray1,y1Array1,y2Array1,y1Error,y2Error);
  71. double error = Math.max(Collections.max(y1Error),Collections.max(y2Error));
  72.  
  73. //TEMP
  74. // //calculate error for t=t2=0.1
  75. // ArrayList<Double> y1ErrorT=new ArrayList<>();
  76. // ArrayList<Double> y2ErrorT=new ArrayList<>();
  77. // calculateError(num2,tjArray2,y1Array2,y2Array2,y1ErrorT,y2ErrorT);
  78. // double errorT = Math.max(Collections.max(y1ErrorT),Collections.max(y2ErrorT));
  79.  
  80. //calculate error Runge
  81. ArrayList<Double> y1Diff=new ArrayList<>();
  82. ArrayList<Double> y2Diff=new ArrayList<>();
  83. calculateDiff(num2,y1Array1, y1Array2, y1Diff);
  84. calculateDiff(num2,y2Array1, y2Array2, y2Diff);
  85. double errorRunge = Math.max(Collections.max(y1Diff),Collections.max(y2Diff));
  86. errorRunge=round(errorRunge/(Math.pow(2.0,p)-1.0));
  87. //output
  88. print(num1,tjArray1,y1Array1,y2Array1, error, errorRunge);
  89.  
  90. //TEMP
  91. // print(num2,tjArray2,y1Array2,y2Array2, errorT, 0);
  92. }
  93. //////////////////////////////////////// calculate error
  94. private void calculateDiff(int num, ArrayList<Double> array1,ArrayList<Double> array2, ArrayList<Double> diff){
  95. double d;
  96. for(int i=0;i<num;i++){
  97. d=Math.abs(array1.get(i*2)-array2.get(i));
  98. diff.add(d);
  99. }
  100. }
  101. private void calculateError(int num, ArrayList<Double> tjArray,ArrayList<Double> y1Array,ArrayList<Double> y2Array,ArrayList<Double> y1Error,ArrayList<Double> y2Error){
  102. double tj;
  103. for(int i=0;i<num;i++){
  104. tj=tjArray.get(i);
  105. y1Error.add(round(Math.abs(u(tj)-y1Array.get(i))));
  106. y2Error.add(round(Math.abs(du(tj)-y2Array.get(i))));
  107.  
  108. // System.out.printf("%2s u %14s u'%14s \n",i, round(Math.abs(u(tj)-y1Array.get(i))),round(Math.abs(du(tj)-y2Array.get(i))));
  109. }
  110. }
  111. //////////////////////////////////////// print output
  112. private void print(int num, ArrayList<Double> tjArray,ArrayList<Double> y1Array,ArrayList<Double> y2Array, double error, double errorRunge){
  113. System.out.printf("%2s%5s%14s%14s%14s%14s\n","j", "tj","u","u'", "y1", "y2");
  114. double tj;
  115. for(int i=0;i<num;i++){
  116. tj=tjArray.get(i);
  117. System.out.printf("%2s%5s%14s%14s%14s%14s\n",i, round(tj),round(u(tj)),round(du(tj)), round(y1Array.get(i)), round(y2Array.get(i)));
  118. }
  119. System.out.printf("Error is %14s\n",error);
  120. System.out.printf("Error (Runge method) is %14s\n",errorRunge);
  121. }
  122. //////////////////////////////////////// round
  123. private double round(double value) {
  124. if (PLASES < 0) throw new IllegalArgumentException();
  125.  
  126. BigDecimal bd = new BigDecimal(Double.toString(value));
  127. bd = bd.setScale(PLASES, RoundingMode.HALF_UP);
  128. return bd.doubleValue();
  129. }
  130. //////////////////////////////////////// u(t)
  131. public double u(double t){
  132. return t*t-3.0*t+3.0;
  133. }
  134. //////////////////////////////////////// u'(t)
  135. public double du(double t){
  136. return 2.0*t-3.0;
  137. }
  138. //////////////////////////////////////// y1.j+1,y2.j+1
  139. //y1.j+1=F(y1.j,y2.j,b)
  140. public double y11(double y1, double y2, double b){
  141. return y1+t/2.0*(y2+b);
  142. }
  143. //y2.j+1=F(tj,y1.j, y2.j)
  144. public double y21(double tj, double y1 , double y2){
  145. double A=tj+t-2.0;
  146. double numerator=y2*A*A+t/2.0*f2(tj,y1,y2)*A*A+t/2.0*(tj+t+2.0)-2.0*t*y1-t*t*y2;
  147. double denumenator=A*A-3.0/2.0*t*A+t*t;
  148. return numerator/denumenator;
  149. }
  150. //////////////////////////////////////// f1,f2
  151. //f1=y2.j
  152. public double f1(double y2){
  153. return y2;
  154. }
  155. //f2=...
  156. public double f2(double tj, double y1, double y2){
  157. double numerator=tj+2.0+3.0*(tj-2.0)*y2-4.0*y1;
  158. double denumenator=(tj-2.0)*(tj-2.0);
  159. return numerator/denumenator;
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement