我使用了这里找到的算法: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绘制图形。