#define _CRT_SECURE_NO_WARNINGS
#define ACCURACY 0.01
#include <iostream>
#include <math.h>
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;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}