Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- #include<string>
- #include<alg.h>
- using namespace std;
- const double EPS = 1e-4;
- const double DELTA = 1e-5;
- const int INF = 1e8 + 13;
- const double goldenRatio1 = (3 - sqrt(5)) / 2; // "Золотое" число
- const double goldenRatio2 = (sqrt(5) - 1) / 2; // "Золотое" число
- double func(double x)
- {
- return 2. * x*x - 6 * x - 3;
- }
- pair<double, double> solve()
- {
- double left = -1;
- double right = 3;
- double step = 0.1;
- double answer = INF;
- double answerPoint = -INF;
- double x1, x2;
- while (fabs(right - left) > EPS) {
- x1 = left + goldenRatio1*(right - left);
- x2 = left + goldenRatio2*(right - left);
- if (func(x1) >= func(x2))
- left = x1;
- else
- right = x2;
- }
- answer = func((left + right) / 2);
- answerPoint = (left + right) / 2;
- return make_pair(answer, answerPoint);
- }
- int main()
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- pair<double, double> ans = solve();
- cout << "min = " << ans.first << "\nwith x = " << ans.second << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement