YauhenMardan

Untitled

Oct 10th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 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.  
  38. double k1,k2,k3,q1,q2,q3;
  39.  
  40. //
  41. for(int j=0; j<num; j++, tj+=t){ //j=0,19
  42. tjArray.add(tj);
  43. y1Array.add(y1);
  44. y2Array.add(y2);
  45. //iteration
  46. k1=k1(y2);
  47. q1=q1(tj,y1,y2);
  48. k2=k2(y2,q1);
  49. q2=q2(tj,y1,y2,k1,q1);
  50. k3=k3(y2,q1,q2);
  51. q3=q3(tj,y1,y2,k1,q1,k2,q2);
  52.  
  53. y1=y11(y1,k1,k2,k3);
  54. y2=y21(y2,q1,q2,q3);
  55. }
  56. }
  57. //////////////////////////////////////// run algorithm
  58. public void run(){
  59. //t=t1=0.05
  60. int num1=(int)((b-a)/t1); //(b-a)/t
  61. ArrayList<Double> y1Array1=new ArrayList<>();
  62. ArrayList<Double> y2Array1=new ArrayList<>();
  63. ArrayList<Double> tjArray1=new ArrayList<>();
  64. doAlgorithm(t1,num1,y1Array1,y2Array1,tjArray1);
  65.  
  66. //t=t2=0.10
  67. int num2=(int)((b-a)/t2); //(b-a)/t
  68. ArrayList<Double> y1Array2=new ArrayList<>();
  69. ArrayList<Double> y2Array2=new ArrayList<>();
  70. ArrayList<Double> tjArray2=new ArrayList<>();
  71. doAlgorithm(t2,num2,y1Array2,y2Array2,tjArray2);
  72.  
  73. //calculate error for t=t1=0.05
  74. ArrayList<Double> y1Error=new ArrayList<>();
  75. ArrayList<Double> y2Error=new ArrayList<>();
  76. calculateError(num1,tjArray1,y1Array1,y2Array1,y1Error,y2Error);
  77. double error = Math.max(Collections.max(y1Error),Collections.max(y2Error));
  78.  
  79. //TEMP
  80. // //calculate error for t=t2=0.1
  81. // ArrayList<Double> y1ErrorT=new ArrayList<>();
  82. // ArrayList<Double> y2ErrorT=new ArrayList<>();
  83. // calculateError(num2,tjArray2,y1Array2,y2Array2,y1ErrorT,y2ErrorT);
  84. // double errorT = Math.max(Collections.max(y1ErrorT),Collections.max(y2ErrorT));
  85.  
  86. //calculate error Runge
  87. ArrayList<Double> y1Diff=new ArrayList<>();
  88. ArrayList<Double> y2Diff=new ArrayList<>();
  89. calculateDiff(num2,y1Array1, y1Array2, y1Diff);
  90. calculateDiff(num2,y2Array1, y2Array2, y2Diff);
  91. double errorRunge = Math.max(Collections.max(y1Diff),Collections.max(y2Diff));
  92. errorRunge=round(errorRunge/(Math.pow(2.0,p)-1.0));
  93. //output
  94. print(num1,tjArray1,y1Array1,y2Array1, error, errorRunge);
  95.  
  96. //TEMP
  97. // print(num2,tjArray2,y1Array2,y2Array2, errorT, 0);
  98. }
  99. //////////////////////////////////////// calculate error
  100. private void calculateDiff(int num, ArrayList<Double> array1,ArrayList<Double> array2, ArrayList<Double> diff){
  101. double d;
  102. for(int i=0;i<num;i++){
  103. d=Math.abs(array1.get(i*2)-array2.get(i));
  104. diff.add(d);
  105. }
  106. }
  107. private void calculateError(int num, ArrayList<Double> tjArray,ArrayList<Double> y1Array,ArrayList<Double> y2Array,ArrayList<Double> y1Error,ArrayList<Double> y2Error){
  108. double tj;
  109. for(int i=0;i<num;i++){
  110. tj=tjArray.get(i);
  111. y1Error.add(round(Math.abs(u(tj)-y1Array.get(i))));
  112. y2Error.add(round(Math.abs(du(tj)-y2Array.get(i))));
  113.  
  114. // 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))));
  115. }
  116. }
  117. //////////////////////////////////////// print output
  118. private void print(int num, ArrayList<Double> tjArray,ArrayList<Double> y1Array,ArrayList<Double> y2Array, double error, double errorRunge){
  119. System.out.printf("%2s%5s%14s%14s%14s%14s\n","j", "tj","u","u'", "y1", "y2");
  120. double tj;
  121. for(int i=0;i<num;i++){
  122. tj=tjArray.get(i);
  123. 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)));
  124. }
  125. System.out.printf("Error is %14s\n",error);
  126. System.out.printf("Error (Runge method) is %14s\n",errorRunge);
  127. }
  128. //////////////////////////////////////// round
  129. private double round(double value) {
  130. if (PLASES < 0) throw new IllegalArgumentException();
  131.  
  132. BigDecimal bd = new BigDecimal(Double.toString(value));
  133. bd = bd.setScale(PLASES, RoundingMode.HALF_UP);
  134. return bd.doubleValue();
  135. }
  136. //////////////////////////////////////// u(t)
  137. public double u(double t){
  138. return t*t-3.0*t+3.0;
  139. }
  140. //////////////////////////////////////// u'(t)
  141. public double du(double t){
  142. return 2.0*t-3.0;
  143. }
  144. //////////////////////////////////////// y1.j+1,y2.j+1
  145. //y1.j+1=F(y1.j,k1,k2,k3)
  146. public double y11(double y1 , double k1, double k2, double k3){
  147. return y1+t/6.0*(k1+4.0*k2+k3);
  148. }
  149. //y2.j+1=F(y2.j,q1,q2,q3)
  150. public double y21(double y2 , double q1, double q2, double q3){
  151. return y2+t/6.0*(q1+4.0*q2+q3);
  152. }
  153. //////////////////////////////////////// k1,q1
  154. //k1=f1(tj,y1.j,y2.j)
  155. public double k1(double y2){
  156. return y2;
  157. }
  158. //q1=f2(tj,y1.j,y2.j)
  159. public double q1(double tj, double y1, double y2){
  160. return f2(tj,y1,y2);
  161. }
  162. //////////////////////////////////////// k2,q2
  163. //k2=f1(tj+1/2t,y1.j+1/2tk1,y2.j+1/2tq1)
  164. public double k2(double y2, double q1){
  165. return y2+0.5*t*q1;
  166. }
  167.  
  168. //q2=f2(tj+1/2t,y1.j+1/2tk1,y2.j+1/2tq1)
  169. public double q2(double tj, double y1, double y2, double k1,double q1){
  170. return f2(tj+0.5*t,y1+0.5*t*k1,y2+0.5*t*q1);
  171. }
  172. //////////////////////////////////////// k3,q3
  173. //k3=f1(tj+t,y1.j-tk1+2tk2,y2.j-tq1+2tq2)
  174. public double k3(double y2, double q1, double q2){
  175. return y2-t*q1+2.0*t*q2;
  176. }
  177. //q3=f2(tj+t,y1.j-tk1+2tk2,y2.j-tq1+2tq2)
  178. public double q3(double tj, double y1, double y2, double k1,double q1, double k2, double q2){
  179. return f2(tj+t,y1-t*k1+2.0*t*k2,y2-t*q1+2.0*t*q2);
  180. }
  181. //////////////////////////////////////// f1,f2
  182. //f1=y2.j
  183. public double f1(double y2){
  184. return y2;
  185. }
  186. //f2=...
  187. public double f2(double tj, double y1, double y2){
  188. double numerator=tj+2.0+3.0*(tj-2.0)*y2-4.0*y1;
  189. double denumenator=(tj-2.0)*(tj-2.0);
  190. return numerator/denumenator;
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment