include iostream define eps 1e-3 define tay 618 void Gold double doubl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#define eps 1e-3
#define tay 1.618
void Gold(double a, double b)
{
std::cout<<"\n\n\n\tМетод золотого сечения:\n";
double x1, x2, _x, xf1, xf2;
int iter(0);
x1 = a + (b - a) / (tay * tay);
x2 = a + (b - a) / tay;
xf1 = Fun(x1);
xf2 = Fun(x2);
P:
iter++;
if(xf1 >= xf2)
{
a = x1;
x1 = x2;
xf1 = Fun(x2);
x2 = a + (b - a) / tay;
xf2 = Fun(x2);
}
else
{
b = x2;
x2 = x1;
xf2 = xf1;
x1 = a + (b - a) / (tay * tay);
xf1 = Fun(x1);
}
if(fabs(b - a) < eps)
{
_x = (a + b) / 2;
std::cout<<"Результат:\nx = "<<_x<<"\t\tF(x) = "<<Fun(_x)<<
"\nКоличество итераций: "<<iter;
}
else
goto P;
}