Optimization.RootBrent Method

Overload List

#SignatureDescription
1Int32 RootBrent(Double ax, Double bx, TRealFunction Func, TMtxFloatPrecision FloatPrecision, Double[] Consts, Object[] ObjConst, ref Double RootX, ref TOptStopReason StopReason)Finds a root of single variable function by using default settings and no log.
2Int32 RootBrent(Double ax, Double bx, TRealFunction Func, TMtxFloatPrecision FloatPrecision, Double[] Consts, Object[] ObjConst, ref Double RootX, ref TOptStopReason StopReason, Int32 MaxIter, Double Accuracy, TStrings Verbose)Finds a root (zero) of a single variable function.

Overload 1: Int32 RootBrent(Double ax, Double bx, TRealFunction Func, TMtxFloatPrecision FloatPrecision, Double[] Consts, Object[] ObjConst, ref Double RootX, ref TOptStopReason StopReason)

Finds a root of single variable function by using default settings and no log.

#NameTypeDescription
1axDoublescalar
2bxDoublescalar
3FuncTRealFunction
4FloatPrecisionTMtxFloatPrecision
5ConstsDouble[]
6ObjConstObject[]
7RootXDouble (ref)output
8StopReasonTOptStopReason (ref)

Returns: Int32

Remarks:

Use this version if algorithm step logs are not needed.

Overload 2: Int32 RootBrent(Double ax, Double bx, TRealFunction Func, TMtxFloatPrecision FloatPrecision, Double[] Consts, Object[] ObjConst, ref Double RootX, ref TOptStopReason StopReason, Int32 MaxIter, Double Accuracy, TStrings Verbose)

Finds a root (zero) of a single variable function.

#NameDescription
1axDefines the lower end of the bracket enclosing the root.
2bxDefines the upper end of the bracket enclosing the root.
3FuncReal function of single variable (must be of Dew.Math.TRealFunction type) whose root is to be found. The function must return the SIGNED value f(x) and not its absolute value: the algorithm locates the sign change.
4ConstsAdditional Func constant parameters (can be/is usually nil).
5ObjConstAdditional Func constant parameters (can be/is usually nil).
6RootXReturns the position of the function root. Returns NAN, if no root was bracketed.
7StopReasonReturns the reason why the root search stopped (see Dew.Math.TOptStopReason).
8MaxIterMaximum allowed numer of root search iterations.
9AccuracyDesired root position tolerance. Pass zero to iterate to full machine precision.
10VerboseIf assigned, stores Func, evaluated at each iteration step. Optionally, you can also pass Dew.Math.TOptControl object to the Verbose parameter. This allows the root search to be interrupted from another thread and optionally also allows logging and iteration count monitoring.
11FloatPrecisionSpecifies the floating point precision to be used by the routine.

Returns: Int32 - the number of iterations required to reach the solution(root) within given tolerance.

Remarks:

What it computes. Locates a root x^* of a real function of
one variable on the bracket [a_x,b_x] using Brent's method (the algol-60 zeroin: inverse quadratic interpolation and the secant rule, each step guarded by bisection). For the example objective

f(x) = x^3 - 2x - 5

Domain. a_x,b_x are finite reals; if b_x < a_x they are
swapped internally, so the order of the bracket does not matter. Func must change sign across the bracket, that is f(a_x) * f(b_x) <= 0. MaxIter > 0 caps the iterations; Accuracy ≥ 0 is the desired position tolerance.

Defined behaviour. Returns the iteration count and writes the root
to RootX. The convergence floor is 2 epsilon |x| + Accuracy/2, so Accuracy = 0 iterates to full machine precision - unlike Dew.Math.Units.Optimization.MinBrent, whose floor is sqrt(epsilon) |x| because a smooth minimum is numerically flat within that distance and a sign change is not. StopReason is Dew.Math.TOptStopReason.OptResConverged when the bracket shrank below the tolerance or the function returned an exact zero, Dew.Math.TOptStopReason.OptResNotFound when Func does not change sign across the bracket (no root is enclosed, and RootX is NAN),

Dew.Math.TOptStopReason.OptResMaxIter when the iteration cap was hit,
Dew.Math.TOptStopReason.optNANValue if the objective returned NaN, and
Dew.Math.TOptStopReason.optStopped if the search was interrupted through
Dew.Math.TOptControl. No exception is raised for a missing bracket: the

result is reported through StopReason.

Finds the root of a function of one variable. This routine uses a slightly modified version of the algol 60 procedure zero, introduced by Richard Brent.

Examples
private double Fun(TVec pars, TVec c, params object[] o)
{
    return pars[0]*pars[0]*pars[0] - 2*pars[0] - 5;
        // note that Pars holds only one variable !
}
private void Example()
{
    // the sign change of f is enclosed by [2,3]
    double x;
    TOptStopReason stop;
    int iter = Optimization.RootBrent(2,3,Fun,TMtxFloatPrecsion.mvDouble,null,null,out x,out stop,500,0.0,null);
    // stop if iter >500 or the bracket is below machine precision
        // Returns x = 2.0945514815423265, stop = OptResConverged
}
See Also: TRealFunction, Optimization.MinBrent