#define _CRT_SECURE_NO_WARNINGS #define ACCURACY 0.01 #include #include unsigned int iterations = 0; volatile double y_a, y_b, a, b; volatile double y_x1, y_x2, x1, x2; double funct(double x) { double _return = x/(1.2 - sin(2*x)); return _return; } double calc_point1() { x1 = (a + b)/3; iterations++; return x1; } double calc_point2() { x2 = ((a + b) * 2)/3; iterations++; return x2; } void calc_limits(double _x1, double _x2) { a = funct(_x1); b = funct(_x2); } void calc_internals(double _x1, double _x2) { y_x1 = funct(_x1); y_x2 = funct(_x2); } void calc() { while ((b-a) > ACCURACY) { calc_internals(calc_point1(), calc_point2()); if (y_x1 >= y_x2) { a = x1; } else if (y_x1 < y_x2) { b = x2; } } } int main() { calc_limits(1.0, 4.2); calc(); std::cout << "Iterations: " << iterations << std::endl; std::cout << "Minimum: " << (a+b)/2 << " " << funct((a+b)/2) << std::endl; getchar(); return 0; }