/* @(#)lfit.c 17.1.1.1 (ES0-DMD) 01/25/02 17:26:59 */ /*=========================================================================== Copyright (C) 1995 European Southern Observatory (ESO) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, MA 02139, USA. Corresponding concerning ESO-MIDAS should be addressed as follows: Internet e-mail: midas@eso.org Postal address: European Southern Observatory Data Management Division Karl-Schwarzschild-Strasse 2 D 85748 Garching bei Muenchen GERMANY ===========================================================================*/ /* @(#)lfit.c 17.1.1.1 (ESO) 01/25/02 17:26:59 */ static double sqrarg; #define SQR(a) (sqrarg=(a),sqrarg*sqrarg) void lfit(x,y,sig,ndata,a,ma,lista,mfit,covar,chisq,funcs) int ndata,ma,lista[],mfit; double sig[],a[],**covar,*chisq; float x[],y[]; void (*funcs)(); /* ANSI: void (*funcs)(double,double *,int); */ { int k,kk,j,ihit,i; double ym,wt,sum,sig2i,**beta,*afunc; void gaussj(),covsrt(),nrerror(),free_dmatrix(),free_dvector(); double **dmatrix(),*dvector(); beta=dmatrix(1,ma,1,1); afunc=dvector(1,ma); kk=mfit+1; for (j=1;j<=ma;j++) { ihit=0; for (k=1;k<=mfit;k++) if (lista[k] == j) ihit++; if (ihit == 0) lista[kk++]=j; else if (ihit > 1) nrerror("Bad LISTA permutation in LFIT-1"); } if (kk != (ma+1)) nrerror("Bad LISTA permutation in LFIT-2"); for (j=1;j<=mfit;j++) { for (k=1;k<=mfit;k++) covar[j][k]=0.0; beta[j][1]=0.0; } for (i=1;i<=ndata;i++) { (*funcs)(x[i],afunc,ma); ym=(double)y[i]; if (mfit < ma) for (j=(mfit+1);j<=ma;j++) ym -= a[lista[j]]*afunc[lista[j]]; sig2i=1.0/SQR(sig[i]); for (j=1;j<=mfit;j++) { wt=afunc[lista[j]]*sig2i; for (k=1;k<=j;k++) covar[j][k] += wt*afunc[lista[k]]; beta[j][1] += ym*wt; } } if (mfit > 1) for (j=2;j<=mfit;j++) for (k=1;k<=j-1;k++) covar[k][j]=covar[j][k]; gaussj(covar,mfit,beta,1); for (j=1;j<=mfit;j++) a[lista[j]]=beta[j][1]; *chisq=0.0; for (i=1;i<=ndata;i++) { (*funcs)(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,lista,mfit); free_dvector(afunc,1,ma); free_dmatrix(beta,1,ma,1,1); } #undef SQR