fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Point {
  7. public:
  8. double x, y;
  9.  
  10. double calculate() const {
  11. return pow(x, 3) + pow(y, 2) - 6 * x * y - 39 * x + 18 * y + 20;
  12. }
  13.  
  14. void print() const {
  15. cout << "F(" << x << ";" << y << ")= " << calculate() << endl;
  16. }
  17.  
  18. Point copy() const {
  19. return *this;
  20. }
  21. };
  22.  
  23. int* determine_direction(Point& point, double step_size) {
  24. static int directions[2] = { 0, 0 };
  25. Point left_point = point.copy(), right_point = point.copy();
  26. double initial_value = point.calculate();
  27.  
  28. left_point.x -= step_size;
  29. right_point.x += step_size;
  30. double left_value = left_point.calculate();
  31. double right_value = right_point.calculate();
  32. left_point.print();
  33. right_point.print();
  34.  
  35. if (initial_value > left_value || initial_value > right_value) {
  36. if (left_value < right_value) {
  37. directions[0] = -1;
  38. point.x -= step_size;
  39. }
  40. else {
  41. directions[0] = 1;
  42. point.x += step_size;
  43. }
  44. }
  45. else {
  46. directions[0] = 0;
  47. }
  48.  
  49. left_point = point.copy(); right_point = point.copy();
  50. left_point.y -= step_size;
  51. right_point.y += step_size;
  52. double down_value = left_point.calculate();
  53. double up_value = right_point.calculate();
  54. left_point.print();
  55. right_point.print();
  56.  
  57. if (initial_value > down_value || initial_value > up_value) {
  58. if (down_value < up_value) {
  59. directions[1] = -1;
  60. point.y -= step_size;
  61. }
  62. else {
  63. directions[1] = 1;
  64. point.y += step_size;
  65. }
  66. }
  67. else {
  68. directions[1] = 0;
  69. }
  70.  
  71. return directions;
  72. }
  73.  
  74. void optimize_point(Point& point, double epsilon, double step_size) {
  75. int iteration = 0;
  76. Point last_point;
  77.  
  78. while (true) {
  79. iteration++;
  80. cout << "Iteration " << iteration << "\nStep size: " << step_size << endl;
  81. cout << "Current Point: ";
  82. point.print();
  83. cout << " -- Find direction -- " << endl;
  84.  
  85. last_point = point.copy();
  86. int* directions = determine_direction(point, step_size);
  87.  
  88. if (last_point.calculate() > point.calculate()) {
  89. cout << " -- Pattern movement -- "<< endl;
  90. while (last_point.calculate() >= point.calculate()) {
  91. last_point = point.copy();
  92. point.x += directions[0] * step_size;
  93. point.y += directions[1] * step_size;
  94. point.print();
  95. }
  96. point = last_point.copy();
  97. cout << " -- New Base Point -- " << endl;
  98. point.print();
  99. }
  100. else if (step_size >= epsilon) {
  101. cout << " -- Change step -- " << endl;
  102. step_size /= 2;
  103. }
  104. else {
  105. break;
  106. }
  107.  
  108. cout << endl;
  109. }
  110.  
  111. cout << "\nFinal Result:\nAchieved within " << iteration << " iterations with epsilon precision at:\n";
  112. point.print();
  113. }
  114.  
  115. int main() {
  116. double epsilon, step_size, x, y;
  117.  
  118. cout << "Enter epsilon value: ";
  119. cin >> epsilon;
  120. if (epsilon <= 0 || epsilon > 1) {
  121. cout << "Invalid epsilon value" << endl;
  122. return 1;
  123. }
  124.  
  125. cout << "Enter step size: ";
  126. cin >> step_size;
  127. if (step_size <= 0) {
  128. cout << "Invalid step size" << endl;
  129. return 1;
  130. }
  131.  
  132. cout << "Enter initial x coordinate: ";
  133. cin >> x;
  134. cout << "Enter initial y coordinate: ";
  135. cin >> y;
  136. cout << endl;
  137.  
  138. Point point;
  139. point.x = x;
  140. point.y = y;
  141.  
  142. optimize_point(point, epsilon, step_size);
  143. return 0;
  144. }
  145.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Enter epsilon value: Enter step size: Enter initial x coordinate: Enter initial y coordinate: 
Iteration 1
Step size: 4.67297e-310
Current Point: F(1.14292e-310;0)= 20
 -- Find direction -- 
F(-3.53005e-310;0)= 20
F(5.81588e-310;0)= 20
F(1.14292e-310;-4.67297e-310)= 20
F(1.14292e-310;4.67297e-310)= 20

Final Result:
Achieved within 1 iterations with epsilon precision at:
F(1.14292e-310;0)= 20