VB Differentiation 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 create differentiator objects for greater control
  over how numerical differentiation is performed.
  Module DifferentiationExample

    Private Function MyFunction(ByVal X As Double) As Double
      Return Math.Sin(Math.Sqrt(X + 1)) * (2 * Math.PI * X)
    End Function

    Sub Main()

      Console.WriteLine()

      As shown in the OneVariableFunctionExample, the Differentiate() method
      on OneVariableFunction computes the derivative of a function at a
      given x-value.
      Dim D As Func(Of Double, Double) = AddressOf MyFunction
      Dim F As OneVariableFunction = New OneVariableFunction(D)
      Console.Write("Derivative of f at Pi = ")
      Console.WriteLine(F.Differentiate(Math.PI))
      Console.WriteLine()

      To perform differentiation, every OneVariableFunction has an IDifferentiator
      object associated with it. NMath Core provides class RiddersDifferentiator,
      which computes the derivative of a given function at a given x-value by
      Ridders’ method of polynomial extrapolation, and implements the
      IDifferentiator interface.

      Extrapolations of higher and higher order are produced. Iteration stops when
      either the estimated error is less than a specified error tolerance, the error
      estimate is significantly worse than the previous order, or the maximum order
      is reached.

      To achieve more control over how differentiation is performed, you can
      instantiate your own RiddersDifferentiator.
      Dim Ridder As New RiddersDifferentiator()

      The Tolerance property gets and sets the error tolerance used in computing
      differentiations. 
      Ridder.Tolerance = 0.0000000001

      Maximum order gets and sets the maximum order. 
      Ridder.MaximumOrder = 20

      The Differentiate() method on RiddersDifferentiator accepts a
      OneVariableFunction and an x-value at which to differentiate.
      Console.Write("Derivative of f at Pi = ")
      Console.WriteLine(Ridder.Differentiate(F, Math.PI))
      Console.WriteLine()

      The ErrorEstimate property gets an estimate of the error of the derivative
      just computed.
      Console.Write("Estimated error = ")
      Console.WriteLine(Ridder.ErrorEstimate)
      Console.WriteLine()

      ToleranceMet gets a boolean value indicating whether or not the error
      estimate for the derivative approximation is less than the tolerance.
      If (Ridder.ToleranceMet) Then
        Console.WriteLine("The estimate is within the error tolerance.")
      Else
        Console.WriteLine("The estimate is NOT within the error tolerance.")
      End If
      Console.WriteLine()

      The Order property gets the order of the final polynomial extrapolation.
      Console.Write("Final order = ")
      Console.WriteLine(Ridder.Order)
      Console.WriteLine()

      The Tableau property gets a matrix of successive approximations produced
      while computing the derivative. Successive columns in the matrix contain
      higher orders of extrapolation successive rows decreasing step size.
      Console.WriteLine("Tableau...")
      Console.WriteLine(Ridder.Tableau.ToTabDelimited("F5"))
      Console.WriteLine()

      Setting the error tolerance to a value less than zero ensures that the
      Ridders differentiation is of the maximum order.
      Ridder.Tolerance = -1
      Console.Write("Derivative of f at Pi = ")
      Console.WriteLine(Ridder.Differentiate(F, Math.PI))
      Console.WriteLine()
      Console.Write("Final order = ")
      Console.WriteLine(Ridder.Order)

      Note that higher orders are not necessarily better. In most cases,
      therefore, youre better off letting the differentiator decide when to
      stop.

      Console.WriteLine()
      Console.WriteLine("Press Enter Key")
      Console.Read()

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top