讨论如何用最好的直线表示一组点。给定一组点的值 (x,y),我们需要找到最好的直线 y = mx + c,所以我们只需要找到 m 和 c 的值,例如
Input: no_of_points = 4 x1 = 2, y1 = 3, x2 = 5, y2 = 6, x3 = 1, y3 = 3, x4 = 4, y4 = 5. Output: m = 0.8, c = 1.85 Explanation: If we apply the value of m and c in the equation y = mx + c for any point (xi, yi) it would give the best straight line covering all the points. Putting value of m and c in (x2,y2), L.H.S : mx + c = 0.8 * 5 + 1.85 = 5.85 R.H.S : y = 6 which is nearly equal to L.H.S. Input: no_of_points = 3 x1 = 3, y1 = 6, x2 = 2, y2 = 4, x3 = 1, y3 = 3, Output: m = 1.5,c = 1.33
为了解决这个问题,我们需要找到 m 和 c 的值。当点数为 2 时,将有唯一解,但当没有点大于 2 时,解可能存在也可能不存在。
让我们取点数为n,
所以我们会有n个方程,fn = mxn + c
为了使这个方程最合适,我们需要找到等于或接近 yi 的 fi 值。
让我们取 Z = ( fi - yi )2; 现在,我们需要使所有点的这个值最小。我们对项 (fi - yi) 进行平方以消除负项。
为了使 Z 最小,这应该满足,