locked
Plot a Polynomial Function in VB.NET RRS feed

  • Question

  • I have some polynomial functions which I need to plot in VB.NET/Visual Studio 2010. Any suggestions would be extremely helpful, as I haven't got a clue. I've bee searching this for almost a week, with absolutely no progress.
    Monday, December 25, 2017 8:40 PM

Answers

  • I agree with Razerz. Lots of examples.

    Here is a start on it using the chart control. That's probably the most bang for the effort right now.

    You need to reference the .net assemblies framework:

     System.Windows.Forms.DataVisualization.dll

    in your project for the chart control to show in the toolbar.

    When you can see the chart control in the toolbar double click it to add to your form and set the dock to fill. Then copy and paste this code into an empty form.

    It is not required for the chart control but you should to get the Chart Samples. Those are the instructions. There are hundreds of chart features you can set for exactly what you want. You have to download open and run that Visual Studio project.

    Its up to you to modify the example for your exact needs. Or determine if another method like drawing your own is appropriate.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            'setup the chart area
            Chart1.Titles.Add("y = (a* x^2) + (b * x) + c")
            Chart1.ChartAreas.Clear()
            Chart1.ChartAreas.Add("Default")
    
            With Chart1.ChartAreas("Default")
                .AxisX.Title = "X"
                .AxisX.MajorGrid.LineColor = Color.SkyBlue
                .AxisY.MajorGrid.LineColor = Color.SkyBlue
                .AxisY.Title = "Y"
            End With
    
            'make the three series plot lines 
            Chart1.Series.Clear()
            Chart1.Series.Add("b = 2")
            Chart1.Series("b = 2").Color = Color.Red
            Chart1.Series("b = 2").ChartType = DataVisualization.Charting.SeriesChartType.Line
    
            Chart1.Series.Add("b = 6")
            Chart1.Series("b = 6").Color = Color.Green
            Chart1.Series("b = 6").ChartType = DataVisualization.Charting.SeriesChartType.Line
    
            Chart1.Series.Add("b = 12")
            Chart1.Series("b = 12").Color = Color.Blue
            Chart1.Series("b = 12").ChartType = DataVisualization.Charting.SeriesChartType.Line
    
    
            'add data to the three series
            'here b varies: 2, 6, 12
            Dim a, b, c, y As Single
    
            For x As Integer = -9 To 10 Step 1
                'calculate the y value for each x value
                'then add that point to the series line
                a = 2 : b = 2 : c = 2       'here b is 2
                y = (a * x * x) + (b * x) + c
                Chart1.Series("b = 2").Points.AddXY(x, y)
    
                a = 2 : b = 6 : c = 2       'change b to 6, a and c are unchanged
                y = (a * x * x) + (b * x) + c
                Chart1.Series("b = 6").Points.AddXY(x, y)
    
                a = 2 : b = 12 : c = 2
                y = (a * x * x) + (b * x) + c
                Chart1.Series("b = 12").Points.AddXY(x, y)
            Next
        End Sub
    End Class


    • Proposed as answer by IronRazerz Monday, December 25, 2017 11:00 PM
    • Edited by tommytwotrain Monday, December 25, 2017 11:28 PM
    • Marked as answer by Surya Kant 1 Tuesday, December 26, 2017 12:11 PM
    Monday, December 25, 2017 10:53 PM

All replies

  •  You must have been using the wrong search keywords.  A quick google search for "polynomial functions vb.net" turned up a bunch of tutorials and examples for vb.net (link below).  Read through some of them.

    Google Search For - "polynomial functions vb.net"


    If you say it can`t be done then i`ll try it

    Monday, December 25, 2017 8:55 PM
  • I agree with Razerz. Lots of examples.

    Here is a start on it using the chart control. That's probably the most bang for the effort right now.

    You need to reference the .net assemblies framework:

     System.Windows.Forms.DataVisualization.dll

    in your project for the chart control to show in the toolbar.

    When you can see the chart control in the toolbar double click it to add to your form and set the dock to fill. Then copy and paste this code into an empty form.

    It is not required for the chart control but you should to get the Chart Samples. Those are the instructions. There are hundreds of chart features you can set for exactly what you want. You have to download open and run that Visual Studio project.

    Its up to you to modify the example for your exact needs. Or determine if another method like drawing your own is appropriate.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            'setup the chart area
            Chart1.Titles.Add("y = (a* x^2) + (b * x) + c")
            Chart1.ChartAreas.Clear()
            Chart1.ChartAreas.Add("Default")
    
            With Chart1.ChartAreas("Default")
                .AxisX.Title = "X"
                .AxisX.MajorGrid.LineColor = Color.SkyBlue
                .AxisY.MajorGrid.LineColor = Color.SkyBlue
                .AxisY.Title = "Y"
            End With
    
            'make the three series plot lines 
            Chart1.Series.Clear()
            Chart1.Series.Add("b = 2")
            Chart1.Series("b = 2").Color = Color.Red
            Chart1.Series("b = 2").ChartType = DataVisualization.Charting.SeriesChartType.Line
    
            Chart1.Series.Add("b = 6")
            Chart1.Series("b = 6").Color = Color.Green
            Chart1.Series("b = 6").ChartType = DataVisualization.Charting.SeriesChartType.Line
    
            Chart1.Series.Add("b = 12")
            Chart1.Series("b = 12").Color = Color.Blue
            Chart1.Series("b = 12").ChartType = DataVisualization.Charting.SeriesChartType.Line
    
    
            'add data to the three series
            'here b varies: 2, 6, 12
            Dim a, b, c, y As Single
    
            For x As Integer = -9 To 10 Step 1
                'calculate the y value for each x value
                'then add that point to the series line
                a = 2 : b = 2 : c = 2       'here b is 2
                y = (a * x * x) + (b * x) + c
                Chart1.Series("b = 2").Points.AddXY(x, y)
    
                a = 2 : b = 6 : c = 2       'change b to 6, a and c are unchanged
                y = (a * x * x) + (b * x) + c
                Chart1.Series("b = 6").Points.AddXY(x, y)
    
                a = 2 : b = 12 : c = 2
                y = (a * x * x) + (b * x) + c
                Chart1.Series("b = 12").Points.AddXY(x, y)
            Next
        End Sub
    End Class


    • Proposed as answer by IronRazerz Monday, December 25, 2017 11:00 PM
    • Edited by tommytwotrain Monday, December 25, 2017 11:28 PM
    • Marked as answer by Surya Kant 1 Tuesday, December 26, 2017 12:11 PM
    Monday, December 25, 2017 10:53 PM
  • Life Saver! Thanks a lot Sir. 
    Tuesday, December 26, 2017 12:11 PM