I have forgotten

COST (GBP)
5.00
0.00
0

# Bracket

Given two initial points, this algorithm finds another point such that a bracketing triple is formed.
Controller: CodeCogs
Contents

C++

## Bracket

 voidbracket( double (*f)(double)[function pointer] double& a double& b double& c )
Given a user-defined function f and two distinct initial points a and b, this algorithm searches in the downhill direction (defined by the function as evaluated at the initial points) and returns a new point c such that the triplet $\inline&space;(a,&space;b,&space;c)$ brackets a minimum of the function. Explicitly, bracketing the minimum means finding three values a, b and c such that
$a&space;<&space;b&space;<&space;c&space;\qquad&space;\wedge&space;\squad&space;f(a)&space;>&space;f(b)&space;<&space;f(c)$

In order to achieve this, an initial guess is made using the golden section
$c&space;=&space;b&space;+&space;\phi&space;\cdot&space;(b&space;-&space;a)&space;\qquad&space;\phi&space;\approx&space;1.618034$

and then parabolic extrapolation is used as described below. Consider
$r&space;=&space;(b&space;-&space;a)&space;\left(&space;f(b)&space;-&space;f(c)&space;\right)&space;\qquad&space;q&space;=&space;(b&space;-&space;c)&space;\left(&space;f(b)&space;-&space;f(a)&space;\right)$

then the new extrapolated abscissa will be
$u&space;=&space;b&space;-&space;\mathrm{sign}(q&space;-&space;r)&space;\frac&space;{q(b&space;-&space;c)&space;-&space;r(b&space;-&space;a)}&space;{2&space;|\mathrm{max}(q&space;-&space;r,&space;\epsilon)|}$

If this does not create a new minimum point $\inline&space;\left(&space;u,&space;f(u)&space;\right)$, default golden section interval magnification is used
$u&space;=&space;c&space;+&space;\phi&space;\cdot&space;(c&space;-&space;b)$

The function takes four parameters.

### Example 1

#include <codecogs/maths/optimization/bracket.h>

#include <iostream>
#include <iomanip>
#include <cmath>

// user-defined function
double f(double x) {
return x * sin(x);
}

int main()
{
double a = 5, b = 4, c;
Maths::Optimization::bracket(f, a, b, c);

std::cout << "The bracketing triple is: " << std::endl << std::endl;
std::cout << "    a = 4" << std::endl << "    b = 5 " << std::endl;
std::cout << "    c = " << std::setprecision(10) << c << std::endl;
std::cout << std::endl;

std::cout << "The corresponding function values are: " << std::endl;
std::cout << std::endl;
std::cout << " f(a) = " << f(4) << std::endl << " f(b) = " << f(5);
std::cout << std::endl;
std::cout << " f(c) = " << f(c) << std::endl << std::endl;
return 0;
}

### Output

The bracketing triple is:

a = 4
b = 5
c = 6.618034

The corresponding function values are:

f(a) = -3.027209981
f(b) = -4.794621373
f(c) = 2.174859828

### Parameters

 f the user-defined function a the left endpoint of the initial interval b the right endpoint of the initial interval c the third point that will complete the bracketing triple

### Returns

The new values of a, b and c creating a bracketing triple.

### Authors

Lucian Bentea (August 2005)
##### Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.