# NMath User's Guide

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

NMath provides two classes that implement the interface, and minimize a using function evaluations and derivative calculations:

Class minimizes a multivariate function using the Polak-Ribiere variant of the Fletcher-Reeves conjugate gradient method. Gradients are calculated using the partial derivatives, then chosen based on a direction that is conjugate to the old gradient and, insofar as possible, to all previous directions traversed.

Class minimizes a multivariate function using the Broyden-Fletcher-Goldfarb-Shanno variable metric (or quasi-Newton) method. Variable metric methods are very similar to conjugate gradient methods—both calculate gradients using the partial derivatives. Storage is less efficient (order N2 storage, versus order a few times N), but since variable metric methods predate conjugate gradient methods, they are still widely used.

Like all NMath minimizers, instances of ConjugateGradientMinimizer and VariableMetricMinimizer are constructed by specifying an error tolerance and a maximum number of iterations, or by accepting the defaults for these values. For example, this code constructs a VariableMetricMinimizer using a tolerance or 10-5 and a maximum of 20 iterations:

Code Example – C# minimization

```double tol = 1e-5;
```
```int maxIter = 20;
```
```VariableMetricMinimizer minimizer =
```
```  new VariableMetricMinimizer( tol, maxIter );
```

Code Example – VB minimization

```Dim Tol As Double = "1e-5"
```
```Dim MaxIter As Integer = 20
```
```Dim Minimizer As New VariableMetricMinimizer(Tol, MaxIter)
```

Class ConjugateGradientMinimizer and VariableMetricMinimizer implement the IMultiVariableDMinimizer interface, which provides a single Minimize() method with the following signature:

Code Example – C# minimization

```DoubleVector Minimize( MultiVariableFunction f,
```
```                       MultiVariableFunction[] df,
```
```                       DoubleVector x );
```

Code Example – VB minimization

```Minimize(F As MultiVariableFunction,
```
```         DF As MultiVariableFunction(),
```
```         X as DoubleVector) As DoubleVector
```

where f is the function to minimize, df is an array of partial derivatives, and x is the start point.

For instance, given the following function and partial derivatives:

Code Example – C# minimization

```protected static double MyFunction( DoubleVector v )
```
```{
```
```  return ( ( v - 5.0 ) * ( v - 5.0 ) ) +
```
```         ( ( v + 3.0 ) * ( v + 3.0 ) );
```
```}

```

```protected static double MyFunctionDx( DoubleVector v )
```
```{
```
```  return ( 2 * v ) - 10;
```
```}

```

```protected static double MyFunctionDy( DoubleVector v )
```
```{
```
```  return ( 2 * v ) + 6;
```
```}
```

Code Example – VB minimization

```Protected Shared Function MyFunction(V As DoubleVector) As Double
```
```  Return ((V(0) - 5.0) * (V(0) - 5.0)) +
```
```         ((V(1) + 3.0) * (V(1) + 3.0))
```
```End Function

```

```Protected Shared Function Dx(V As DoubleVector) As Double
```
```  Return (2 * V(0)) - 10
```
```End Function

```

```Protected Shared Function Dy(V As DoubleVector) As Double
```
```  Return (2 * V(1)) + 6
```
```End Function
```

This code computes the minimum using a ConjugateGradientMinimizer, starting at the origin:

Code Example – C# minimization

```var function = new MultiVariableFunction(
```
```    new Func<DoubleVector, double>( MyFunction ) );

```

```var partialx = new MultiVariableFunction(
```
```  new Func<DoubleVector, double>( MyFunctionDx ) );
```
```var partialy = new MultiVariableFunction(
```
```  new Func<DoubleVector, double>( MyFunctionDy ) );
```
```var df = new MultiVariableFunction[] { partialx, partialy };

```

```var minimizer =  new ConjugateGradientMinimizer();
```
```var start = new DoubleVector( 2, 0 );
```
```DoubleVector min = minimizer.Minimize( f, df, start );
```

Code Example – VB minimization

```Dim MultiFunction As New MultiVariableFunction(
```
```  New Func(Of DoubleVector, Double)(AddressOf MyFunction))
```
```Dim PartialX As New MultiVariableFunction(
```
```  New Func(Of DoubleVector, Double)(AddressOf Dx))
```
```Dim PartialY As New MultiVariableFunction(
```
```  New Func(Of DoubleVector, Double)(AddressOf Dy))

```

```Dim Minimizer As New ConjugateGradientMinimizer()
```
```Dim Start As New DoubleVector(2, 0)
```
```Dim Min As DoubleVector = Minimizer.Minimize(F, DF, Start)

```

Top

Top