Create Graph Plots from given polynomial


(Surya Kant) #1

Hey Guys, I have a polynomial equation(max possible degree of the polynomial is 6).

I want to plot the equation, or more than 1 equation on a graph. The Environment I want to use is VB.Net(on Microsoft Visual Studio 2010), as I want it to be a local Windows Application. I have no idea(or experience) in preparing graphs, I have very little experience in VB6, and know simply how to make basic applications like calculator, etc.

Any help would be appreciated! And Thanks in Advance!


(Peter Vanderwaart) #2

As far as I can see, this not really question for Numerics. There are several answers depending on exactly what you want.

The very simplest way to make a chart is to export the data in .csv format, read it into Excel and make the graph in Excel. You might even be able to do your whole project in Excel using the VB features.

At the other end of the spectrum, you can draw your entire graph line by line. You would have to learn how to do graphics. There is a tutorial here to get you started. You have to figure out how to transform coordinates from your polynomial’s plane to the coordinates of the drawing surface.

In the middle is the possibility of using a graphics package of some sort. I’ve never used one, but I’ve seen links to the Microsoft Chart Control, which looks like it has a pretty big learning curve, but will help make charts prettier.


(Charles Wallace) #3

I have been using the OxyPlot.WPF library with good results. Here’s the documentation: http://docs.oxyplot.org/en/latest/index.html


(Peter Vanderwaart) #4

Until stimulated by this question to investigate, I didn’t know that Visual Studio/C# has a built in graphing facility. Here is an example for anyone who digs up this question in the future.

You need a pointer to the right library depending on whether you are using a Web Form or a Windows Form.

using  System.Web.UI.DataVisualization.Charting;
or 
using System.Windows.Forms.DataVisualization.Charting;

In my example, I set up an alias for simplicity:

using Charting = System.Windows.Forms.DataVisualization.Charting;

You need a Chart object. You can drag one from the tool box. See link to msdn.Microsoft in the program text. In the example, it’s named MyChart.

Poly() is a function that returns values for the polynomial with roots at the integers 1,2,…9. Everything else is straightforward. There may need to be some changes for Web Forms.

        private void btnChart_Click(object sender, EventArgs e)
        {
            // Helpful links
            //https://msdn.microsoft.com/en-us/library/dd489237.aspx
            //https://stackoverflow.com/questions/9641436/saving-higher-resolution-charts-without-messing-up-the-appearance?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

            MyChart.Visible = true;
            MyChart.Palette = Charting.ChartColorPalette.Bright;

            Charting.Title chtTitle = new System.Windows.Forms.DataVisualization.Charting.Title();
            System.Drawing.Font chtFont = new System.Drawing.Font("Arial", 16);
            chtTitle.Font = chtFont;
            chtTitle.Text = "Polynomial Chart Example";
            MyChart.Titles.Add(chtTitle);
       
            // Create Series 
            MyChart.Series.Clear();
            MyChart.Series.Add("Polynomial");
            MyChart.Series["Polynomial"].ChartType = Charting.SeriesChartType.Line; ;
            
            // populate Series    
            for (int i=9; i <= 91; i++)
            {
                double x = 0.1 * Convert.ToDouble(i);
                MyChart.Series["Polynomial"].Points.AddXY(x,Poly(x));
            }

            MyChart.Printing.Print(true);
            MyChart.SaveImage(@"C:\users\peter\documents\poly.jpg",Charting.ChartImageFormat.Jpeg);
        }

        double Poly(double x)
        {
            double y = 1.0;
            for (double c = 1.0; c < 10.0; c += 1.0)  y = y * (x - c);
            return y;
        }

The Print and SaveImage methods worked fine for me.There are issues and complications having to do with scale. There is some discussion at the link to Stack Overflow.

There are also issues with the labeling of the X-axis. In the example, grid lines are at 0.8, 2.8, 4.8, 6.8, 8.8. It would be nice to have them at integers. I have not tried control this X-axis format, but it may have to be done on a graph-by-graph basis.

You can find lots of examples on the internet once you know what to look for.