user2783004
user2783004

Reputation:

Use Math.NET library for plotting polynomial in vb.net

I have equations that I need to plot in VB.Net. I found this library, MATH.Net. It has a lot of examples with C#.

Can I use this library to plot polynomial functions in VB.NET?

If so, then how? As I couldn't find any examples on the Internet. If not, then can anyone please guide me on how to plot polynomials on a graph in VB.Net?

I'm using Microsoft Visual Studio 2010, and have very little experience in visual basic (I have made so simple applications like calculators, etc).

Upvotes: 2

Views: 2514

Answers (1)

Andrew Morton
Andrew Morton

Reputation: 25013

Generate some data points and add them to a series on the chart.

For example,

Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

    Function Polynom(x As Double) As Double
        Return Math.Pow(x, 3) + Math.Pow(x, 2) + x
    End Function

    Function Polynom2(x As Double) As Double
        Return 3 * Math.Pow(x, 2) + 2 * x + 1
    End Function

    Sub CreateChart()
        Dim xMin = -2.0
        Dim xMax = 2.0
        Dim nPoints = 21
        Dim xInc = (xMax - xMin) / (nPoints - 1)

        Dim c As New Chart
        c.Size = New Size(Me.Width * 0.9, Me.Height * 0.9)
        c.Series.Clear()

        Dim ca As New ChartArea With {.Name = "ChartArea1"}
        ca.AxisX.Title = "variable"
        ca.AxisY.Title = "dependent variable"
        ca.AxisX.Minimum = xMin
        ca.AxisX.Maximum = xMax

        c.ChartAreas.Add(ca)

        Dim s1 As New Series
        s1.Name = "Polynomial"
        s1.MarkerStyle = MarkerStyle.Circle

        Dim s2 As New Series With {
            .Name = "Derivative",
            .MarkerStyle = MarkerStyle.Diamond,
            .ChartType = SeriesChartType.Line
            }

        For i = 0 To nPoints - 1
            Dim x = xMin + i * xInc
            s1.Points.AddXY(x, Polynom(x))
            s2.Points.AddXY(x, Polynom2(x))
        Next

        c.Series.Add(s1)
        c.Series.Add(s2)
        c.Series("Polynomial").ChartType = SeriesChartType.Line

        Dim lgnd As New Legend With {.Name = "Legend"}
        c.Legends.Add(lgnd)
        lgnd.DockedToChartArea = "ChartArea1"
        lgnd.Docking = Docking.Top Or Docking.Left
        s1.Legend = "Legend"
        s2.Legend = "Legend"

        Me.Controls.Add(c)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Size = New Size(640, 480)
        Me.Text = "Simple polynomial plot"
        CreateChart()

    End Sub

End Class

generates

enter image description here

Upvotes: 2

Related Questions