(
conststd::function<
double(
double)> &f,
doublelim_a,
41std::swap(lim_a, lim_b);
42}
else if(std::abs(lim_a - lim_b) <= EPSILON) {
43std::cerr <<
"Search range must be greater than "<< EPSILON <<
"\n";
48 const doubleM_GOLDEN_RATIO = (3.f - std::sqrt(5.f)) / 2.f;
50 doublev = lim_a + M_GOLDEN_RATIO * (lim_b - lim_a);
51 doubleu, w = v, x = v;
53 doublefw = fv, fx = fv;
55 doublemid_point = (lim_a + lim_b) / 2.f;
56 doublep = 0, q = 0, r = 0;
59 doubletolerance, tolerance2;
62mid_point = (lim_a + lim_b) / 2.f;
63tolerance = EPSILON * std::abs(x);
64tolerance2 = 2 * tolerance;
66 if(std::abs(e) > tolerance2) {
68r = (x - w) * (fx - fv);
69q = (x - v) * (fx - fw);
70p = (x - v) * q - (x - w) * r;
80 if(std::abs(p) < std::abs(0.5 * q * r) && p < q * (lim_b - x)) {
84 if(u - lim_a < tolerance2 || lim_b - u < tolerance2)
85d = x < mid_point ? tolerance : -tolerance;
88e = (x < mid_point ? lim_b : lim_a) - x;
89d = M_GOLDEN_RATIO * e;
93 if(std::abs(d) >= tolerance)
119 if(fu <= fw || x == w) {
124}
else if(fu <= fv || v == x || v == w) {
131}
while(std::abs(x - mid_point) > (tolerance - (lim_b - lim_a) / 2.f));
133std::cout <<
" (iters: "<< iters <<
") ";
146std::function<double(
double)> f1 = [](
doublex) {
147 return(x - 2) * (x - 2);
150std::cout <<
"Test 1.... ";
154std::cout << minima <<
"...";
156assert(std::abs(minima - 2) < EPSILON);
157std::cout <<
"passed\n";
169std::function<double(
double)> func = [](
doublex) {
170 return-std::pow(x, 1.f / x);
173std::cout <<
"Test 2.... ";
177std::cout << minima <<
" ("<< M_E <<
")...";
179assert(std::abs(minima - M_E) < EPSILON);
180std::cout <<
"passed\n";
192std::function<double(
double)> func = [](
doublex) {
returnstd::cos(x); };
194std::cout <<
"Test 3.... ";
198std::cout << minima <<
" ("<< M_PI <<
")...";
200assert(std::abs(minima - M_PI) < EPSILON);
201std::cout <<
"passed\n";
206std::cout.precision(18);
208std::cout <<
"Computations performed with machine epsilon: "<< EPSILON
void test2()
Test function to find root for the function in the interval Expected result: .
void test1()
Test function to find root for the function in the interval Expected result = 2.
double get_minima(const std::function< double(double)> &f, double lim_a, double lim_b)
Get the real root of a function in the given interval.
void test3()
Test function to find maxima for the function in the interval Expected result: .
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4