|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
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>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. More... | |
| 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\). More... | |
| 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\). More... | |
| int | main () |
Find extrema of a univariate real function in a given interval using golden section search algorithm.
| 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\]
| f | function to get minima for |
| lim_a | lower limit of search window |
| lim_b | upper limit of search window |
| int main | ( | void | ) |
Main function
| 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\).