Creating a Gaussian Curve

To create a Gaussian Curve in C1Chart, use the following code:

 

  // create and add to the chart data series representing Gaussian function

    //   y(x) = a * exp( -(x-b)*(x-b) / (2*c*c))

    // in the interval from x1 to x2

    void CreateGaussian(double x1, double x2, double a, double b, double c)

    {

      // number of points

      int cnt = 200;       var xvals = new double[cnt];

      var yvals = new double[cnt];

 

      double dx = (x2 - x1) / (cnt-1);

 

      for (int i = 0; i < cnt; i++)

      {

        var x = x1 + dx * i;

        xvals[i] = x;

        x =  (x - b) / c;

        yvals[i] = a * Math.Exp(-0.5*x*x);

      }

 

      var ds = new XYDataSeries()

      {

        XValuesSource = xvals,

        ValuesSource = yvals,

        ChartType = ChartType.Line

      };

 

      chart.Data.Children.Add(ds);

    }


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.