![]() |
TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Find real extrema of a univariate real function in a given interval using Brent's method. 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 |
| required for MS Visual C++ | |
| #define | EPSILON |
| system accuracy limit | |
Functions | |
| 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 | test1 () |
| Test function to find root for the function \(f(x)= (x-2)^2\) in the interval \([1,5]\) Expected result = 2. | |
| void | test2 () |
| Test function to find root 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 real extrema of a univariate real function in a given interval using Brent's method.
Refer the algorithm discoverer's publication online and also associated book:
R. P. Brent, Algorithms for Minimization without Derivatives, Prentice-Hall, Englewood Cliffs, New Jersey, 1973
Definition in file brent_method_extrema.cpp.
| #define _USE_MATH_DEFINES |
required for MS Visual C++
Definition at line 16 of file brent_method_extrema.cpp.
| #define EPSILON |
system accuracy limit
Definition at line 24 of file brent_method_extrema.cpp.
| 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.
| f | function to get root for |
| lim_a | lower limit of search window |
| lim_b | upper limit of search window |
Definition at line 36 of file brent_method_extrema.cpp.
| int main | ( | void | ) |
Main function
Definition at line 205 of file brent_method_extrema.cpp.
| void test1 | ( | ) |
Test function to find root for the function \(f(x)= (x-2)^2\) in the interval \([1,5]\)
Expected result = 2.
Definition at line 144 of file brent_method_extrema.cpp.
| void test2 | ( | ) |
Test function to find root for the function \(f(x)= x^{\frac{1}{x}}\) in the interval \([-2,10]\)
Expected result: \(e\approx 2.71828182845904509\).
Definition at line 166 of file brent_method_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 189 of file brent_method_extrema.cpp.