← 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 integrator objects for greater control
over how numerical integration is performed.
Module IntegrationExample
Private Function MyFunction(ByVal X As Double) As Double
Return Math.Sqrt(X)
End Function
Sub Main()
Console.WriteLine()
Console.WriteLine("Known exact integral of f from 1 to 4 = 4.6666666...")
Console.WriteLine()
As shown in the OneVariableFunctionExample, the Integrate() method
on OneVariableFunction computes the integral of a function over a
given interval.
Dim D As Func(Of Double, Double) = AddressOf MyFunction
Dim F As New OneVariableFunction(D)
Console.Write("Estimate using default Romberg integrator = ")
Console.WriteLine(F.Integrate(1, 4))
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.
Instances of class RombergIntegrator compute successive Romberg
approximations of increasing order until the estimated error in the
approximation is less than a specified error tolerance, or until the
maximum order is reached. To achieve greater control over how integration
is performed, you can instantiate your own RombergIntegrator.
Dim Romberg As New RombergIntegrator()
The Tolerance property gets and sets the error tolerance used in computing
integrals. MaximumOrder gets and sets the maximum order.
Romberg.Tolerance = 0.0000000001
Romberg.MaximumOrder = 20
The Integrate() method on RombergIntegrator accepts a
OneVariableFunction and an interval over which to integrate.
Console.Write("Estimate using customized Romberg integrator = ")
Console.WriteLine(Romberg.Integrate(F, 1, 4))
After computing an estimate, a RombergIntegrator holds a record of
the iteration process. Read-only properties are provided for accessing
this information.
Console.Write("Order of estimate = ")
Console.WriteLine(Romberg.Order)
Console.Write("Error estimate = ")
Console.WriteLine(Romberg.RombergErrorEstimate)
ToleranceMet gets a boolean value indicating whether or not the error
estimate for the integral approximation is less than the tolerance.
if ( romberg.ToleranceMet )
Console.WriteLine("The estimate is within the error tolerance.")
Else
Console.WriteLine("The estimate is NOT within the error tolerance.")
End If
Console.WriteLine()
Tableau gets the entire matrix of successive approximations
computed while computing a Romberg estimate. The rows are the order
of approximation. The columns are the level of approximation.
The first column contains the trapezoidal approximations,
the second column the Simpsons rule approximations, the third
column the Booles rule approximations, and so on, up to the Order
of the approximation just computed.
Console.WriteLine("Romberg Tableau...")
Console.WriteLine(Romberg.Tableau.ToTabDelimited("F5"))
Console.WriteLine()
The automatic GaussKronrodIntegrator class uses Gauss-Kronrod rules with
increasing number of points. Approximation ends when the estimated error
is less than a specified error tolerance, or when the maximum number of
points is reached. The Gauss-Kronrod method is especially suited for
non-singular oscillating integrands.
Dim GK As New GaussKronrodIntegrator()
Console.Write("Estimate using Gauss-Kronrod automatic integrator = ")
Console.WriteLine(GK.Integrate(F, 1, 4))
NMath Core also includes Gauss-Kronrod classes for different numbers of
Kronrod points (2n+1, beginning with a Gauss 10-point rule). For instance,
GaussKronrod21Integrator approximates integrals using the Gauss 10-point
and the Kronrod 21-point rule.
Dim GK21 As New GaussKronrod21Integrator()
Console.Write("Estimate using Gauss-Kronrod 21 integrator = ")
Console.WriteLine(GK21.Integrate(F, 1, 4))
Console.WriteLine()
Console.WriteLine("Press Enter Key")
Console.Read()
End Sub
End Module
End Namespace
← All NMath Code Examples