TheAlgorithms/C++ 1.0.0
All the 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 <cstdint>
#include <functional>
#include <iostream>
#include <limits>
Go to the source code of this file.
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: | |
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 () |
Find extrema of a univariate real function in a given interval using golden section search algorithm.
Definition in file golden_search_extrema.cpp.
#define _USE_MATH_DEFINES |
Definition at line 10 of file golden_search_extrema.cpp.
#define EPSILON 1e-7 |
solution accuracy limit
Definition at line 18 of file golden_search_extrema.cpp.
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 |
Definition at line 30 of file golden_search_extrema.cpp.
int main | ( | void | ) |
Main function
Definition at line 140 of file golden_search_extrema.cpp.
void test1 | ( | ) |
Test function to find minima for the function \(f(x)= (x-2)^2\) in the interval \([1,5]\)
Expected result = 2.
Definition at line 79 of file golden_search_extrema.cpp.
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\).
Definition at line 101 of file golden_search_extrema.cpp.
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\).
Definition at line 124 of file golden_search_extrema.cpp.