Extreme Optimization >
QuickStart Samples >
Quadratic Curve QuickStart Sample (VB.NET)
Extreme Optimization QuickStart Samples
Quadratic Curve QuickStart Sample (VB.NET)
Illustrates the use of the Quadratic class (Extreme.Mathematics.Curves
namespace) in Visual Basic .NET.
C# code Back to
QuickStart Samples
' The Point structure and Quadratic class reside in the
' Extreme.Mathematics.Curves namespace.
Imports Extreme.Mathematics.Curves
Namespace Extreme.Mathematics.QuickStart.VB
Module QuadraticCurve
' Illustrates the use of the Point structure and the Quadratic
' class in the Extreme.Mathematics.Curve namespace of the
' Extreme Optimization Mathematics Library for .NET.
Sub Main()
' All curves inherit from the Curve abstract base
' class. The Quadratic class overrides implements all
' the methods and properties of the Curve class,
' and adds a few more.
'
' In a few places, we will need the Point structure.
' It simply containts an X and a Y component, much
' like the PointF structure in the System.Drawing
' namespace.
'
' Let's create some points:
Dim point1 As Point = New Point(-1, -4)
Dim point3 As Point = New Point(0, -3)
Dim point2 As Point = New Point(3, 12)
'
' Quadratic constructors
'
' The Quadratic class has multiple constructors. Each
' constructor derives from a different way to define
' a quadratic curve or parabola.
' 1st option: a quadratic curve through 3 points.
Dim quadratic1 As Quadratic = New Quadratic( _
point1, point2, point3)
' 2nd option: same as above, but we use the x and
' y coordinates of the points directly.
Dim quadratic2 As Quadratic = New Quadratic( _
point1.x, point1.y, point2.x, point2.y, point3.x, point3.y)
' 3rd option: give the three coefficients, a, b, and c
' of thequadratic form ax^2+bx+c.
Dim quadratic3 As Quadratic = New Quadratic(1, 2, 1)
'
' Curve Parameters
'
' The shape of any curve is determined by a set of parameters.
' These parameters can be retrieved and set through the
' Parameters collection. The number of parameters for a curve
' is given by this collection's Count property.
'
' Quadratic curves have three parameters.
Console.WriteLine("quadratic1.Parameters.Count = {0}", _
quadratic1.Parameters.Count)
' Parameters can easily be retrieved:
Console.WriteLine("quadratic1 parameters {0}, {1}, {2}", _
quadratic1.Parameters(0) = , quadratic1.Parameters(1) = , _
quadratic1.Parameters(2)
' We can see that quadratic2 defines the same quadratic
' curve as quadratic1, but quadratic3 is different:
Console.WriteLine("quadratic2 parameters {0}, {1}, {2}", _
quadratic2.Parameters(0) = , quadratic2.Parameters(1) = , _
quadratic2.Parameters(2)
Console.WriteLine("quadratic3 parameters {0}, {1}, {2}", _
quadratic3.Parameters(0) = , quadratic3.Parameters(1) = , _
quadratic3.Parameters(2)
' Parameters can also be set:
quadratic3.Parameters(0) = 1
'
' Curve Methods
'
' The ValueAt method returns the y value of the
' curve at the specified x value:
Console.WriteLine("quadratic1.ValueAt(2) = {0}", _
quadratic1.ValueAt(2))
' The SlopeAt method returns the slope of the curve
' a the specified x value:
Console.WriteLine("quadratic1.SlopeAt(2) = {0}", _
quadratic1.SlopeAt(2))
' You can also create a new curve that is the
' derivative of the original:
Dim derivative As Curve = quadratic1.GetDerivative()
Console.WriteLine("Slope at 2 (derivative) = {0}", _
derivative.ValueAt(2))
' For a quadratic curve, the derivative is a Line:
Console.WriteLine("Type of derivative: {0}", _
derivative.GetType().ToString())
Console.WriteLine("Derivative parameters:")
Console.WriteLine(" Y-intercept = {0}", _
derivative.Parameters(0)
Console.WriteLine(" Slope = {0}", _
derivative.Parameters(1)
' You can get a Line that is the tangent to a curve
' at a specified x value using the TangentAt method:
Dim tangent As Line = quadratic1.TangentAt(2)
Console.WriteLine("Tangent line at 2:")
Console.WriteLine(" Y-intercept = {0}", _
tangent.Parameters(0)
Console.WriteLine(" Slope = {0}", _
tangent.Parameters(1)
' For many curves, you can evaluate a definite
' integral exactly:
Console.WriteLine("Integral between 0 and 1 = {0}", _
quadratic1.Integral(0, 1))
' You can find the zeroes or roots of the curve
' by calling the FindRoots method:
Dim roots As Double() = quadratic1.FindRoots()
Console.WriteLine("quadratic1 has {0} roots:", _
roots.Length)
Console.WriteLine("Value of root 1 = {0}", roots(0))
Console.WriteLine("Value of root 2 = {0}", roots(1))
' quadratic3 has one double root at x = -1:
roots = quadratic3.FindRoots()
Console.WriteLine("Number of roots of quadratic3: {0}", _
roots.Length)
Console.WriteLine("Value of root = {0}", roots(0))
' Let's change quadratic3 so it has no real roots:
quadratic3.Parameters(0) = 2
roots = quadratic3.FindRoots()
Console.WriteLine("New quadratic3 has {0} roots.", _
roots.Length)
Console.Write("Press Enter key to exit...")
Console.ReadLine()
End Sub
End Module
End Namespace
Copyright 2004-2007,
Extreme Optimization. All rights reserved.
Extreme Optimization, Complexity made simple, M#, and M
Sharp are trademarks of ExoAnalytics Inc.
Microsoft, Visual C#, Visual Basic, Visual Studio, and Visual
Studio.NET are registered trademarks of Microsoft Corporation