add numerical recipes library
This commit is contained in:
66
lib/nr/k_and_r/recipes/medfit.c
Normal file
66
lib/nr/k_and_r/recipes/medfit.c
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
#include <math.h>
|
||||
#include "nrutil.h"
|
||||
int ndatat;
|
||||
float *xt,*yt,aa,abdevt;
|
||||
|
||||
void medfit(x,y,ndata,a,b,abdev)
|
||||
float *a,*abdev,*b,x[],y[];
|
||||
int ndata;
|
||||
{
|
||||
float rofunc();
|
||||
int j;
|
||||
float bb,b1,b2,del,f,f1,f2,sigb,temp;
|
||||
float sx=0.0,sy=0.0,sxy=0.0,sxx=0.0,chisq=0.0;
|
||||
|
||||
ndatat=ndata;
|
||||
xt=x;
|
||||
yt=y;
|
||||
for (j=1;j<=ndata;j++) {
|
||||
sx += x[j];
|
||||
sy += y[j];
|
||||
sxy += x[j]*y[j];
|
||||
sxx += x[j]*x[j];
|
||||
}
|
||||
del=ndata*sxx-sx*sx;
|
||||
aa=(sxx*sy-sx*sxy)/del;
|
||||
bb=(ndata*sxy-sx*sy)/del;
|
||||
for (j=1;j<=ndata;j++)
|
||||
chisq += (temp=y[j]-(aa+bb*x[j]),temp*temp);
|
||||
sigb=sqrt(chisq/del);
|
||||
b1=bb;
|
||||
f1=rofunc(b1);
|
||||
if (sigb > 0.0) {
|
||||
b2=bb+SIGN(3.0*sigb,f1);
|
||||
f2=rofunc(b2);
|
||||
if (b2 == b1) {
|
||||
*a=aa;
|
||||
*b=bb;
|
||||
*abdev=abdevt/ndata;
|
||||
return;
|
||||
}
|
||||
while (f1*f2 > 0.0) {
|
||||
bb=b2+1.6*(b2-b1);
|
||||
b1=b2;
|
||||
f1=f2;
|
||||
b2=bb;
|
||||
f2=rofunc(b2);
|
||||
}
|
||||
sigb=0.01*sigb;
|
||||
while (fabs(b2-b1) > sigb) {
|
||||
bb=b1+0.5*(b2-b1);
|
||||
if (bb == b1 || bb == b2) break;
|
||||
f=rofunc(bb);
|
||||
if (f*f1 >= 0.0) {
|
||||
f1=f;
|
||||
b1=bb;
|
||||
} else {
|
||||
f2=f;
|
||||
b2=bb;
|
||||
}
|
||||
}
|
||||
}
|
||||
*a=aa;
|
||||
*b=bb;
|
||||
*abdev=abdevt/ndata;
|
||||
}
|
||||
Reference in New Issue
Block a user