A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/db/d01/brent__method__extrema_8cpp_source.html below:

TheAlgorithms/C++: numerical_methods/brent_method_extrema.cpp Source File

Go to the documentation of this file. 16#define _USE_MATH_DEFINES 26 std::numeric_limits<double>::epsilon()) 36double get_minima

(

const

std::function<

double

(

double

)> &f,

double

lim_a,

41

std::swap(lim_a, lim_b);

42

}

else if

(std::abs(lim_a - lim_b) <= EPSILON) {

43

std::cerr <<

"Search range must be greater than "

<< EPSILON <<

"\n"

;

48 const double

M_GOLDEN_RATIO = (3.f - std::sqrt(5.f)) / 2.f;

50 double

v = lim_a + M_GOLDEN_RATIO * (lim_b - lim_a);

51 double

u, w = v, x = v;

53 double

fw = fv, fx = fv;

55 double

mid_point = (lim_a + lim_b) / 2.f;

56 double

p = 0, q = 0, r = 0;

59 double

tolerance, tolerance2;

62

mid_point = (lim_a + lim_b) / 2.f;

63

tolerance = EPSILON * std::abs(x);

64

tolerance2 = 2 * tolerance;

66 if

(std::abs(e) > tolerance2) {

68

r = (x - w) * (fx - fv);

69

q = (x - v) * (fx - fw);

70

p = (x - v) * q - (x - w) * r;

80 if

(std::abs(p) < std::abs(0.5 * q * r) && p < q * (lim_b - x)) {

84 if

(u - lim_a < tolerance2 || lim_b - u < tolerance2)

85

d = x < mid_point ? tolerance : -tolerance;

88

e = (x < mid_point ? lim_b : lim_a) - x;

89

d = M_GOLDEN_RATIO * e;

93 if

(std::abs(d) >= tolerance)

119 if

(fu <= fw || x == w) {

124

}

else if

(fu <= fv || v == x || v == w) {

131

}

while

(std::abs(x - mid_point) > (tolerance - (lim_b - lim_a) / 2.f));

133

std::cout <<

" (iters: "

<< iters <<

") "

;

146

std::function<double(

double

)> f1 = [](

double

x) {

147 return

(x - 2) * (x - 2);

150

std::cout <<

"Test 1.... "

;

154

std::cout << minima <<

"..."

;

156

assert(std::abs(minima - 2) < EPSILON);

157

std::cout <<

"passed\n"

;

169

std::function<double(

double

)> func = [](

double

x) {

170 return

-std::pow(x, 1.f / x);

173

std::cout <<

"Test 2.... "

;

177

std::cout << minima <<

" ("

<< M_E <<

")..."

;

179

assert(std::abs(minima - M_E) < EPSILON);

180

std::cout <<

"passed\n"

;

192

std::function<double(

double

)> func = [](

double

x) {

return

std::cos(x); };

194

std::cout <<

"Test 3.... "

;

198

std::cout << minima <<

" ("

<< M_PI <<

")..."

;

200

assert(std::abs(minima - M_PI) < EPSILON);

201

std::cout <<

"passed\n"

;

206

std::cout.precision(18);

208

std::cout <<

"Computations performed with machine epsilon: "

<< EPSILON

void test2()

Test function to find root for the function in the interval Expected result: .

void test1()

Test function to find root for the function in the interval Expected result = 2.

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 test3()

Test function to find maxima for the function in the interval Expected result: .


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4