首页 > c# > 如何在c#中绘制高斯曲线

如何在c#中绘制高斯曲线 (How to Draw a Gaussian Curve in c#)

2019-04-11 c#

问题

我有一个直方图统计条形图,其中包含以下数据。

Count, HistogramBin
    0,    -1615.25
    0,    -1056.42
    0,     -497.48
    1,       61.25
    1,      620.05
    1,     1178.92
    0,     1737.76
    0,     2296.59

我需要根据上面的值形成高斯曲线。任何人都可以指导我如何实现同样的目标。

我写了一个基于维基百科链接的函数

https://en.wikipedia.org/wiki/Gaussian_function

我们的平均值是:340.67 SD:标准偏差:488.98001098632812

 private DataTable GenerateGaussTable1(DataTable histogramDataTable, 
                                       HistogramValueItem histogramValueDBItem)
    {
      double amplitude = (Average + 3 * Sigma) / 2;
      double mean = Average;
      double sd = Sigma;
      DataTable dt = new DataTable();
      dt.Columns.Add("x", typeof(float));
      dt.Columns.Add("Y", typeof(float));
      foreach (DataRow row in histogramDataTable.Rows)// top provided data
      {
        double x = Convert.ToDouble(row[1]) / 2;
        double var1 = 1 / sd * Math.Sqrt(2 * 3.14);
        double var2 = -0.5 *  Math.Pow((x - mean)/sd, 2);

        double var4= Math.Exp(var2);
        double var5 = var1 * var4;
        // Y = Amplitude * exp(-0.5 * ((X - Mean) / SD) ^ 2)
        double y = var5;
        dt.Rows.Add((float)x, (float)y);
      }
      return dt;
    }

解决方法

我已经尝试过你的功能,但它给出了错误的曲线,高斯功能是错误的,为什么你使用“2d”?

这里的功能:

在此输入图像描述

首先,v1 =(xb)。然后v2 =(xb)²/2c²最后v3 = a exp(v2)

 double gauss(double x, double a, double b, double c)
        {
            var v1 = (x - b);
            var v2 = (v1 * v1) / (2 * (c*c));
            var v3 = a * Math.Exp(-v2);
            return v3;
        }

修复后,曲线要好得多。

问题

I have a Histogram statistics bar chart with below data.

Count, HistogramBin
    0,    -1615.25
    0,    -1056.42
    0,     -497.48
    1,       61.25
    1,      620.05
    1,     1178.92
    0,     1737.76
    0,     2296.59

I need to form Gauss curve based on above values. Could anyone guide me how to achieve the same.

I have written a function based on Wikipedia link

https://en.wikipedia.org/wiki/Gaussian_function

Our average is : 340.67 SD: Standard deviation: 488.98001098632812

 private DataTable GenerateGaussTable1(DataTable histogramDataTable, 
                                       HistogramValueItem histogramValueDBItem)
    {
      double amplitude = (Average + 3 * Sigma) / 2;
      double mean = Average;
      double sd = Sigma;
      DataTable dt = new DataTable();
      dt.Columns.Add("x", typeof(float));
      dt.Columns.Add("Y", typeof(float));
      foreach (DataRow row in histogramDataTable.Rows)// top provided data
      {
        double x = Convert.ToDouble(row[1]) / 2;
        double var1 = 1 / sd * Math.Sqrt(2 * 3.14);
        double var2 = -0.5 *  Math.Pow((x - mean)/sd, 2);

        double var4= Math.Exp(var2);
        double var5 = var1 * var4;
        // Y = Amplitude * exp(-0.5 * ((X - Mean) / SD) ^ 2)
        double y = var5;
        dt.Rows.Add((float)x, (float)y);
      }
      return dt;
    }

解决方法

I've tried your function, but it gives wrong curves, The gauss function is wrong, why do you use "2d"?

Here the function :

enter image description here

so first, v1 = (x-b). Then v2 = (x-b)² / 2 c² And finaly v3 = a exp (v2)

 double gauss(double x, double a, double b, double c)
        {
            var v1 = (x - b);
            var v2 = (v1 * v1) / (2 * (c*c));
            var v3 = a * Math.Exp(-v2);
            return v3;
        }

After this fix, the curves are much better.

相似信息