📜  用最好的直线表示一组给定的点

📅  最后修改于: 2021-05-06 07:37:55             🧑  作者: Mango

求m和c的值,使直线y = mx + c最好地表示给定点集(x _1   ,Y _1   ), (X _2   ,Y _2   ), (X _3   ,Y _3   ), ……。, (X _n   ,Y _n   ),给定n> = 2。

例子:

输入:n = 5 x _1 = 1,x _2 = 2,x _3 = 3,x _4 = 4,x _5 = 5年_1 = 14,y _2 = 27,y _3 = 40,y _4 = 55,y _5 = 68输出:m = 13.6 c = 0如果我们取任意一对数字(x _i ,Y _i )从给定的数据来看,m和c的这些值应使其最适合直线方程y = mx + c。取x _1 = 1和y _1 = 14,然后使用输出中的m和c值,并将其放在以下等式中,y = mx + c,LHS:y = 14,RHS:mx + c = 13.6 x 1 + 0 = 13.6大致相等。现在,取x _3 = 3和y _3 = 40,LHS:y = 40,RHS:mx + c = 13.6 x 3 + 0 = 40.8因此,它们也近似相等,对于所有其他值,依此类推。输入:n = 6 x _1 = 1,x _2 = 2,x _3 = 3,x _4 = 4,x _5 = 5,x _6 = 6年_1 = 1200,y _2 = 900,y _3 = 600,y _4 = 200,y _5 = 110,y _6 = 50输出:m = -243.42 c = 1361.97

方法

为了使方程中的一组点最适合一条直线,我们需要找到两个变量m和c的值。现在,由于存在2个未知变量,并且取决于n的值,两种情况是可能的–

情况1 –当n = 2时:将找到两个方程和两个未知变量,因此,将有一个唯一的解。
情况2 –当n> 2时:在这种情况下,可能存在或不存在满足所有n个方程式的m和c的值,但是我们可以找到m和c的最佳可能值,它们可以拟合一条直线给定的点。

因此,如果我们有n对不同的x和y对,那么我们可以形成n个不。来自它们的方程组的直线,如下

F _1 = mx _1 + c,f _2 = mx _2 + c,f _3 = mx _3 + c,………………………………..,…….. …………………………, F _n = mx _n + c,其中,f _i ,是将x放入的值_i在等式mx + c中。

那么,由于理想上f _i   应该与y相同_i   ,但是我们仍然可以找到f _i   最接近y _i   在所有情况下,如果我们取一个新的量,U =?(y _i   – F _i   ) ^2   ,并使该数量对于i的所有值(从1到n)最小。

注意: (y _i   – F _i   ) ^2   用于代替(y _i   – F _i   ),因为我们要考虑f时的两种情况_i   或当y _i   更大,并且我们希望它们的差最小,因此,如果我们不对项求平方,则在这种情况下,f _i
更大,且其中y的情况_i   更大的程度会互相抵消,这不是我们想要的。因此,我们需要对术语进行平方。

现在,要使U最小,它必须满足以下两个方程式–

\frac{\partial U}{\partial m} = 0且\frac{\partial U}{\partial c} = 0。

在求解上述两个方程式时,我们得到两个方程式,如下所示:

?y = nc + m?x,?xy = c?x + m?x ^2 ,可以重新排列为-m =(n *?xy-?x?y)/(n *?x ^2 – (?X) ^2 ),而c =(?y-m?x)/ n,

因此,这就是两种情况下m和c的值的获取方式,并且我们可以通过最佳可能的直线表示一组给定的点。

以下代码实现了上述算法-

C++
// C++ Program to find m and c for a straight line given,
// x and y
#include 
#include 
using namespace std;
 
// function to calculate m and c that best fit points
// represented by x[] and y[]
void bestApproximate(int x[], int y[], int n)
{
    float m, c, sum_x = 0, sum_y = 0, sum_xy = 0, sum_x2 = 0;
    for (int i = 0; i < n; i++) {
        sum_x += x[i];
        sum_y += y[i];
        sum_xy += x[i] * y[i];
        sum_x2 += pow(x[i], 2);
    }
 
    m = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - pow(sum_x, 2));
    c = (sum_y - m * sum_x) / n;
 
    cout << "m =" << m;
    cout << "\nc =" << c;
}
 
// Driver main function
int main()
{
    int x[] = { 1, 2, 3, 4, 5 };
    int y[] = { 14, 27, 40, 55, 68 };
    int n = sizeof(x) / sizeof(x[0]);
    bestApproximate(x, y, n);
    return 0;
}


C
// C Program to find m and c for a straight line given,
// x and y
#include 
 
// function to calculate m and c that best fit points
// represented by x[] and y[]
void bestApproximate(int x[], int y[], int n)
{
    int i, j;
    float m, c, sum_x = 0, sum_y = 0, sum_xy = 0, sum_x2 = 0;
    for (i = 0; i < n; i++) {
        sum_x += x[i];
        sum_y += y[i];
        sum_xy += x[i] * y[i];
        sum_x2 += (x[i] * x[i]);
    }
 
    m = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - (sum_x * sum_x));
    c = (sum_y - m * sum_x) / n;
 
    printf("m =% f", m);
    printf("\nc =% f", c);
}
 
// Driver main function
int main()
{
    int x[] = { 1, 2, 3, 4, 5 };
    int y[] = { 14, 27, 40, 55, 68 };
    int n = sizeof(x) / sizeof(x[0]);
    bestApproximate(x, y, n);
    return 0;
}


Java
// Java Program to find m and c for a straight line given,
// x and y
import java.io.*;
import static java.lang.Math.pow;
 
public class A {
    // function to calculate m and c that best fit points
    // represented by x[] and y[]
    static void bestApproximate(int x[], int y[])
    {
        int n = x.length;
        double m, c, sum_x = 0, sum_y = 0,
                     sum_xy = 0, sum_x2 = 0;
        for (int i = 0; i < n; i++) {
            sum_x += x[i];
            sum_y += y[i];
            sum_xy += x[i] * y[i];
            sum_x2 += pow(x[i], 2);
        }
 
        m = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - pow(sum_x, 2));
        c = (sum_y - m * sum_x) / n;
 
        System.out.println("m = " + m);
        System.out.println("c = " + c);
    }
 
    // Driver main function
    public static void main(String args[])
    {
        int x[] = { 1, 2, 3, 4, 5 };
        int y[] = { 14, 27, 40, 55, 68 };
        bestApproximate(x, y);
    }
}


Python3
# python Program to find m and c for
# a straight line given, x and y
 
# function to calculate m and c that
# best fit points represented by x[]
# and y[]
def bestApproximate(x, y, n):
     
    sum_x = 0
    sum_y = 0
    sum_xy = 0
    sum_x2 = 0
     
    for i in range (0, n):
        sum_x += x[i]
        sum_y += y[i]
        sum_xy += x[i] * y[i]
        sum_x2 += pow(x[i], 2)
 
    m = (float)((n * sum_xy - sum_x * sum_y)
            / (n * sum_x2 - pow(sum_x, 2)));
             
    c = (float)(sum_y - m * sum_x) / n;
     
    print("m = ", m);
    print("c = ", c);
     
     
# Driver main function
x = [1, 2, 3, 4, 5 ]
y = [ 14, 27, 40, 55, 68]
n = len(x)
 
bestApproximate(x, y, n)
     
# This code is contributed by Sam007.


C#
// C# Program to find m and c for a
// straight line given, x and y
using System;
 
class GFG {
 
    // function to calculate m and c that
    // best fit points represented by x[] and y[]
    static void bestApproximate(int[] x, int[] y)
    {
        int n = x.Length;
        double m, c, sum_x = 0, sum_y = 0,
                     sum_xy = 0, sum_x2 = 0;
 
        for (int i = 0; i < n; i++) {
            sum_x += x[i];
            sum_y += y[i];
            sum_xy += x[i] * y[i];
            sum_x2 += Math.Pow(x[i], 2);
        }
 
        m = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - Math.Pow(sum_x, 2));
 
        c = (sum_y - m * sum_x) / n;
 
        Console.WriteLine("m = " + m);
        Console.WriteLine("c = " + c);
    }
 
    // Driver main function
    public static void Main()
    {
        int[] x = { 1, 2, 3, 4, 5 };
        int[] y = { 14, 27, 40, 55, 68 };
 
        // Function calling
        bestApproximate(x, y);
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出:

m=13.6
c=0.0

分析以上代码
辅助空间:O(1)
时间复杂度:O(n)。我们有一个循环迭代n次,每次循环执行常数no。计算。

参考-
BS Grewal的1-高等工程数学。