0

我使用了在这里找到的算法:http ://faculty.cs.niu.edu/~hutchins/csci230/best-fit.htm来绘制一条最佳拟合线,如下所示:

int pointCount = 0; //The number of readings in the graph
    double SumX = 0; //Sum of all the X values
    double SumY = 0; //Sum of all the Y values
    double SumX2 = 0; //Sum of all the squares of the X values
    double SumXY = 0; //Sum of all the products X*Y for all the points
    double XMean = 0; //Mean of the X values
    double YMean = 0; //Mean of the Y values
    double Gradient = 0; //The gradient of the graph
    double YIntercept = 0; //The y-intercept of the graph

    for (int q=0; q<29;q++)
    {
        if (GraphReadings[q*2+2] != 0)
        {
            pointCount = pointCount + 1;
            SumX=SumX + q;
            SumY=SumY + GraphReadings[q*2+2];
            SumX2=SumX2 + (q*q);
            SumXY=SumXY + (q*GraphReadings[q*2+2]);
        }
    }

    XMean = SumX / pointCount;
    YMean = SumY / pointCount;
    Gradient = (SumXY - SumX * YMean) / (SumX2 - SumX * XMean);
    YIntercept = YMean - Gradient * XMean;

    for (int k = 0; k<141; k++)
    {
        x2[k]=k;
        y2[k]=Gradient * k + YIntercept;
    }

但是,我的图是对数图,因此上面的算法不正确。

我将如何调整此算法以在对数图上绘制最佳拟合曲线?

我正在开发这是 Qt 并使用 QCustomPlot 来绘制图形。

4

0 回答 0