help-gsl
[Top][All Lists]

## [Help-gsl] converting old least-squares fit code to use gsl instead - fa

 From: Martin Sperl Subject: [Help-gsl] converting old least-squares fit code to use gsl instead - fails miserably Date: Wed, 04 Feb 2009 09:42:02 +0100 User-agent: Thunderbird 2.0.0.19 (Windows/20081209)

```Hi!

```
I have tried to convert my non-linear least squares sinus-fit code from my university day to use the gsl library (gsl_multifit) instead of Bevingtons curfit code.
```
```
Sadly the conversion-code does not to converge to the expected value except for when using a single variable. (Taking the example in the documentation as base and adapting the functions *_f, *_df, *_fdf, as well as the data test data,...)
```
```
I have even tried to do just a linear fit (mx+b) usin ggsl_multifit and it does not converge either (see the fit functions below).
```
Has someone got experience to get me going in the right direction?
Maybe I am missing something obvious...

Thanks,
Martin

P.s: Here the _f, _df, _fdf functions:
int fit_f(const gsl_vector * x, void *data, gsl_vector * f) {
double v,phase,tmp,delta;
int i;
double sum2=0;
/* the parameters */
double p[10];
for(i=0;i<x->size;i++) {
p[i]=gsl_vector_get (x, i);
```
fprintf(stderr,"fit_f: Parameter%i: %f\n",i,p[i]); }
``` /* now the loop */
for(i=0;i<50;i++) {
/* here the function */
v=p[0]+p[1]*time[i];
/* and store the delta */
delta=v-value[i];
sum2+=delta*delta;
gsl_vector_set(f,i,v);
}
/* and return ok */
return GSL_SUCCESS;
}

int fit_df (const gsl_vector * x, void *data, gsl_matrix * J) {
double phase,tmp;
int i;
/* the parameters */
double p[10];
for(i=0;i<x->size;i++) {
p[i]=gsl_vector_get (x, i);
}
/* now the loop */
for(i=0;i<50;i++) {
/* the intercept b */
gsl_matrix_set (J, i, 0, 1);
/* the slope m */
gsl_matrix_set (J, i, 1, p[1]);
}

/* and return ok */
return GSL_SUCCESS;
}

int fit_fdf (const gsl_vector * x, void *data,
gsl_vector * f, gsl_matrix * J) {
fit_f (x, data, f);
fit_df (x, data, J);
return GSL_SUCCESS;
}

```