VB Two Variable Integration 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 integrate over two variables.
  Module TwoVariableIntegrationExample

    Private Function F(ByVal V As DoubleVector) As Double
      Return (V(0) + 10) * (V(1) * V(1))
    End Function

    Private Function Y(ByVal X As Double) As Double
      Return (3 * X) + 5
    End Function

    The main entry point for the application.
    Sub Main()

      Integrate the function F over the area where 0 < x < 1 and 0 < y < 3

      First create the integrand
      Dim D As New Func(Of DoubleVector, Double)(AddressOf F)
      Dim Integrand As New MultiVariableFunction(D)

      Create the integrator with defaults
      Dim Integrator As New TwoVariableIntegrator

      Console.WriteLine()

      Console.WriteLine("Calculating the integral of ( x + 10 ) * y^2")
      Console.WriteLine("where 0 < x < 1 and 0 < y < 3")
      Console.WriteLine()

      Dim Integral As Double = Integrator.Integrate(Integrand, 0, 1, 0, 3)
      Console.WriteLine("integral is... " & Integral)
      Console.WriteLine()

      What if one of the bounds is not constant but a function? 
      Here the upper bound of y is 3x + 5.
      Dim D2 As Func(Of Double, Double)
      D2 = New Func(Of Double, Double)(AddressOf Y)
      Dim YUpper As New OneVariableFunction(D2)

      Integrate the function, F, over the area where 0 < x < 1 and 0 < y < ( 3x + 5 )
      Console.WriteLine("where 0 < x < 1 and 0 < y < 3x + 5")
      Console.WriteLine()

      Integral = Integrator.Integrate(Integrand, 0, 1, 0, YUpper)
      Console.WriteLine("integral is... " & Integral)
      Console.WriteLine()

      Console.WriteLine("switch to Romberg")
      Console.WriteLine()
      Integrator.DxIntegrator = New RombergIntegrator
      Integrator.DyIntegrator = New RombergIntegrator
      Integral = Integrator.Integrate(Integrand, 0, 1, 0, YUpper)
      Console.WriteLine("integral is... " & Integral)
      Console.WriteLine()

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

    End Sub

  End Module

End Namespace
← All NMath Code Examples
Top