2012-11-18 101 views
2

我對C編程非常陌生。我正在使用C中的'數字食譜'中的C代碼進行多項式迴歸,但是我在執行/編譯時遇到問題。多項式迴歸c代碼

下面是我使用的代碼。一些//顯示我嘗試將程序更改爲C++。如果我不能在C語言下編譯它,也許它會在C++下工作?

我已將int main()添加到程序中以實現它。 nrutil.h是頭文件,但我沒有使用它,因爲我將所需的函數傳遞給.ccp文件(函數matrix()vector()nrerror()SQR())。

我使用的編譯器是MSVC 2010 Express。

//#include "nrutil.h" 
#include <stdio.h> 
//#include <iostream> 
//using namespace std; 

static float sqrarg; 
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg) 
void nrerror(char error_text[]); 
float *vector(long nl, long nh); 
float **matrix(long nrl, long nrh, long ncl, long nch); 

int fpoly(float x, float a[], int ma){ 
int j; 
a[1]=1.0; 

for(j=2; j<=ma;j++) 
    a[j]=a[j-1]*x; 

return ma; 
} 

void lfit(float x[], float y[], float sig[], int ndat, float a[], int ia[], int ma, float **covar, float *chisq) 

//Given a set of data points x[1..ndat], y[1..ndat] with individual standard deviations 
//sig[1..ndat], use χ2 minimization to fit for some or all of the coefficients a[1..ma] of 
//a function that depends linearly on a, y =sum(i)(ai × afunci(x)). The input array ia[1..ma] 
//indicates by nonzero entries those components of a that should be fitted for, and by zero entries 
//those components that should be held fixed at their input values. The program returns values 
//for a[1..ma], χ2 = chisq, and the covariance matrix covar[1..ma][1..ma]. (Parameters 
//held fixed will return zero covariances.)Th e user supplies a routine funcs(x,afunc,ma) that 
//returns the ma basis functions evaluated at x = x in the array afunc[1..ma]. 

{ 
void gaussj(float **a, int n, float **b, int m); 

int i,j,k,l,m,mfit=0; 
float ym,wt,sum,sig2i,**beta, *afunc; 

//vector<float> afunc(1,ma); 
//matrix<float> beta(1,ma,1,1); 
afunc=vector(1,ma); 
beta=matrix(1,ma,1,1); 

for (j=1;j<=ma;j++) 
    if (ia[j]) mfit++; 
if (mfit == 0) 
    nrerror("lfit: no parameters to be fitted"); 
for (j=1;j<=mfit;j++) {      //Initialize the (symmetric)matrix. 
    for (k=1;k<=mfit;k++) covar[j][k]=0.0; 
    beta[j][1]=0.0; 
} 
for (i=1;i<=ndat;i++) {      //Loop over data to accumulate coefficients of the normal equations. 

fpoly(x[i], afunc ,ma); 
ym=y[i]; 
if (mfit < ma) {        //Subtract off dependences on known pieces of the fitting function. 
    for (j=1;j<=ma;j++)     
     if (!ia[j]) ym -= a[j]*afunc[j]; 
} 

sig2i=1.0/SQR(sig[i]); 
for (j=0,l=1;l<=ma;l++) { 
    if (ia[l]) { 
     wt=afunc[l]*sig2i; 
     for (j++,k=0,m=1;m<=l;m++) 
      if (ia[m]) covar[j][++k] += wt*afunc[m]; 
     beta[j][1] += ym*wt; 
    } 
} 
} 
for (j=2;j<=mfit;j++)     //Fill in above the diagonal from symmetry. 
    for (k=1;k<j;k++) 
     covar[k][j]=covar[j][k]; 
gaussj(covar,mfit,beta,1);    //Matrix solution. 
for (j=0,l=1;l<=ma;l++) 
    if (ia[l]) a[l]=beta[++j][1];  //Partition solution to appropriate coefficients a Evaluate χ2 of the fit. 
*chisq=0.0; 
    for (i=1;i<=ndat;i++) { 
     fpoly(x[i], afunc,ma); 
for (sum=0.0,j=1;j<=ma;j++) 
    sum += a[j]*afunc[j]; 
*chisq += SQR((y[i]-sum)/sig[i]); 

} 
//covsrt(covar,ma,ia,mfit);    //Sort covariance matrix to true order of fitting coefficients. 
//free_vector(afunc,1,ma); 
//free_matrix(beta,1,ma,1,1); 
} 
//#endif // INTERPFUNCTIONS_H_INCLUDED 

int main() 
{ 

float x1[]={100.000000f,88.00000f,76.199997f,68.599998f,54.500000f,37.599998f,26.500000f,17.000000f,8.300000f,0.900000f,-7.200000f,-17.000000f,-24.900000f,-33.799999f,-42.500000f,-51.000000f,-60.500000f,-69.500000f,-75.300003f,-83.099998f,-94.099998f,-103.000000f,-110.099998f}; 
float y1[]={-2.876821f,-2.788704f,-2.596228f,-2.468143f,-1.898085f,-1.296223f,-0.664981f,-0.245603f,-0.280993f,-0.094657f,-0.184912f,-0.263328f,-0.181819f,-0.132037f,-0.029368f,0.134307f,0.257734f,0.305223f,0.091159f,0.063768f,-0.163334f,-0.136314f,-0.372106f}; 
float sig1[]={62.2940f,1.0532f}; 
float a1[]={1.8f,1.6f,1.7f,1.3f}; 
float **covar1, xa; 
int ia1[]={2,3,4,2}; 
int ma=4, ndat=23; 
float *chisq1; 

*chisq1=0.9f; 
covar1[4][4]=0.0f; 

fpoly(xa,a1,ma); 
lfit(x1,y1,sig1,ndat,a1,ia1,ma,covar1,chisq1); 

} 

當我嘗試編譯這個程序,它讓我看到這個錯誤輸出:

1>------ Build started: Project: lfit, Configuration: Debug Win32 ------ 
1> lfit.cpp 
1>c:\users\sony\documents\visual studio 2010\projects\lfit\lfit\lfit.cpp(68): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 
1>c:\users\sony\documents\visual studio 2010\projects\lfit\lfit\lfit.cpp(89): warning C4244: '+=' : conversion from 'double' to 'float', possible loss of data 
1>c:\users\sony\documents\visual studio 2010\projects\lfit\lfit\lfit.cpp(110): warning C4700: uninitialized local variable 'chisq1' used 
1>c:\users\sony\documents\visual studio 2010\projects\lfit\lfit\lfit.cpp(111): warning C4700: uninitialized local variable 'covar1' used 
1>c:\users\sony\documents\visual studio 2010\projects\lfit\lfit\lfit.cpp(113): warning C4700: uninitialized local variable 'xa' used 
1>lfit.obj : error LNK2019: unresolved external symbol "void __cdecl gaussj(float * *,int,float * *,int)" ([email protected]@[email protected]) referenced in function "void __cdecl lfit(float * const,float * const,float * const,int,float * const,int * const,int,float * *,float *)" ([email protected]@[email protected]) 
1>lfit.obj : error LNK2019: unresolved external symbol "void __cdecl nrerror(char * const)" ([email protected]@[email protected]) referenced in function "void __cdecl lfit(float * const,float * const,float * const,int,float * const,int * const,int,float * *,float *)" ([email protected]@[email protected]) 
1>lfit.obj : error LNK2019: unresolved external symbol "float * * __cdecl matrix(long,long,long,long)" ([email protected]@[email protected]) referenced in function "void __cdecl lfit(float * const,float * const,float * const,int,float * const,int * const,int,float * *,float *)" ([email protected]@[email protected]) 
1>lfit.obj : error LNK2019: unresolved external symbol "float * __cdecl vector(long,long)" ([email protected]@[email protected]) referenced in function "void __cdecl lfit(float * const,float * const,float * const,int,float * const,int * const,int,float * *,float *)" ([email protected]@[email protected]) 
1>C:\Users\Sony\Documents\Visual Studio 2010\Projects\lfit\Debug\lfit.exe : fatal error LNK1120: 4 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

回答

0

您已經定義未實現的功能一堆。例如,在你早期定義這個函數時:void nrerror(char error_text[]);,但你永遠不會爲它提供一個實現。確保項目內部包含任何其他相關的.c或.cpp文件,這些文件包含這些內容的定義。

即使您可能認爲您沒有使用這些功能,實際上您也是!例如,請注意以下呼叫:nrerror("lfit: no parameters to be fitted");。你的代碼使用這些函數,所以除非你爲他們提供了一個實現,你的代碼將不能正確鏈接。

+0

謝謝,你是對的,我可以找到實施,並把它們放在程序中。 – user1654988