golden_search_extrema.cpp File Reference

Find extrema of a univariate real function in a given interval using golden section search algorithm. More...

`#include <cassert>`
`#include <cmath>`
`#include <functional>`
`#include <iostream>`
`#include <limits>`
Include dependency graph for golden_search_extrema.cpp:

## Macros

#define | _USE_MATH_DEFINES
#define | EPSILON 1e-7
| solution accuracy limit

## Functions

double | get_minima (const std::function< double(double)> &f, double lim_a, double lim_b)
| Get the minima of a function in the given interval. To get the maxima, simply negate the function. The golden ratio used here is: More...
void | test1 ()
| Test function to find minima for the function \(f(x)= (x-2)^2\) in the interval \([1,5]\) Expected result = 2.
void | test2 ()
| Test function to find maxima for the function \(f(x)= x^{\frac{1}{x}}\) in the interval \([-2,10]\) Expected result: \(e\approx 2.71828182845904509\).
void | test3 ()
| Test function to find maxima for the function \(f(x)= \cos x\) in the interval \([0,12]\) Expected result: \(\pi\approx 3.14159265358979312\).
int | main ()

## Detailed Description

Find extrema of a univariate real function in a given interval using golden section search algorithm.

See also
  brent_method_extrema.cpp

Author
  Krishna Vedala

## Function Documentation

## get_minima()

double get_minima | ( | const std::function< double(double)> & | f,
|  | double | lim_a,
|  | double | lim_b
| ) |  |

Get the minima of a function in the given interval. To get the maxima, simply negate the function. The golden ratio used here is:

\[ k=\frac{3-\sqrt{5}}{2} \approx 0.381966\ldots\]

Parameters

  f | function to get minima for
  lim_a | lower limit of search window
  lim_b | upper limit of search window

Returns
  local minima found in the interval

{
uint32_t iters = 0;
double c, d;
double prev_mean, mean = std::numeric_limits<double>::infinity();
// golden ratio value
const double M_GOLDEN_RATIO = (1.f + std::sqrt(5.f)) / 2.f;
// 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;
// compute the section ratio width
double ratio = (lim_b - lim_a) / M_GOLDEN_RATIO;
c = lim_b - ratio; // right-side section start
d = lim_a + ratio; // left-side section end
if (f(c) < f(d)) {
// select left section
lim_b = d;
} else {
// selct right section
lim_a = c;
}
mean = (lim_a + lim_b) / 2.f;
iters++;
// continue till the interval width is greater than sqrt(system epsilon)
} while (std::abs(lim_a - lim_b) > EPSILON);
std::cout << " (iters: " << iters << ") ";
return prev_mean;
}
std::cerr
EPSILON
#define EPSILON
solution accuracy limit
Definition: golden_search_extrema.cpp:17
std::numeric_limits::infinity
T infinity(T... args)
std::sqrt
T sqrt(T... args)
std::swap
T swap(T... args)
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function

{
std::cout.precision(9);
std::cout << "Computations performed with machine epsilon: " << EPSILON
<< "\n";
test1();
test2();
test3();
return 0;
}
test2
void test2()
Test function to find maxima for the function in the interval Expected result: .
Definition: golden_search_extrema.cpp:100
test1
void test1()
Test function to find minima for the function in the interval Expected result = 2.
Definition: golden_search_extrema.cpp:78
test3
void test3()
Test function to find maxima for the function in the interval Expected result: .
Definition: golden_search_extrema.cpp:123
Here is the call graph for this function: