Extreme Optimization > QuickStart Samples > Basic Integration QuickStart Sample (VB.NET)

Extreme Optimization QuickStart Samples

Basic Integration QuickStart Sample (VB.NET)

Illustrates the basic use of the numerical integration (quadrature) classes (Extreme.Mathematics.Calculus namespace) in Visual Basic .NET.

C# code F# 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 use of the Newton-Raphson equation solver 
    ' in the Extreme.Mathematics.EquationSolvers namespace.
    Module BasicIntegration

        Sub Main()
            ' Numerical integration algorithms fall into two
            ' main categories: adaptive and non-adaptive.
            ' This QuickStart Sample illustrates the use of
            ' the non-adaptive numerical integrators.
            '
            ' 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.
            Dim f As RealFunction = _
                New RealFunction(AddressOf Math.Sin)
            ' Variable to hold the result:
            Dim result As Double

            '
            ' SimpsonIntegrator
            ' 

            ' The simplest numerical integration algorithm
            ' is Simpson's rule. 
            Dim simpson As SimpsonIntegrator = _
                New SimpsonIntegrator()
            ' You can set the relative or absolute tolerance
            ' to which to evaluate the integral.
            simpson.RelativeTolerance = 0.00001
            ' You can select the type of tolerance using the
            ' ConvergenceCriterion property:
            simpson.ConvergenceCriterion = _
                ConvergenceCriterion.WithinRelativeTolerance
            ' The Integrate method performs the actual 
            ' integration:
            result = simpson.Integrate(f, 0, 5)
            Console.WriteLine("sin(x) on [0,2]")
            Console.WriteLine("Simpson integrator:")
            ' The result is also available in the Result 
            ' property:
            Console.WriteLine("  Value: {0}", simpson.Result)
            ' To see whether the algorithm ended normally,
            ' inspect the Status property:
            Console.WriteLine("  Status: {0}", _
                simpson.Status)
            ' You can find out the estimated error of the result
            ' through the EstimatedError property:
            Console.WriteLine("  Estimated error: {0}", _
                simpson.EstimatedError)
            ' The number of iterations to achieve the result
            ' is available through the IterationsNeeded property.
            Console.WriteLine("  Iterations: {0}", _
                simpson.IterationsNeeded)
            ' The number of function evaluations is available 
            ' through the FunctionEvaluationsNeeded property.
            Console.WriteLine("  Function evaluations: {0}", _
                simpson.FunctionEvaluationsNeeded)

            '
            ' Gauss-Kronrod Integration
            '

            ' Gauss-Kronrod integrators also use a fixed point 
            ' scheme, but with certain optimizations in the 
            ' choice of points where the integrand is evaluated.
            '
            ' The Extreme.Mathematics.Calculus namespace contains a series
            ' of Gauss-Kronrod integrators that are mainly used
            ' as an integration rule for the adaptive 
            ' integrator. No iteration is used.
            ' Here's the 21-point rule:
            Dim gk21 As GaussKronrodIntegrator21 = _
                New GaussKronrodIntegrator21()
            gk21.Integrate(f, 0, 5)
            Console.WriteLine("21 point Gauss-Kronrod rule:")
            Console.WriteLine("  Value: {0}", gk21.Result)
            Console.WriteLine("  Status: {0}", _
                gk21.Status)
            Console.WriteLine("  Estimated error: {0}", _
                gk21.EstimatedError)
            Console.WriteLine("  Iterations: {0}", _
                gk21.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                gk21.FunctionEvaluationsNeeded)

            ' The NonAdaptiveGaussKronrodIntegrator uses a
            ' succession of 10, 21, 43, and 87 point rules
            ' to approximate the integral.
            Dim nagk As NonAdaptiveGaussKronrodIntegrator = _
                New NonAdaptiveGaussKronrodIntegrator()
            nagk.Integrate(f, 0, 5)
            Console.WriteLine("Non-adaptive Gauss-Kronrod rule:")
            Console.WriteLine("  Value: {0}", nagk.Result)
            Console.WriteLine("  Status: {0}", _
                nagk.Status)
            Console.WriteLine("  Estimated error: {0}", _
                nagk.EstimatedError)
            Console.WriteLine("  Iterations: {0}", _
                nagk.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                nagk.FunctionEvaluationsNeeded)

            '
            ' Romberg Integration
            '

            ' Romberg integration combines Simpson's Rule
            ' with a scheme to accelerate convergence.
            ' This algorithm is useful for smooth integrands.
            Dim romberg As RombergIntegrator = _
                New RombergIntegrator()
            result = romberg.Integrate(f, 0, 5)
            Console.WriteLine("Romberg integration:")
            Console.WriteLine("  Value: {0}", romberg.Result)
            Console.WriteLine("  Status: {0}", _
                romberg.Status)
            Console.WriteLine("  Estimated error: {0}", _
                romberg.EstimatedError)
            Console.WriteLine("  Iterations: {0}", _
                romberg.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                romberg.FunctionEvaluationsNeeded)

            ' However, it breaks down if the integration
            ' algorithm contains singularities or 
            ' discontinuities.
            f = New RealFunction(AddressOf HardIntegrand)
            result = romberg.Integrate(f, 0, 1)
            Console.WriteLine("Romberg on hard integrand:")
            Console.WriteLine("  Value: {0}", romberg.Result)
            Console.WriteLine("  Actual value: 100")
            Console.WriteLine("  Status: {0}", _
                romberg.Status)
            Console.WriteLine("  Estimated error: {0}", _
                romberg.EstimatedError)
            Console.WriteLine("  Iterations: {0}", _
                romberg.IterationsNeeded)
            Console.WriteLine("  Function evaluations: {0}", _
                romberg.FunctionEvaluationsNeeded)

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

        ' Function that will cause difficulties to the
        ' simplistic integration algorithms.
        Private Function HardIntegrand(ByVal x As Double) As Double
            ' This is put in because some integration rules
            ' evaluate the function at x=0.
            If (x <= 0) Then
                Return 0
            End If
            Return Math.Pow(x, -0.9) * Math.Log(1 / x)
        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