VB One Variable Minimization Example

← All NMath Code Examples

 

Imports System
Imports CenterSpace.NMath.Core


Namespace CenterSpace.NMath.Examples.VisualBasic

  A .NET example in Visual Basic showing how to find a minimum of a univariate function.
  Module OneVariableMinimizationExample

    Private Function df(ByVal x As Double) As Double
      Return 8 * Math.Cos(x) + 6 * x - 5
    End Function

    Private Function f(ByVal x As Double) As Double
      Return 8 * Math.Sin(x) + 3 * x * x - 5 * x
    End Function

    Sub Main()

      Create a one-variable function.
      Dim D As New Func(Of Double, Double)(AddressOf f)
      Dim Func As New OneVariableFunction(D)

      Use golden section search to find the minimum of the function
      in the interval -10000 to 10000.
      Dim Lower As Integer = -10000
      Dim Upper As Integer = 10000

      Create the minimizer with default tolerance and default maximum iterations.
      Dim Golden As New GoldenMinimizer

      Perform the minimization.
      Dim Min As Double = Golden.Minimize(Func, Lower, Upper)

      Console.WriteLine()

      Console.WriteLine("GoldenMinimizer found a minimum of " & Func.Evaluate(Min))
      Console.WriteLine("at " & Min & " in " & Golden.Iterations & " iterations.")

      If (Golden.ToleranceMet) Then
        Error is less than desired tolerance
        Console.Write("Error of " & Golden.Error & " is within tolerance of ")
        Console.WriteLine(Golden.Tolerance & ".")
      Else
        Minimization ended because maximum iterations was reached
        Console.WriteLine("Ended with maximum iterations.")
      End If

      Console.WriteLine()

      Try Brents Method for better efficiency.
      Dim Brent As New BrentMinimizer
      Min = Brent.Minimize(Func, Lower, Upper)

      Console.WriteLine("BrentMinimizer found a minimum of " & Func.Evaluate(Min))
      Console.WriteLine("at " & Min & " in " & Brent.Iterations & " iterations.")
      If (Brent.ToleranceMet) Then
        Error is less than desired tolerance
        Console.Write("Error of " & Brent.Error & " is within tolerance of ")
        Console.WriteLine(Brent.Tolerance & ".")
      Else
        Minimization ended because maximum iterations was reached
        Console.WriteLine("Ended with maximum iterations.")
      End If
      Console.WriteLine()

      If the derivative is known, we can utilize a better Brents
      algorithm.

      Create the derivative function.
      D = New Func(Of Double, Double)(AddressOf df)
      Dim Derivative As New OneVariableFunction(D)

      Instantiate a DBrentMinimizer.
      Dim DBRent As New DBrentMinimizer
      Min = DBRent.Minimize(Func, Derivative, Lower, Upper)

      Console.WriteLine("DBrentMinimizer found a minimum of " & Func.Evaluate(Min))
      Console.WriteLine("at " & Min & " in " & DBRent.Iterations & " iterations.")
      If (DBRent.ToleranceMet) Then
        Error is less than desired tolerance
        Console.Write("Error of " & DBRent.Error & " is within tolerance of ")
        Console.WriteLine(DBRent.Tolerance & ".")
      Else
        Minimization ended because maximum iterations was reached
        Console.WriteLine("Ended with maximum iterations.")
      End If
      Console.WriteLine()

      Increase the tolerance to 0.1
      DBRent.Tolerance = 0.1
      Min = DBRent.Minimize(Func, Derivative, Lower, Upper)

      Console.WriteLine("DBrentMinimizer found a minimum of " & Func.Evaluate(Min))
      Console.WriteLine("at " & Min & " in " & DBRent.Iterations & " iterations.")
      If (DBRent.ToleranceMet) Then
        Error is less than desired tolerance
        Console.Write("Error of " & DBRent.Error & " is within tolerance of ")
        Console.WriteLine(DBRent.Tolerance & ".")
      Else
        Minimization ended because maximum iterations was reached
        Console.WriteLine("Ended with maximum iterations.")
      End If
      Console.WriteLine()

      Decrease tolerance to 1e-13 and increase the interval.
      DBRent.Tolerance = 0.0000000000001
      Min = DBRent.Minimize(Func, Derivative, Lower * 100, Upper * 100)

      Console.WriteLine("DBrentMinimizer found a minimum of " & Func.Evaluate(Min))
      Console.WriteLine("at " & Min & " in " & DBRent.Iterations & " iterations.")
      If (DBRent.ToleranceMet) Then
        Console.Write("Error of " & DBRent.Error & " is within tolerance of ")
        Console.WriteLine(DBRent.Tolerance & ".")
      Else
        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
Top