将数据读入线性回归C程序

时间:2014-07-15 16:04:48

标签: c linux linear-regression numerical-methods

以下是我从Numerical Recipes修改过的代码。我的x代表电压输入,我的y代表数字代码输出。我仍然是编程的新手,这也是我第一次在Linux环境中工作,所以我只是想知道将数据集读入fit函数的最佳方法。谢谢!

#include <vxWorks.h>
#include <vxLib.h>
#include <sysLib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <taskLib.h>

static float sqrarg;
#define SQR(a) (sqrarg=(a),sqrarg*sqrarg)


void fit(x,y,ndata,sig,mwt,a, b, siga, sigb, chi2, q)
float x[], y[], sig[], *a, *b, *siga, *sigb, *chi2, *q;
int ndata, mwt;


{
int i;
float t,sxoss,sx=0.0,sy=0.0,st2=0.0,ss,sigdat;

*b=0.0;

    for (i=1;i<=ndata; i++) {
    sx += x[i];
    sy += y[i];
    }
    ss=ndata;

sxoss=sx/ss;

        for (i=1;i<=ndata;i++) {
    t=(x[i]-sxoss);
    st2 += t*t;
    *b += t*y[i];
     }

*b /= st2;
*a=(sy-sx*(*b))/ss;
*siga=sqrt ((1.0+sx*sx/(ss+st2))/ss);
*sigb=sqrt (1.0/st2);
*chi2=0.0;

(mwt==0) 

   for (i=1;i<=ndata;i++) {
       *chi2 += SQR(y[i]-(*a)-(*b)*x[i]);
   *q=1.0;
   sigdat = sqrt((*chi2)/(ndata-2));
   *siga *= sigdat;
   *sigb *= sigdat;
  } 
}

0 个答案:

没有答案
相关问题