?? 線性擬合函數.c
字號:
/* 這份源代碼文件已被未注冊的SourceFormatX格式化過 */
/* 如果您想不再添加此類信息,請您注冊這個共享軟件 */
/* 更多相關信息請訪問網站: http://cn.textrush.com */
/////////////////////////////////////////////////////////////////////
// Purpose: (x_i,y_i)線性擬合函數 //
/////////////////////////////////////////////////////////////////////
#include <stdio.h>
#define MAX_N 25
typedef struct tagPOINT
{
double x;
double y;
} POINT;
int main()
{
int m;
int i;
POINT points[MAX_N];
static double u11, u12, u21, u22, c1, c2;
double a, b, tmp;
printf("\n Input m value: ");
scanf("%d", &m);
if (m > MAX_N)
{
printf("The input m is larger than MAX_N, please redefine the MAX_N.\n");
return 1;
}
if (m <= 0)
{
printf("Please input a number between 1 and &d. \n", MAX_N);
return 1;
}
printf("Now input the (x_i, y_i), i=0, ..., %d: \n", m - 1);
for (i = 0; i < m; i++)
{
scanf("%lf", &tmp);
points[i].x = tmp;
scanf("%lf", &tmp);
points[i].y = tmp;
}
for (i = 0; i < m; i++)
{
u21 += points[i].x;
u22 += points[i].x *points[i].x;
c1 += points[i].y;
c2 += points[i].x *points[i].y;
}
u12 = u21;
u11 = m;
a = (c1 *u22 - c2 * u12) / (u11 *u22 - u12 * u21);
b = (c1 *u21 - c2 * u11) / (u21 *u12 - u22 * u11);
printf("Solve: p(x)=%f+%fx\n", a, b);
system("pause");
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -