NMath User's Guide

TOC |  Previous |  Next |  Index

24.3 Minimizing Derivable Functions (.NET, C#, CSharp, Visual Basic, VB.NET)

Class DBrentMinimizer implements the IOneVariableDMinimizer interface and minimizes a univariate function using Brent's Method in combination with evaluations of the first derivative. As described in Section 24.2, Brent's Method uses parabolic interpolation to fit a parabola through the current bracketing triplet, then uses the parabola to estimate the function's minimum. Class DBrentMinimizer uses the sign of the derivative at the central point of the bracketing triplet to decide which region should be used for the next test point.

Like GoldenMinimizer and BrentMinimizer (Section 24.2), instances of DBrentMinimizer are constructed by specifying an error tolerance and a maximum number of iterations, or by accepting the defaults for these values. This code constructs a DBrentMinimizer using the default error tolerance and maximum number of iterations:

DBrentMinimizer minimizer = new DBrentMinimizer();

This code uses an error tolerance of 10-4 and a maximum of 50 iterations:

double tol = 1e-4
int maxIter = 50;
DBrentMinimizer minimizer = new DBrentMinimizer( tol, maxIter );

Once you have constructed a DBrentMinimizer instance, you can use the Minimize() method to minimize a given function within a given interval. Overloads of Minimize() accept a bounding Interval, a Bracket, or a triplet of points satisfying the bracketing conditions (Section 24.1). Because DBrentMinimizer uses evaluations of the first derivative of the function, you must also supply a OneVariableFunction encapsulating the derivative. For example, the function:


has a minimum at 5.0. To compute the minimum, first encapsulate the function and its derivative:

public static double MyFunction( double x )
{
  return ( ( x - 5 ) * ( x - 5 ) );
}

public static double MyFunctionPrime( double x )
{
  return ( 2 * x ) - 10;
}

OneVariableFunction f = new OneVariableFunction(
  new NMathFunctions.DoubleUnaryFunction( MyFunction ) );
OneVariableFunction df = new OneVariableFunction(
  new NMathFunctions.DoubleUnaryFunction( MyFunctionPrime ) );

This code then constructs a Bracket starting from (1,2), and computes the minimum:

DBrentMinimizer minimizer = new DBrentMinimizer();
Bracket bracket = new Bracket( f, 1, 2 );
double min = minimizer.Minimize( bracket, df );

TOC |  Previous |  Next |  Index