使用Math.NET库在vb.net中绘制多项式

时间:2017-12-23 21:46:19

标签: vb.net plot graph

我需要在VB.Net中绘制方程式。我找到了这个库,MATH.Net。它有很多C#的例子。

我可以使用这个库在VB.NET中绘制多项式函数吗?

如果是这样,那怎么样?因为我在互联网上找不到任何例子。 如果没有,那么有人可以指导我如何在VB.Net中绘制多项式吗?

我正在使用Microsoft Visual Studio 2010,并且在visual basic方面经验很少(我已经制作了如计算器等简单的应用程序)。

1 个答案:

答案 0 :(得分:1)

生成一些数据点并将它们添加到图表上的系列中。

例如,

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

产生

enter image description here