Files
2025-02-Numerical/lib/nr/ansi/recipes/svdfit.c
2025-09-12 18:55:25 +09:00

43 lines
1022 B
C

#define NRANSI
#include "nrutil.h"
#define TOL 1.0e-5
void svdfit(float x[], float y[], float sig[], int ndata, float a[], int ma,
float **u, float **v, float w[], float *chisq,
void (*funcs)(float, float [], int))
{
void svbksb(float **u, float w[], float **v, int m, int n, float b[],
float x[]);
void svdcmp(float **a, int m, int n, float w[], float **v);
int j,i;
float wmax,tmp,thresh,sum,*b,*afunc;
b=vector(1,ndata);
afunc=vector(1,ma);
for (i=1;i<=ndata;i++) {
(*funcs)(x[i],afunc,ma);
tmp=1.0/sig[i];
for (j=1;j<=ma;j++) u[i][j]=afunc[j]*tmp;
b[i]=y[i]*tmp;
}
svdcmp(u,ndata,ma,w,v);
wmax=0.0;
for (j=1;j<=ma;j++)
if (w[j] > wmax) wmax=w[j];
thresh=TOL*wmax;
for (j=1;j<=ma;j++)
if (w[j] < thresh) w[j]=0.0;
svbksb(u,w,v,ndata,ma,b,a);
*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 += (tmp=(y[i]-sum)/sig[i],tmp*tmp);
}
free_vector(afunc,1,ma);
free_vector(b,1,ndata);
}
#undef TOL
#undef NRANSI