1 /*
2  * Library:   lmfit (Levenberg-Marquardt least squares fitting)
3  *
4  * File:      lmcurve.c
5  *
6  * Contents:  Implements lmcurve_tyd(), a variant of lmcurve() that weighs
7  *            data points y(t) with the inverse of the standard deviations dy.
8  *
9  * Copyright: Joachim Wuttke, Forschungszentrum Juelich GmbH (2004-2013)
10  *
11  * License:   see ../COPYING (FreeBSD)
12  *
13  * Homepage:  apps.jcns.fz-juelich.de/lmfit
14  */
15 
16 #include "lmmin.h"
17 
18 typedef struct {
19     const double* t;
20     const double* y;
21     const double* dy;
22     double (*f)(const double t, const double* par);
23 } lmcurve_tyd_data_struct;
24 
lmcurve_tyd_evaluate(const double * par,const int m_dat,const void * data,double * fvec,int * info)25 void lmcurve_tyd_evaluate(
26     const double* par, const int m_dat, const void* data, double* fvec,
27     int* info)
28 {
29     lmcurve_tyd_data_struct* D = (lmcurve_tyd_data_struct*)data;
30     int i;
31     for (i = 0; i < m_dat; i++)
32         fvec[i] = ( D->y[i] - D->f(D->t[i], par) ) / D->dy[i];
33 }
34 
lmcurve_tyd(const int n_par,double * par,const int m_dat,const double * t,const double * y,const double * dy,double (* f)(const double t,const double * par),const lm_control_struct * control,lm_status_struct * status)35 void lmcurve_tyd(
36     const int n_par, double* par, const int m_dat,
37     const double* t, const double* y, const double* dy,
38     double (*f)(const double t, const double* par),
39     const lm_control_struct* control, lm_status_struct* status)
40 {
41     lmcurve_tyd_data_struct data = { t, y, dy, f };
42 
43     lmmin(n_par, par, m_dat, (const void*)&data, lmcurve_tyd_evaluate,
44           control, status);
45 }
46