c语言平曲线,算法——纯C语言最小二乘法曲线拟合

  • Post author:
  • Post category:其他


算法——纯C语言最小二乘法曲线拟合

写完,还没来得及写注释,已通过Matlab的polyfit验证(阶数高或者数据量太大会有double数据溢出的危险,低阶的都吻合),时间有点紧,程序注释,数学推导等时间充裕一点再贴上来

// 最小二乘法拟合.cpp : Defines the entry point for the console application.

//

#include “stdafx.h”

#include “stdlib.h”

#include “math.h”

//

#define ParaBuffer(Buffer,Row,Col) (*(Buffer + (Row) * (SizeSrc + 1) + (Col)))

//

/***********************************************************************************

***********************************************************************************/

static int GetXY(const char* FileName, double* X, double* Y, int* Amount)

{

FILE* File = fopen(FileName, “r”);

if (!File)

return -1;

for (*Amount = 0; !feof(File); X++, Y++, (*Amount)++)

if (2 != fscanf(File, (const char*)”%lf %lf”, X, Y))

break;

fclose(File);

return 0;

}

/***********************************************************************************

***********************************************************************************/

static int PrintPara(double* Para, int SizeSrc)

{

int i, j;

for (i = 0; i < SizeSrc; i++)

{

for (j = 0; j <= SizeSrc; j++)

printf(“%10.6lf “, ParaBuffer(Para, i, j));

printf(”

“);

}

printf(”

“);

return 0;

}

/***********************************************************************************

***********************************************************************************/

static int ParalimitRow(double* Para, int SizeS