Advertisement
Guest User

Untitled

a guest
Dec 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4. import java.math.*;
  5.  
  6. public class Main
  7. {
  8.  
  9. static double sum;
  10. static int n;
  11. static int N;
  12. static double[] ages;
  13. static double[] numbers;
  14. static double eps = 0.000001;
  15.  
  16. public static void setAges(double[] ages) throws FileNotFoundException
  17. {
  18.  
  19. Scanner sc = new Scanner(new File("Ages"));
  20. String s;
  21. int counter = 0;
  22. while(sc.hasNext()){
  23. s = sc.nextLine();
  24. ages[counter] = Double.parseDouble(s);
  25. counter ++;
  26. }
  27.  
  28. }
  29. public static void setNumbers(double[] numbers) throws FileNotFoundException {
  30.  
  31. Scanner sc = new Scanner(new File("Numbers"));
  32. String s;
  33. int counter = 0;
  34. while(sc.hasNext()){
  35. s = sc.nextLine();
  36. numbers[counter] = Double.parseDouble(s);
  37. counter ++;
  38. }
  39.  
  40. }
  41. public static void PrintArr(double[] arr){
  42. for (int i = 0; i<arr.length; i++){
  43. System.out.println(arr[i]);
  44. }
  45.  
  46. }
  47.  
  48. public static void setLength(){
  49. n = ages.length;
  50. }
  51.  
  52. public static void setSum(double[] numbers, double[] ages){
  53. for (int i = 0; i<n; i++)
  54. sum += numbers[i] * ages[i] ;
  55. }
  56.  
  57. public static void setNumbersSum(double[] numbers){
  58. for (int i = 0; i<n; i++)
  59. N += numbers[i] ;
  60. }
  61.  
  62. public static double getFunctionValue (double a) {
  63. double fa;
  64. double up = 0, bt = 0; // числитель и знаменатель
  65.  
  66. for (int i = 0; i < n; i++) {
  67. up += numbers[i]*ages[i] * Math.exp(a * ages[i]);
  68. bt += numbers[i]* Math.exp(a * ages[i]);
  69. }
  70. up *= N;
  71. bt -= N;
  72.  
  73. fa = sum + (N / a) - (up / bt);
  74.  
  75. return fa;
  76. }
  77. public static double getDerivative (double a) {
  78. double Der;
  79. double up1 = 0, up2 = 0, bt = 0; // числитель и знаменатель
  80.  
  81. for (int i = 0; i < n; i++) {
  82. up1 += numbers[i]*ages[i] * ages[i] * Math.exp(a * ages[i]);
  83. up2 += numbers[i]*ages[i] * Math.exp(a * ages[i]);
  84. bt += numbers[i]*Math.exp(a * ages[i]);
  85. }
  86. up1 *= N;
  87. up2 *= N;
  88. bt -= N;
  89.  
  90. Der = - (N / (a * a)) - (up1 * bt - up2 * (up2 / N)) / (bt * bt);
  91.  
  92. return Der;
  93. }
  94.  
  95. public static double solveEquationForA (double old_a) {
  96. double a = old_a;
  97. double fa = 1;
  98. double der;
  99. double sol = 0;
  100.  
  101. int i = 0;
  102. while(Math.abs(fa) > eps) {
  103. i++;
  104. sol = a;
  105. fa = getFunctionValue(a);
  106. der = getDerivative(a);
  107. a = a - fa / der;
  108. if (i > 100) {
  109. System.out.println("break");
  110. break;
  111. }
  112. //System.out.println(sol);
  113.  
  114. }
  115. return sol;
  116. }
  117.  
  118. public static double solveEquationForB (double a) {
  119. double bt = 0;
  120. for (int i = 0; i < n; i++) {
  121. bt += numbers[i]*Math.exp(a * ages[i]);
  122. }
  123. bt -= N;
  124.  
  125. return (N * a) / bt;
  126. }
  127.  
  128. public static void main(String[] args) throws FileNotFoundException
  129. {
  130. ages = new double[80];
  131. numbers = new double[80];
  132.  
  133. setAges(ages);
  134. setNumbers(numbers);
  135. setLength();
  136. setSum(numbers,ages);
  137. setNumbersSum(numbers);
  138.  
  139. double a, b;
  140. for (double i = 0.01; i < 0.25; i += 0.01) {
  141. a = solveEquationForA(i);
  142. b = solveEquationForB(a);
  143.  
  144. System.out.println("a="+a+", b="+b);
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement