1 /*
2 * Library: lmfit (Levenberg-Marquardt least squares fitting)
3 *
4 * File: lmcurve.c
5 *
6 * Contents: Implements lmcurve, a simplified API for curve fitting
7 * using the generic Levenberg-Marquardt routine lmmin.
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 * Note to programmers: Don't patch and fork, but copy and variate!
16 * If you need to compute residues differently, then please do not patch
17 * lmcurve.c, but copy it to a differently named file, and rename lmcurve(),
18 * lmcurve_evaluate() and lmcurve_data_struct before adapting them to your
19 * needs, like we have done in lmcurve_tyd.c.
20 */
21
22 #include "lmmin.h"
23
24 typedef struct {
25 const double* t;
26 const double* y;
27 double (*f)(const double t, const double* par);
28 } lmcurve_data_struct;
29
lmcurve_evaluate(const double * par,const int m_dat,const void * data,double * fvec,int * info)30 void lmcurve_evaluate(
31 const double* par, const int m_dat, const void* data, double* fvec,
32 int* info)
33 {
34 lmcurve_data_struct* D = (lmcurve_data_struct*)data;
35 int i;
36 for (i = 0; i < m_dat; i++)
37 fvec[i] = D->y[i] - D->f(D->t[i], par);
38 }
39
lmcurve(const int n_par,double * par,const int m_dat,const double * t,const double * y,double (* f)(const double t,const double * par),const lm_control_struct * control,lm_status_struct * status)40 void lmcurve(
41 const int n_par, double* par, const int m_dat,
42 const double* t, const double* y,
43 double (*f)(const double t, const double* par),
44 const lm_control_struct* control, lm_status_struct* status)
45 {
46 lmcurve_data_struct data = { t, y, f };
47
48 lmmin(n_par, par, m_dat, (const void*)&data, lmcurve_evaluate,
49 control, status);
50 }
51