int main(void) { long idum=(-1); int j; float a,b,chi2,q,sa,sb,siga,sigb; float *x,*y,*dx,*dy,*dz; x=vector(1,NPT); y=vector(1,NPT); dx=vector(1,NPT); dy=vector(1,NPT); dz=vector(1,NPT); for (j=1;j<=NPT;j++) { dx[j]=0.1+ran1(&idum); dy[j]=0.1+ran1(&idum); dz[j]=0.0; x[j]=10.0+10.0*gasdev(&idum); y[j]=2.0*x[j]-5.0+dy[j]*gasdev(&idum); x[j] += dx[j]*gasdev(&idum); } printf("Values of a,b,siga,sigb,chi2,q:\n"); printf("Fit with x and y errors gives:\n"); fitexy(x,y,NPT,dx,dy,&a,&b,&siga,&sigb,&chi2,&q); printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n\n", a,b,siga,sigb,chi2,q); printf("Setting x errors to zero gives:\n"); fitexy(x,y,NPT,dz,dy,&a,&b,&siga,&sigb,&chi2,&q); printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n", a,b,siga,sigb,chi2,q); printf("...to be compared with fit result:\n"); fit(x,y,NPT,dy,1,&a,&b,&siga,&sigb,&chi2,&q); printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n\n", a,b,siga,sigb,chi2,q); printf("Setting y errors to zero gives:\n"); fitexy(x,y,NPT,dx,dz,&a,&b,&siga,&sigb,&chi2,&q); printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n", a,b,siga,sigb,chi2,q); printf("...to be compared with fit result:\n"); fit(y,x,NPT,dx,1,&a,&b,&siga,&sigb,&chi2,&q); sa=sqrt(siga*siga+SQR(sigb*(a/b)))/b; sb=sigb/(b*b); printf("%11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n", -a/b,1.0/b,sa,sb,chi2,q); free_vector(dz,1,NPT); free_vector(dy,1,NPT); free_vector(dx,1,NPT); free_vector(y,1,NPT); free_vector(x,1,NPT); return 0; }
do_line() { real *x, *y, *dx, *dy, *dz; int i,j, mwt; real chi2,q,siga,sigb, sigma, d, sa, sb; real cov00, cov01, cov11, sumsq; real r, prob, z; void fit(), pearsn(); if (nxcol < 1) error("nxcol=%d",nxcol); if (nycol < 1) error("nycol=%d",nycol); x = xcol[0].dat; y = ycol[0].dat; dx = (dxcolnr>0 ? dxcol.dat : NULL); dy = (dycolnr>0 ? dycol.dat : NULL); #if HAVE_GSL if (dx) error("Cannot use GSL with errors in X column"); gsl_fit_linear (x, 1, y, 1, npt, &b, &a, &cov00, &cov01, &cov11, &sumsq); printf("y=ax+b: a=%g b=%g cov00,01,11=%g %g %g sumsq=%g\n", a,b, cov00,cov01,cov11, sumsq); if (dy) { for (i=0; i<npt; i++) dy[i] = 1/(dy[i]*dy[i]); gsl_fit_wlinear (x, 1, dy, 1, y, 1, npt, &b, &a, &cov00, &cov01, &cov11, &chi2); printf("y=ax+b: a=%g b=%g cov00,01,11=%g %g %g chi^2=%g\n", a,b, cov00,cov01,cov11, chi2); } #else if (dx) { #if defined(TESTNR) warning("new FITEXY method"); dz = (real *) allocate(npt*sizeof(real)); for (i=0; i<npt; i++) dz[i] = 0.0; fitexy(x,y,npt,dx,dy,&b,&a,&sigb,&siga,&chi2,&q); printf("fitexy(x,y,dx,dy) a=%g b=%g %g %g\n",b,a,sigb,siga); fitexy(x,y,npt,dz,dy,&a,&b,&siga,&sigb,&chi2,&q); printf("fitexy(x,y, 0,dy) a=%g b=%g %g %g\n",a,b,siga,sigb); fitexy(x,y,npt,dx,dz,&a,&b,&siga,&sigb,&chi2,&q); printf("fitexy(x,y,dx, 0) a=%g b=%g %g %g\n",a,b,siga,sigb); fit (x,y,npt,dy, 1,&a,&b,&siga,&sigb,&chi2,&q); printf("fit (x,y, 0,dy) a=%g b=%g %g %g\n",a,b,siga,sigb); fit (y,x,npt,dx, 1,&a,&b,&siga,&sigb,&chi2,&q); printf("fit (y,x, 0,dx) a=%g b=%g %g %g\n",a,b,siga,sigb); sa=sqrt(siga*siga+sqr(sigb*(a/b)))/b; sb=sigb/(b*b); printf("FITEXY: %11.6f %11.6f %11.6f %11.6f %11.6f %11.6f\n", -a/b,1.0/b,sa,sb,chi2,q); #elif defined(TESTMP) struct vars_struct v; /* private data for mpfit() */ int status; mp_result result; double p[2] = {1.0, 1.0}; double perror[2]; warning("MPFIT method; mode mpfit=%d",mpfit_mode); bzero(&result,sizeof(result)); result.xerror = perror; v.x = xcol[0].dat; v.y = ycol[0].dat; v.ex = dxcol.dat; v.ey = dycol.dat; v.mode = mpfit_mode; status = mpfit(linfitex, npt, 2, p, 0, 0, (void *) &v, &result); dprintf(1,"*** mpfit status = %d\n", status); printf(" CHI-SQUARE = %f (%d DOF)\n", result.bestnorm, result.nfunc-result.nfree); printf(" NPAR = %d\n", result.npar); printf(" NFREE = %d\n", result.nfree); printf(" NPEGGED = %d\n", result.npegged); printf(" NITER = %d\n", result.niter); printf(" NFEV = %d\n", result.nfev); printf("\n"); for (i=0; i<result.npar; i++) { printf(" P[%d] = %f +/- %f\n", i, p[i], result.xerror[i]); } #else error("no dycol= implemented yet"); #endif } else { for(mwt=0;mwt<=1;mwt++) { #if 1 if (mwt>0 && nsigma>0) { fit(x,y,npt,dy,0,&b,&a,&sigb,&siga,&chi2,&q); } else { if (mwt>0 && dycolnr==0) continue; fit(x,y,npt,dy,mwt,&b,&a,&sigb,&siga,&chi2,&q); } #else if (mwt==0) fit(x,y,npt,dy,mwt,&b,&a,&sigb,&siga,&chi2,&q); else myfit(x,y,npt,dy,mwt,&b,&a,&sigb,&siga,&chi2,&q); #endif dprintf(2,"\n"); dprintf (1,"Fit: y=ax+b\n"); if (mwt == 0) dprintf(1,"Ignoring standard deviations\n"); else dprintf(1,"Including standard deviation\n"); printf("%12s %9.6f %18s %9.6f \n", "a = ",a,"uncertainty:",siga); printf("%12s %9.6f %18s %9.6f \n", "b = ",b,"uncertainty:",sigb); printf("%12s %9.6f %18s %9.6f \n", "x0 = ",-b/a,"uncertainty:",sqrt(sqr(sigb/a)+sqr(siga*b/(a*a)))); printf("%12s %9.6f %18s %9.6f \n", "y0 = ",b,"uncertainty:",sigb); printf("%19s %14.6f \n","chi-squared: ",chi2); printf("%23s %10.6f %s\n","goodness-of-fit: ",q, q==1 ? "(no Y errors supplied [dycol=])" : ""); pearsn(x, y, npt, &r, &prob, &z); printf("%9s %g\n","r: ",r); printf("%12s %g\n","prob: ",prob); printf("%9s %g\n","z: ",z); if (mwt==0 && nsigma>0) { sigma = 0.0; for(i=0; i<npt; i++) sigma += sqr(y[i] - a*x[i] - b); sigma /= (real) npt; sigma = nsigma * sqrt(sigma); /* critical distance */ for(i=0, j=0; i<npt; i++) { /* loop over points */ d = ABS(y[i] - a*x[i] - b); if (d > sigma) continue; x[j] = x[i]; /* shift them over */ y[j] = y[i]; if (dy) dy[j] = dy[i]; j++; } dprintf(0,"%d/%d points outside %g*sigma (%g)\n", npt-j,npt,nsigma,sigma); npt = j; } } /* mwt */ } /* dxcol */ if (outstr) write_data(outstr); #endif }