From 00dda54f4f1f52ad497747a2316309cc2d0cbf05 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Sun, 21 Jun 2020 17:12:18 -0400 Subject: [PATCH] ensure search window is increasing and greater than tolerance --- numerical_methods/golden_search_extrema.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/numerical_methods/golden_search_extrema.cpp b/numerical_methods/golden_search_extrema.cpp index 5a327c1ef..5d84c5572 100644 --- a/numerical_methods/golden_search_extrema.cpp +++ b/numerical_methods/golden_search_extrema.cpp @@ -31,6 +31,14 @@ double get_minima(const std::function &f, double lim_a, double c, d; double prev_mean, mean = std::numeric_limits::infinity(); + // ensure that lim_a < lim_b + if (lim_a > lim_b) { + std::swap(lim_a, lim_b); + } else if (std::abs(lim_a - lim_b) <= EPSILON) { + std::cerr << "Search range must be greater than " << EPSILON << "\n"; + return lim_a; + } + do { prev_mean = mean;