Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <exception>
- double parab(double x1, double f1, double x2, double f2, double x3, double f3)
- {
- return x2 - ((x2 - x1) * (x2 - x1) * (f2 - f3) - (x2 - x3) * (x2 - x3) * (f2 - f1)) / (2 * ((x2 - x1) * (f2 - f3) - (x2 - x3) * (f2 - f1)));
- }
- double parabola(double a, double b, double (*f)(double x), int64_t n, double eps)
- {
- if (a > b)
- std::swap(a, b);
- double e = (a + b) * 0.5;
- double x = (a + b) * 0.5;
- double res = x;
- double incor = b + 1;
- double c, d;
- for (int64_t i = 0; i < n; ++i)
- {
- res = parab(a, f(a), e, f(e), b, f(b));
- if (!(res >= a && res <= b))
- throw std::invalid_argument("Can't find min");
- if (abs(res - x) < eps)
- return res;
- x = res;
- c = std::min(e, x);
- d = std::max(e, x);
- if (f(c) < f(d))
- {
- b = d;
- e = c;
- }
- else
- {
- a = c;
- e = d;
- }
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement