VB One Variable Function 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 and manipulate functions of one variable.
  Module OneVariableFunctionExample

    Private Function MyFunction(ByVal X As Double) As Double
      Return Math.Sin(X) + Math.Pow(X, 3) / Math.PI
    End Function

    Sub Main()

      Console.WriteLine()

      Class OneVariableFunction encapsulates an arbitrary function, and
      works with other numerical classes to approximate integrals and
      derivatives. 

      A OneVariableFunction is constructed from an Func(Of Double, Double),
      a function delegate that takes a single double parameter and returns a double.
      For example, to encapsulate MyFunction (see above):
      Dim D As Func(Of Double, Double) = AddressOf MyFunction
      Dim F As New OneVariableFunction(D)

      The Evaluate() method evaluates a function at a given x-value.
      Console.Write("f(pi) = ")
      Console.WriteLine(F.Evaluate(Math.PI))
      Console.WriteLine()

      Evaluate() also accepts a vector of x-values, and returns a vector of
      y-values, such that y[i] = f( x[i] ). This code evaluates f at 10 points
      between 0 and 1:
      Dim X As New DoubleVector(10, 0, 1.0 / 10)
      Dim Y As DoubleVector = F.Evaluate(X)
      Console.WriteLine("x...")
      Console.WriteLine(X)
      Console.WriteLine()

      Console.WriteLine("y...")
      Console.WriteLine(Y.ToString("F3"))
      Console.WriteLine()

      Class OneVariableFunction provides overloads of the arithmetic operators
      (and equivalent named methods) that work with either with two function
      objects, or with a function and a scalar. For example:
      Dim G As OneVariableFunction = (F + 1) / 2
      G = F * G
      Console.Write("g(pi) = ")
      Console.WriteLine(G.Evaluate(Math.PI))

      Evaluate() accepts another OneVariableFunction, and returns a new
      function encapsulating the composite, g( f(x) ).
      Dim Composite As OneVariableFunction = G.Evaluate(F)
      Console.Write("g( f(pi) ) = ")
      Console.WriteLine(Composite.Evaluate(Math.PI))
      Console.WriteLine()

      The Integrate() method approximates the integral of a function over a
      given interval.
      Dim Integral As Double = F.Integrate(0, Math.PI / 2)
      Console.Write("Integral of f from 0 to pi/2 = ")
      Console.WriteLine(Integral)
      Console.WriteLine()

      To perform integration, every OneVariableFunction has an IIntegrator
      object associated with it. NMath Core integration classes such as
      RombergIntegrator and GaussKronrodIntegrator implement the IIntegrator
      interface. The default integrator for a OneVariableFunction is an instance
      of RombergIntegrator, which may be changed using the Integrator property.
      F.Integrator = New GaussKronrodIntegrator()
      Integral = F.Integrate(0, Math.PI / 2)
      Console.Write("Integral of f from 0 to pi/2 using Gauss-Kronrod = ")
      Console.WriteLine(Integral)
      Console.WriteLine()

      The Differentiate() method computes the derivative of a function at
      a given x-value.
      Dim Derivative As Double = F.Differentiate(Math.PI / 2)
      Console.Write("Derivative of f at pi/2 = ")
      Console.WriteLine(Derivative)

      Derivative() returns a new function object encapsulating the first
      derivative of a function.
      Dim F1stDeriv As OneVariableFunction = F.Derivative()
      Dim F2ndDeriv As OneVariableFunction = F1stDeriv.Derivative()

      Console.Write("1st derivative of f at pi/2 = ")
      Console.WriteLine(F1stDeriv.Evaluate(Math.PI / 2))
      Console.Write("2nd derivative of f at pi/2 = ")
      Console.WriteLine(F2ndDeriv.Evaluate(Math.PI / 2))

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

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top