Overload List
| # | Signature | Description |
|---|---|---|
| 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. |
| 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. |
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.
| # | Name | Type | Description |
|---|---|---|---|
| 1 | ax | Double | scalar |
| 2 | bx | Double | scalar |
| 3 | Func | TRealFunction | |
| 4 | FloatPrecision | TMtxFloatPrecision | |
| 5 | Consts | Double[] | |
| 6 | ObjConst | Object[] | |
| 7 | RootX | Double (ref) | output |
| 8 | StopReason | TOptStopReason (ref) |
Returns: Int32
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.
| # | Name | Description |
|---|---|---|
| 1 | ax | Defines the lower end of the bracket enclosing the root. |
| 2 | bx | Defines the upper end of the bracket enclosing the root. |
| 3 | Func | Real 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. |
| 4 | Consts | Additional Func constant parameters (can be/is usually nil). |
| 5 | ObjConst | Additional Func constant parameters (can be/is usually nil). |
| 6 | RootX | Returns the position of the function root. Returns NAN, if no root was bracketed. |
| 7 | StopReason | Returns the reason why the root search stopped (see Dew.Math.TOptStopReason). |
| 8 | MaxIter | Maximum allowed numer of root search iterations. |
| 9 | Accuracy | Desired root position tolerance. Pass zero to iterate to full machine precision. |
| 10 | Verbose | If 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. |
| 11 | FloatPrecision | Specifies 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.
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.
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
}