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 h = 0.001;
- double x = -1;
- if (x == 0)
- x += 0.1;
- while ((func(x + h) - 2 * func(x) + func(x - h)) / (h*h) <= 0)
- x += 0.1;
- double x1;
- x1 = x - 0.5*h*(func(x + h) - func(x - h)) / (func(x + h) - 2 * func(x) + func(x - h));
- while (fabs(x1 - x) > EPS)
- {
- x = x1;
- x1 = x - 0.5*h*(func(x + h) - func(x - h)) / (func(x + h) - 2 * func(x) + func(x - h));
- }
- return { x1, func(x1) };
- }
- 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