60 #include <gsl/gsl_rng.h>
61 #include <gsl/gsl_randist.h>
62 #include <gsl/gsl_matrix.h>
63 #include <gsl/gsl_vector.h>
64 #include <gsl/gsl_blas.h>
65 #include <gsl/gsl_multifit_nlinear.h>
77 { (96740.0-96739.4), 1.039670408E-02, 1.0 },
78 { (96740.2-96739.4), 1.489661168E-02, 1.0 },
79 { (96740.3-96739.4), 5.147212651E-03, 1.0 },
80 { (96740.5-96739.4), 7.694352418E-03, 1.0 },
81 { (96740.7-96739.4), 2.027618140E-02, 1.0 },
82 { (96740.8-96739.4), 3.102330491E-02, 1.0 },
83 { (96741.0-96739.4), 6.541901827E-02, 1.0 },
84 { (96741.1-96739.4), 1.259476393E-01, 1.0 },
85 { (96741.3-96739.4), 1.189766824E-01, 1.0 },
86 { (96741.4-96739.4), 6.505819410E-02, 1.0 },
87 { (96741.6-96739.4), 4.259039834E-02, 1.0 },
88 { (96741.7-96739.4), 3.023725376E-02, 1.0 },
89 { (96741.9-96739.4), 1.395332254E-02, 1.0 },
90 { (96742.1-96739.4), 1.104300097E-02, 1.0 },
91 { (96742.2-96739.4), 4.119268619E-03, 1.0 },
92 { (96742.4-96739.4), 3.464561305E-04, 1.0 },
93 { (96742.5-96739.4), -3.346168669E-03, 1.0 },
94 { (96742.7-96739.4), 1.483750762E-03, 1.0 }
108 size_t n = ((
struct data *)data)->n;
109 double *
t = ((
struct data *)data)->t;
110 double *
y = ((
struct data *)data)->y;
112 double A = gsl_vector_get (x, 0);
113 double lambda = gsl_vector_get (x, 1);
114 double b = gsl_vector_get (x, 2);
118 for (i = 0; i <
n; i++)
121 double Yi = A * exp (-lambda * t[i]) + b;
122 gsl_vector_set (f, i, Yi - y[i]);
132 size_t n = ((
struct data *)data)->n;
133 double *
t = ((
struct data *)data)->t;
135 double A = gsl_vector_get (x, 0);
136 double lambda = gsl_vector_get (x, 1);
140 for (i = 0; i <
n; i++)
146 double e = exp(-lambda * t[i]);
147 gsl_matrix_set (J, i, 0, e);
148 gsl_matrix_set (J, i, 1, -t[i] * A * e);
149 gsl_matrix_set (J, i, 2, 1.0);
157 const gsl_multifit_nlinear_workspace *w)
159 gsl_vector *f = gsl_multifit_nlinear_residual(w);
160 gsl_vector *x = gsl_multifit_nlinear_position(w);
164 gsl_multifit_nlinear_rcond(&rcond, w);
166 fprintf(stderr,
"iter %2zu: A = %.4f, lambda = %.4f, b = %.4f, cond(J) = %8.4f, |f(x)| = %.4f\n",
168 gsl_vector_get(x, 0),
169 gsl_vector_get(x, 1),
170 gsl_vector_get(x, 2),
178 const gsl_multifit_nlinear_type *T = gsl_multifit_nlinear_trust;
179 gsl_multifit_nlinear_workspace *w;
180 gsl_multifit_nlinear_fdf fdf;
181 gsl_multifit_nlinear_parameters fdf_params =
182 gsl_multifit_nlinear_default_parameters();
188 gsl_matrix *covar = gsl_matrix_alloc (p, p);
189 double t[
N],
y[
N], weights[
N];
190 struct data d = {
n,
t, y };
191 double x_init[3] = { 1.0, 1.0, 0.0 };
193 gsl_vector_view x = gsl_vector_view_array (x_init, p);
194 gsl_vector_view wts = gsl_vector_view_array(weights, n);
196 double chisq, chisq0;
200 const double xtol = 1e-8;
201 const double gtol = 1e-8;
202 const double ftol = 0.0;
205 r = gsl_rng_alloc(gsl_rng_default);
233 weights[i] = indata[i].
wt;
235 printf (
"data: %g %g %g\n", t[i], y[i], weights[i]);
239 w = gsl_multifit_nlinear_alloc (T, &fdf_params, n, p);
242 gsl_multifit_nlinear_winit (&x.vector, &wts.vector, &fdf, w);
245 f = gsl_multifit_nlinear_residual(w);
246 gsl_blas_ddot(f, f, &chisq0);
249 status = gsl_multifit_nlinear_driver(100, xtol, gtol, ftol,
253 J = gsl_multifit_nlinear_jac(w);
254 gsl_multifit_nlinear_covar (J, 0.0, covar);
257 gsl_blas_ddot(f, f, &chisq);
259 #define FIT(i) gsl_vector_get(w->x, i)
260 #define ERR(i) sqrt(gsl_matrix_get(covar,i,i))
262 fprintf(stderr,
"summary from method '%s/%s'\n",
263 gsl_multifit_nlinear_name(w),
264 gsl_multifit_nlinear_trs_name(w));
265 fprintf(stderr,
"number of iterations: %zu\n",
266 gsl_multifit_nlinear_niter(w));
267 fprintf(stderr,
"function evaluations: %zu\n", fdf.nevalf);
268 fprintf(stderr,
"Jacobian evaluations: %zu\n", fdf.nevaldf);
269 fprintf(stderr,
"reason for stopping: %s\n",
270 (info == 1) ?
"small step size" :
"small gradient");
271 fprintf(stderr,
"initial |f(x)| = %f\n", sqrt(chisq0));
272 fprintf(stderr,
"final |f(x)| = %f\n", sqrt(chisq));
276 double c = GSL_MAX_DBL(1, sqrt(chisq / dof));
278 fprintf(stderr,
"chisq/dof = %g\n", chisq / dof);
280 fprintf (stderr,
"A = %.5f +/- %.5f\n",
FIT(0), c*
ERR(0));
281 fprintf (stderr,
"lambda = %.5f +/- %.5f\n",
FIT(1), c*
ERR(1));
282 fprintf (stderr,
"b = %.5f +/- %.5f\n",
FIT(2), c*
ERR(2));
285 fprintf (stderr,
"status = %s\n", gsl_strerror (status));
287 gsl_multifit_nlinear_free (w);
288 gsl_matrix_free (covar);
int expb_f(const gsl_vector *x, void *data, gsl_vector *f)
int expb_df(const gsl_vector *x, void *data, gsl_matrix *J)
void callback(const size_t iter, void *params, const gsl_multifit_nlinear_workspace *w)