求m和c的值,使直线y = mx + c最好地表示给定点集(x ,Y ), (X ,Y ), (X ,Y ), ……。, (X ,Y ),给定n> = 2。
例子:
输入:n = 5 x = 1,x = 2,x = 3,x = 4,x = 5年 = 14,y = 27,y = 40,y = 55,y = 68输出:m = 13.6 c = 0如果我们取任意一对数字(x ,Y )从给定的数据来看,m和c的这些值应使其最适合直线方程y = mx + c。取x = 1和y = 14,然后使用输出中的m和c值,并将其放在以下等式中,y = mx + c,LHS:y = 14,RHS:mx + c = 13.6 x 1 + 0 = 13.6大致相等。现在,取x = 3和y = 40,LHS:y = 40,RHS:mx + c = 13.6 x 3 + 0 = 40.8因此,它们也近似相等,对于所有其他值,依此类推。输入:n = 6 x = 1,x = 2,x = 3,x = 4,x = 5,x = 6年 = 1200,y = 900,y = 600,y = 200,y = 110,y = 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 = mx + c,f = mx + c,f = mx + c,………………………………..,…….. …………………………, F = mx + c,其中,f ,是将x放入的值在等式mx + c中。
那么,由于理想上f 应该与y相同 ,但是我们仍然可以找到f 最接近y 在所有情况下,如果我们取一个新的量,U =?(y – F ) ,并使该数量对于i的所有值(从1到n)最小。
注意: (y – F ) 用于代替(y – F ),因为我们要考虑f时的两种情况或当y 更大,并且我们希望它们的差最小,因此,如果我们不对项求平方,则在这种情况下,f
更大,且其中y的情况更大的程度会互相抵消,这不是我们想要的。因此,我们需要对术语进行平方。
现在,要使U最小,它必须满足以下两个方程式–
= 0且 = 0。
在求解上述两个方程式时,我们得到两个方程式,如下所示:
?y = nc + m?x,?xy = c?x + m?x ,可以重新排列为-m =(n *?xy-?x?y)/(n *?x – (?X) ),而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-高等工程数学。