Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package Lab_3.NonLinear;
  2.  
  3. class SolvingNonlinearEquations {
  4. private double accuracy;
  5. private double section_a;
  6. private double section_b;
  7. private Function func;
  8. private double root_chord = 0;
  9. private double root_iterate = 0;
  10. private int iteration_chord = 0;
  11. private int iteration_iterate = 0;
  12. private double[][] xy_mChord = new double[20000][2];
  13. private double[][] xy_mIter = new double[20000][2];
  14.  
  15.  
  16. SolvingNonlinearEquations(int func_num, double accuracy, double section_a, double section_b) {
  17. this.accuracy = accuracy;
  18. if (section_a > section_b) {
  19. this.section_a = section_a;
  20. this.section_b = section_b;
  21. } else {
  22. this.section_a = section_b;
  23. this.section_b = section_a;
  24. }
  25. func = new Function(func_num);
  26. }
  27.  
  28. double[][] getXy_mChord() {
  29. return xy_mChord;
  30. }
  31.  
  32. double[][] getXy_mIter() {
  33. return xy_mIter;
  34. }
  35.  
  36. int getIteration_chord() {
  37. return iteration_chord;
  38. }
  39.  
  40. int getIteration_iterate() {
  41. return iteration_iterate;
  42. }
  43.  
  44. void methodChord() {
  45. double prev_root;
  46. do {
  47. prev_root = root_chord;
  48.  
  49. root_chord = ((section_a * func.calcFunc(section_b)) - (section_b * func.calcFunc(section_a)))
  50. / (func.calcFunc(section_b) - func.calcFunc(section_a));
  51.  
  52. xy_mChord[iteration_chord][0] = section_a;
  53. xy_mChord[iteration_chord][1] = func.calcFunc(section_a);
  54. xy_mChord[iteration_chord + 1][0] = section_b;
  55. xy_mChord[iteration_chord + 1][1] = func.calcFunc(section_b);
  56.  
  57. iteration_chord += 2;
  58.  
  59. if (func.checkSection(root_chord, section_a)) {
  60. section_b = root_chord;
  61.  
  62. } else section_a = root_chord;
  63. } while (accuracyAssessment(root_chord, prev_root));
  64. }
  65.  
  66. void methodIteration() {
  67. double prev_root;
  68. double x = section_a;
  69. double lambda = func.lambda(section_a, section_b);
  70. do {
  71. prev_root = root_iterate;
  72. root_iterate = x + lambda * (func.calcFunc(x));
  73.  
  74. xy_mIter[iteration_iterate][0] = x;
  75. xy_mIter[iteration_iterate][1] = func.calcFunc(x);
  76. xy_mIter[iteration_iterate + 1][0] = root_iterate;
  77. xy_mIter[iteration_iterate + 1][1] = func.calcFunc(root_iterate);
  78.  
  79. x = root_iterate;
  80.  
  81. iteration_iterate += 2;
  82. } while (accuracyAssessment(root_iterate, prev_root));
  83. }
  84.  
  85. private boolean accuracyAssessment(double root, double prev_root) {
  86. return !(Math.abs(root - prev_root) <= accuracy);
  87. }
  88.  
  89. @Override
  90. public String toString() {
  91. return "Метод хорд:\n" +
  92. " корень: " + root_chord + " количество итераций: " + iteration_chord / 2 + "\n" +
  93. "Метод хорд:\n" +
  94. " корень: " + root_iterate + " количество итераций: " + iteration_iterate / 2;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement