Extreme Optimization > Mathematics Library for .NET > QuickStart Samples > AdvancedIntegration QuickStart Sample (VB.NET)

Extreme Optimization Mathematics Library for .NET

AdvancedIntegration QuickStart Sample (VB.NET)

Illustrates more advanced numerical integration using the AdaptiveIntegrator class (Extreme.Mathematics.Calculus namespace) 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 the more advanced use of the 
    ' AdaptiveGaussKronrodIntegrator numerical integrator class
    ' classes in the Extreme.Mathematics.Calculus namespace of the Extreme
    ' Optimization Mathematics Library for .NET.
    Module AdvancedIntegration

        Sub Main()
            ' Numerical integration algorithms fall into two
            ' main categories: adaptive and non-adaptive.
            ' This QuickStart Sample illustrates the use of
            ' the adaptive numerical integrator implemented by
            ' the AdaptiveIntegrator class. This class is the
            ' most advanced of the numerical integration 
            ' classes.
            '
            ' All numerical integration classes derive from
            ' NumericalIntegrator. This abstract base class
            ' defines properties and methods that are shared
            ' by all numerical integration classes.

            '
            ' The integrand
            '

            ' The function we are integrating must be
            ' provided as a RealFunction. For more
            ' information about this delegate, see the
            ' Functions QuickStart sample.
            '
            ' The functions used in this sample are defined at
            ' the end of this file.
            Dim f1 As RealFunction = _
                New RealFunction(AddressOf Integrand2)
            Dim f2 As RealFunction = _
                New RealFunction(AddressOf Integrand3)
            Dim f3 As RealFunction = _
                New RealFunction(AddressOf Integrand4)
            ' Variable to hold the result:
            Dim result As Double
            ' Construct an instance of the integrator class:
            Dim integrator As AdaptiveIntegrator = _
                New AdaptiveIntegrator()

            '
            ' Adaptive integrator basics
            '

            ' All the properties and methods defined by the
            ' NumericalIntegrator base class are available.
            ' See the BasicIntegration QuickStart Sample 
            ' for details. The AdaptiveIntegrator class defines
            ' the following additional properties:
            '
            ' The IntegrationRule property gets or sets the
            ' integration rule that is to be used for
            ' integrating subintervals. It can be any
            ' object derived from IntegrationRule.
            '
            ' For convenience, a series of Gauss-Kronrod
            ' integration rules of order 15, 21, 31, 41, 51, 
            ' and 61 have been provided.
            integrator.IntegrationRule = _
                New GaussKronrodIntegrator15()
            ' The UseExtrapolation property specifies whether
            ' precautions should be taken for singularities
            ' in the integration interval.
            integrator.UseExtrapolation = False
            ' Finally, the Singularities property allows you
            ' to specify singularities or discontinuities
            ' inside the integration interval. See the
            ' sample below for details.

            '
            ' Integration over infinite intervals
            ' 

            integrator.AbsoluteTolerance = 0.00000001
            integrator.ConvergenceCriterion = _
                ConvergenceCriterion.WithinAbsoluteTolerance
            ' The Integrate method performs the actual 
            ' integration. To integrate over an infinite
            ' interval, simply use either or both of
            ' Double.PositiveInfinity and 
            ' Double.NegativeInfinity as bounds:
            result = integrator.Integrate(f1, _
                Double.NegativeInfinity, Double.PositiveInfinity)
            Console.WriteLine("Exp(-x^2-x) on [-inf,inf]")
            Console.WriteLine("  Value: {0}", integrator.Result)
            ' To see whether the algorithm ended normally,
            ' inspect the Status property:
            Console.WriteLine("  Status: {0}", _
                integrator.Status)
            Console.WriteLine("  Estimated error: {0}", _
                integrator.EstimatedError)
            Console.WriteLine("  Iterations: {0}", _
                integrator.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                integrator.FunctionEvaluationsNeeded)

            '
            ' Functions with singularities at the end points
            ' of the integration interval.
            '

            ' Thanks to the adaptive nature of the algorithm,
            ' special measures can be taken to accelerate 
            ' convergence near singularities. To enable this
            ' acceleration, set the Singularities property
            ' to true.
            integrator.UseExtrapolation = True
            ' We'll use the function that gives the Romberg
            ' integrator in the BasicIntegration QuickStart
            ' sample trouble.
            integrator.Integrate(f2, 0, 1)
            Console.WriteLine("Singularities on boundary:")
            Console.WriteLine("  Value: {0}", integrator.Result)
            Console.WriteLine("  Exact value: 100")
            Console.WriteLine("  Status: {0}", _
                integrator.Status)
            Console.WriteLine("  Estimated error: {0}", _
                integrator.EstimatedError)
            ' Where Romberg integration failed after 1,000,000
            ' function evaluations, we find the correct answer 
            ' to within tolerance using only 135 function
            ' evaluations!
            Console.WriteLine("  Iterations: {0}", _
                integrator.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                integrator.FunctionEvaluationsNeeded)

            '
            ' Functions with singularities or discontinuities
            ' inside the interval.
            '
            integrator.UseExtrapolation = True
            ' We will pass an array containing the interior
            ' singularities to the integrator through the
            ' Singularities property:
            integrator.SetSingularities(1, Math.Sqrt(2))
            integrator.Integrate(f3, 0, 3)
            Console.WriteLine("Singularities inside the interval:")
            Console.WriteLine("  Value: {0}", integrator.Result)
            Console.WriteLine("  Exact value: 52.740748383471445")
            Console.WriteLine("  Status: {0}", _
                integrator.Status)
            Console.WriteLine("  Estimated error: {0}", _
                integrator.EstimatedError)
            Console.WriteLine("  Iterations: {0}", _
                integrator.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                integrator.FunctionEvaluationsNeeded)

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

        ' Function to integrate over [-inf, +inf]
        Private Function Integrand2(ByVal x As Double) As Double
            Return Math.Exp(-x - x * x)
        End Function

        ' Function with singularities on the boundaries
        ' of the integration interval
        Private Function Integrand3(ByVal x As Double) As Double
            Return Math.Pow(x, -0.9) * Math.Log(1 / x)
        End Function

        ' Function with singularities inside the interval,
        ' at 1 and Sqrt(2)
        Private Function Integrand4(ByVal x As Double) As Double
            Return x * x * x * _
                Math.Log(Math.Abs((x * x - 1) * (x * x - 2)))
        End Function

    End Module

End Namespace
Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
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