Extreme Optimization > QuickStart Samples > Numerical Differentiation QuickStart Sample (VB.NET)

Extreme Optimization QuickStart Samples

Numerical Differentiation QuickStart Sample (VB.NET)

Illustrates the use of the NumericalDifferentiator class (Extreme.Mathematics.Calculus namespace) for numerical differentiation in Visual Basic .NET.

C# code Back to QuickStart Samples

' The numerical integration classes reside in the
' Extreme.Mathematics.Calculus namespace.
Imports Extreme.Mathematics.Calculus
' Function delegates reside in the Extreme.Mathematics
' namespace.
Imports Extreme.Mathematics

Namespace Extreme.Mathematics.QuickStart.VB
    ' Illustrates numerical differentiation using the
    ' NumericalDifferentiator class in the Extreme.Mathematics.Calculus 
    ' namespace of the Extreme Optimization Mathematics 
    ' Library for .NET.
    Module NumericalDifferentiation

        Sub Main()
            ' Numerical differentiation is a fairly simple
            ' procedure. Its accuracy is inherently limited
            ' because of unavoidable round-off error.
            '
            ' All calculations are performed by Shared methods
            ' of the NumericalDifferentiator class.
            Dim result As Double
            Dim estimatedError As Double

            '
            ' Standard numerical differentiation.
            '

            ' Central differences are the standard way of
            ' approximating the result of a function.
            ' For this to work, it must be possible to 
            ' evaluate the target function on both sides of
            ' the point where the numerical result is
            ' requested.
            '
            ' The target function must be provided as a 
            ' RealFunction. For more information about 
            ' this delegate, see the Functions 
            ' QuickStart Sample.
            Dim fCentral As RealFunction = _
                New RealFunction(AddressOf Math.Cos)
            Console.WriteLine("Central differences:")
            ' The actual calculation is performed by the
            ' CentralDerivative method.
            result = NumericalDifferentiator.CentralDerivative( _
                fCentral, 1)
            Console.WriteLine("  Result = {0}", result)
            Console.WriteLine("  Actual = {0}", -Math.Sin(1))
            ' This method is overloaded. It has an optional
            ' out parameter that returns an estimate for the
            ' error in the result.
            result = NumericalDifferentiator.CentralDerivative( _
                fCentral, 1, estimatedError)
            Console.WriteLine("Estimated error = {0}", _
                estimatedError)

            '
            ' Forward and backward differences.
            '

            ' Some functions are not defined everywhere.
            ' If the result is required on a boundary
            ' of the domain where it is defined, the central
            ' differences method breaks down. This also happens
            ' if the function has a discontinuity close to the
            ' differentiation point.
            ' 
            ' In these cases, either forward or backward 
            ' differences may be used instead.
            '
            ' The FForward function at the end of this file
            ' is an example of a function that may require
            ' forward differences. It is undefined for
            ' x < -2.
            Dim FForward As RealFunction = _
                New RealFunction(AddressOf FnForward)
            ' Calculating the derivative using central 
            ' differences returns NaN (Not a Number):
            result = NumericalDifferentiator.CentralDerivative( _
                FForward, -2, estimatedError)
            Console.WriteLine("  Derivative = {0}", result)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)
            ' Using the ForwardDerivative method does work:
            Console.WriteLine("Using forward differences:")
            result = NumericalDifferentiator.ForwardDerivative( _
                FForward, -2, estimatedError)
            Console.WriteLine("  Derivative = {0}", result)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)

            ' The FBackward function at the end of this file
            ' is an example of a function that requires
            ' backward differences for differentiation at
            ' x = 2.
            Dim fBackward As RealFunction = _
                New RealFunction(AddressOf FnBackward)
            Console.WriteLine("Using backward differences:")
            result = NumericalDifferentiator.BackwardDerivative( _
                fBackward, 2, estimatedError)
            Console.WriteLine("  Derivative = {0}", result)
            Console.WriteLine("  Estimated error = {0}", _
                estimatedError)

            '
            ' Derivative function
            '

            ' In some cases, it may be useful to have the
            ' derivative of a function in the form of a 
            ' RealFunction, so it can be passed as
            ' an argument to other methods. This is very
            ' easy to do.
            Console.WriteLine("Using delegates:")
            ' For central differences:
            Dim dfCentral As RealFunction = _
                NumericalDifferentiator.CreateDelegate(fCentral)
            Console.WriteLine("Central: f'(1) = {0}", _
                dfCentral(1))
            ' For forward differences:
            Dim dfForward As RealFunction = _
                NumericalDifferentiator.CreateForwardDelegate( _
                    FForward)
            Console.WriteLine("Forward: f'(-2) = {0}", _
                dfForward(-2))
            ' For backward differences:
            Dim dfBackward As RealFunction = _
                NumericalDifferentiator.CreateBackwardDelegate( _
                    fBackward)
            Console.WriteLine("Backward: f'(0) = {0}", _
                dfBackward(1))

            Console.Write("Press Enter key to exit...")
            Console.ReadLine()
        End Sub

        ' Function that requires the forward differences
        ' for numerical differentiation.
        Private Function FnForward(ByVal x As Double) As Double
            Return (x + 2) * (x + 2) * Math.Sqrt(x + 2)
        End Function

        ' Function that requires the backward differences
        ' for numerical differentiation.
        Private Function FnBackward(ByVal x As Double) As Double
            If (x > 0) Then
                Return 1
            Else
                Return Math.Sin(x)
            End If
        End Function

    End Module

End Namespace
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army