none
How can I plot Arrays in C Sharp? RRS feed

  • Question

  • I need to plot Arrays in C#; I'm very new to C# and I don't know the best way to do that. can you please tell me how can I plot y1 and t1 against each other?

    here is my codes:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Research.Oslo;
    
    
    namespace odestiff
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //solve the equations with Gear method
                var sol = Microsoft.Research.Oslo.Ode.GearBDF(
               0,
               new Vector(2, 0),
                (t, x) => new Vector(
                x[1],
                1000 * (1 - x[0]*x[0]) * x[1] - x[0])).TakeWhile(p => p.T <= 3000).ToArray();  //define the time span
    
    
                //// put results in an array
                double[] y1 = sol.Select(p => p.X[0]).ToArray();
                double[] t1 = sol.Select(p => p.T).ToArray();
    
            }
        }
    }
    

    Wednesday, November 22, 2017 4:59 AM

Answers

  • Hi Mahan,


    In Visual c#, use Chart control from Toolbox, and the code are as follows,

    In your case, the following expression

    chart1.Series["Series1"].Points.AddXY(i, rdn.Next(0, 10));

     is substituted by

    chart1.Series["Series1"].Points.AddXY(t1, y1);

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

    namespace WindowsFormsPlotArray
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Random rdn = new Random();
                for (int i = 0; i < 50; i++)
                {
                    chart1.Series["Series1"].Points.AddXY(i, rdn.Next(0, 10));
                }
                chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
                chart1.Series["Series1"].Color = Color.Red;
            }
        }
    }


    • Edited by E-John Wednesday, November 22, 2017 5:44 AM
    • Marked as answer by Mahan Moattari Thursday, November 23, 2017 7:39 AM
    Wednesday, November 22, 2017 5:31 AM

All replies

  • Hi Mahan,


    In Visual c#, use Chart control from Toolbox, and the code are as follows,

    In your case, the following expression

    chart1.Series["Series1"].Points.AddXY(i, rdn.Next(0, 10));

     is substituted by

    chart1.Series["Series1"].Points.AddXY(t1, y1);

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

    namespace WindowsFormsPlotArray
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Random rdn = new Random();
                for (int i = 0; i < 50; i++)
                {
                    chart1.Series["Series1"].Points.AddXY(i, rdn.Next(0, 10));
                }
                chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
                chart1.Series["Series1"].Color = Color.Red;
            }
        }
    }


    • Edited by E-John Wednesday, November 22, 2017 5:44 AM
    • Marked as answer by Mahan Moattari Thursday, November 23, 2017 7:39 AM
    Wednesday, November 22, 2017 5:31 AM
  • Thanks a lot! It's Work
    Appreciate that  
    Thursday, November 23, 2017 9:31 AM