VB Polynomial 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 create and manipulate polynomial objects.
  Module PolynomialExample

    Sub Main()

      Console.WriteLine()

    Module Polynomial represents a polynomial by its coefficients, arranged in
    ascending order—-that is, a vector of coefficients a0, a1, ... an such that
    f(x) = a0*x^0 + a1*X^1 + ... + an*x^n.

    A Polynomial instance can be constructed in two ways. If you know the
    exact form of the polynomial, simply pass in a vector of coefficients.
      Dim Coef As New DoubleVector("3 1 -2 0 5")
      Dim F As New Polynomial(Coef)

    f(x) = 5x^4 - 2x^2 + x + 3
      Console.WriteLine( "f(x) = {0}", f.ToString() )
      Console.WriteLine( "Degree = {0}", f.Degree )
      Console.WriteLine("Coefficients = {0}" & Environment.NewLine, F.Coeff)

    You can also interpolate a polynomial through a set of points. If the
    number of points is n, then the constructed polynomial will have degree
    n - 1 and pass through the interpolation points. For example, this code
    interpolates a polynomial through the points (1,6), (2,11), and (3,20):
      Dim X As New DoubleVector("1 2 3")
      Dim Y As New DoubleVector("6 11 20")
      Dim G As New Polynomial(X, Y)

    g(x) = 2x^2 - x + 5
      Console.WriteLine( "g(x) = {0}", g.ToString( "G3" ))
      Console.WriteLine( "Degree = {0}", g.Degree )
      Console.WriteLine("Coefficients = {0}" & Environment.NewLine, G.Coeff)

    The Evaluate() method evaluates a polynomial at a given x-value, or
    vector of x-values. This code evaluates f at ten points between 0 and 1:
      x = new DoubleVector( 10, 0.1, 1.0/10 )
      y = f.Evaluate( x )
      Console.WriteLine( "x = {0}", x )
      Console.WriteLine("y = {0}" & Environment.NewLine, Y)

    Module Polynomial provides overloads of the arithmetic operators (and 
    equivalent named methods) that work with either with two polynomials, or
    with a polynomial and a scalar. For example:
      Dim H As Polynomial = (F + G) * G / 2
      Console.WriteLine( "h(x) = {0}", h.ToString() )
      Console.WriteLine( "h(2) = {0}", h.Evaluate( 2 ) )
      Console.WriteLine()

    The Integrate() method computes the integral of a polynomial over a given
    interval. 
      Console.WriteLine( "Integral of h(x) over 0 to 1 = {0}", h.Integrate( 0, 1 ).ToString( "G3" ))
      Console.WriteLine()

    The AntiDerivative() method returns a new polynomial encapsulating
    the antiderivative (indefinite integral) of the current polynomial.
    The constant of integration is assumed to be zero.
      Dim hAntiDeriv As Polynomial = H.AntiDerivative()
      Console.WriteLine( "Antiderivative of h(x)..." )
      Console.WriteLine( hAntiDeriv.ToString( "G3" ))
      Console.WriteLine()

    The Differentiate() method computes the derivative of a polynomial at a
    given x-value.
      Console.WriteLine( "Derivative of h(x) at 1 = {0}", h.Differentiate( 1 ).ToString( "G3" ))
      Console.WriteLine()

    Derivative() returns a new polynomial that is the first derivative of
    the current polynomial.
      Console.WriteLine( "First derivative of h(x)..." )
      Console.WriteLine( h.Derivative().ToString( "G3" ))
      Console.WriteLine()

      Console.WriteLine( "Second derivative of h(x)..." )
      Console.WriteLine( h.Derivative().Derivative().ToString( "G3" ))
      Console.WriteLine()

    Check that the derivative of the antiderivative of h(x) == h(x)
      Console.WriteLine( "Derivative of the antiderivative of h(x)..." )
      Console.WriteLine( h.AntiDerivative().Derivative().ToString( "G3" ))

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

    End Sub
  End Module
End Namespace

← All NMath Code Examples
Top