Extreme Optimization > QuickStart Samples > Chebyshev Series QuickStart Sample (VB.NET)

Extreme Optimization QuickStart Samples

Chebyshev Series QuickStart Sample (VB.NET)

Illustrates approximating functions using the ChebyshevSeries class in the Extreme.Mathematics.Curves namespace in Visual Basic .NET.

C# code Back to QuickStart Samples

' The ChebyshevSeries class resides in the 
' Extreme.Mathematics.Curves namespace.
Imports Extreme.Mathematics.Curves
' The Constants class and RealFunction delegate reside in the 
' Extreme.Mathematics namespace.
Imports Extreme.Mathematics

Namespace Extreme.Mathematics.QuickStart.VB

    Module ChebyshevExpansions

        ' Illustrates the use of the ChebyshevSeries class 
        ' in the Extreme.Mathematics.Curve namespace.
        Sub Main()
            ' Chebyshev polynomials form an alternative basis
            ' for polynomials. A Chebyshev expansion is a 
            ' polynomial expressed as a sum of Chebyshev 
            ' polynomials.
            '
            ' Using the ChebyshevSeries class instead of 
            ' Polynomial can have two major advantages:
            '   1. They are numerically more stable. Higher
            '      accuracy is maintained even for large problems.
            '   2. When approximating other functions with
            '      polynomials, the coefficients in the
            '      Chebyshev expansion will tend to decrease
            '      in size, where those of the normal polynomial
            '      approximation will tend to oscillate wildly.

            ' Index variable.
            Dim index As Int32

            '
            ' Constructing Chebyshev expansions
            '

            ' Chebyshev expansions are defined over an interval.
            ' The first constructor requires you to specify the
            ' boundaries of the interval, and the coefficients
            ' of the expansion.
            Dim coefficients As Double() = New Double() {1, 0.5, -0.3, 0.1}
            Dim chebyshev1 As ChebyshevSeries = _
                New ChebyshevSeries(0, 2, coefficients)
            ' If you omit the boundaries, they are assumed to be
            ' -1 and +1:
            Dim chebyshev2 As ChebyshevSeries = _
                New ChebyshevSeries(coefficients)

            ' 
            ' Chebyshev approximations
            '

            ' A third constructor creates a Chebyshev 
            ' approximation to an arbitrary function. For more
            ' about the RealFunction delegate, see the
            ' Functions QuickStart Sample.
            '
            ' Chebyshev expansions allow us to obtain an 
            ' excellent approximation at minimal cost.
            '
            ' The following creates a Chebyshev approximation
            ' of degree 7 to Cos(x) over the interval (0, 2):
            Dim cos As RealFunction = _
                New RealFunction(AddressOf Math.Cos)
            Dim approximation1 As ChebyshevSeries = _
                ChebyshevSeries.GetInterpolatingPolynomial(cos, 0, 2, 7)
            Console.WriteLine("Chebyshev approximation of cos(x):")
            For index = 0 To 7
                Console.WriteLine("  c{0} = {1}", index, _
                    approximation1.Parameters[index])
            Next

            ' The largest errors are approximately at the
            ' zeroes of the Chebyshev polynomial of degree 8:
            For index = 0 To 8
                Dim zero As Double = _
                    1 + Math.Cos(index * Constants.Pi / 8)
                Dim err As Double = _
                    approximation1.ValueAt(zero) - Math.Cos(zero)
                Console.WriteLine(" Error {0} = {1}", index, err)
            Next

            '
            ' Least squares approximations
            '

            ' We will now calculate the least squares polynomial
            ' of degree 7 through 33 points.
            ' First, calculate the points:
            Dim xValues As Double() = New Double(32) {}
            Dim yValues As Double() = New Double(32) {}
            For index = 0 To 32
                Dim angle As Double = index * Constants.Pi / 32
                xValues(index) = 1 + Math.Cos(angle)
                yValues(index) = Math.Cos(xValues(index))
            Next
            ' Next, define a ChebyshevBasis object for the
            ' approximation we want: interval (0,2) and degree
            ' is 7.
            Dim basis As ChebyshevBasis = _
                New ChebyshevBasis(0, 2, 7)
            ' Now we can calculate the least squares fit:
            Dim approximation2 As ChebyshevSeries = _
                CType(basis.LeastSquaresFit(xValues, yValues, xValues.Length), _
                ChebyshevSeries)
            ' We can see it is close to the original 
            ' approximation we found earlier:
            For index = 0 To 7
                Console.WriteLine("  c{0} = {1}", index, _
                    approximation2.Parameters[index])
            Next

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

        End Sub

    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