⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 线性拟合函数.c

📁 线形拟合函数
💻 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 + -