Imports System Imports CenterSpace.NMath.Core Namespace CenterSpace.NMath.Examples.VisualBasic A .NET example in Visual Basic showing how to use the multivariable minimizer. Module MultiVariableMinimizationExample Private Function df0(ByVal X As DoubleVector) As Double Return 1.6 * Math.Pow(X(0), 3) End Function Private Function df1(ByVal X As DoubleVector) As Double Return 0.1 * Math.Cos(X(1)) End Function Private Function df2(ByVal X As DoubleVector) As Double Return 10 * X(2) End Function Private Function df3(ByVal X As DoubleVector) As Double Return 16 * Math.Pow(X(3), 3) End Function Private Function F(ByVal x As DoubleVector) As Double Return 0.4 * Math.Pow(x(0), 4) + 0.1 * Math.Sin(x(1)) + 5 * x(2) * x(2) + 4 * Math.Pow(x(3), 4) End Function Sub Main() Create a multivariable function. Dim D As Func(Of DoubleVector, Double) = AddressOf F Dim Func As New MultiVariableFunction(D) Start at ( 0, 0, 0, 0 ). Dim StartPoint As New DoubleVector(0.0, 0.0, 0.0, 0.0) Create a DownhillSimplexMinimizer with the default error tolerance and maximum iterations. Dim Simplex As New DownhillSimplexMinimizer Minimize the function. Dim Min As DoubleVector = Simplex.Minimize(Func, StartPoint) Console.WriteLine() Console.WriteLine("DownhillSimplexMinimizer found a minimum of " & Func.Evaluate(Min)) Console.WriteLine("at " & Min.ToString("G7") & ".") If (Simplex.ToleranceMet) Then Error is less than desired tolerance Console.Write("Error of " & Simplex.Error & " is within tolerance of ") Console.WriteLine(Simplex.Tolerance & ".") Else Minimization ended because maximum iterations was reached Console.WriteLine("Ended with maximum iterations.") End If Console.WriteLine() Increase the maximum number of iterations from the default of 100 to 1000. Simplex.MaxIterations = 1000 Minimize the function. Min = Simplex.Minimize(Func, StartPoint) Console.WriteLine("DownhillSimplexMinimizer found a minimum of " & Func.Evaluate(Min)) Console.WriteLine("at " & Min.ToString("G7") & ".") If (Simplex.ToleranceMet) Then Error is less than desired tolerance Console.Write("Error of " & Simplex.Error & " is within tolerance of ") Console.WriteLine(Simplex.Tolerance & ".") Else Minimization ended because maximum iterations was reached Console.WriteLine("Ended with maximum iterations.") End If Console.WriteLine() Now try using Powells Method. Create the minimizer with default error tolerance and default maximum iterations. Dim Powell As New PowellMinimizer Perform the minimization. Min = Powell.Minimize(Func, StartPoint) Console.WriteLine("PowellMinimizer found a minimum of " & Func.Evaluate(Min)) Console.WriteLine("at " & Min.ToString("G7") & ".") If (Powell.ToleranceMet) Then Error is less than desired tolerance Console.Write("Error of " & Powell.Error & " is within tolerance of ") Console.WriteLine(Powell.Tolerance & ".") Else Minimization ended because maximum iterations was reached Console.WriteLine("Ended with maximum iterations.") End If Console.WriteLine() If the derivatives are known, then use one of the IMultiVariableDMinimizer classes such as ConjugateGradientMinimizer. Create an array of partial derivatives Dim Partials(3) As MultiVariableFunction Partials(0) = New MultiVariableFunction(New Func(Of DoubleVector, Double)(AddressOf df0)) Partials(1) = New MultiVariableFunction(New Func(Of DoubleVector, Double)(AddressOf df1)) Partials(2) = New MultiVariableFunction(New Func(Of DoubleVector, Double)(AddressOf df2)) Partials(3) = New MultiVariableFunction(New Func(Of DoubleVector, Double)(AddressOf df3)) Create the minimizer with default tolerance and default maximum iterations. Dim CG As New ConjugateGradientMinimizer Perform the minimization. StartPoint = New DoubleVector(1.0, 2.0, 3.0, 4.0) Min = CG.Minimize(Func, Partials, StartPoint) Console.WriteLine("ConjugateGradientMinimizer found a minimum of " & Func.Evaluate(Min)) Console.WriteLine("at " & Min.ToString("G7") & ".") If (CG.ToleranceMet) Then Error is less than desired tolerance Console.Write("Error of " & CG.Error & " is within tolerance of ") Console.WriteLine(CG.Tolerance & ".") Else Minimization ended because maximum iterations was reached Console.WriteLine("Ended with maximum iterations.") End If Console.WriteLine() Console.WriteLine() Console.WriteLine("Press Enter Key") Console.Read() End Sub End Module End Namespace← All NMath Code Examples