| 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 modify! |
| 16 |
* If you need to compute residues differently, then please do not patch |
| 17 |
* lmcurve.h and lmcurve.c, but copy them, and create differently named |
| 18 |
* versions of lmcurve_data_struct, lmcurve_evaluate, and lmcurve of your own. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "lmmin.h" |
| 22 |
|
| 23 |
|
| 24 |
typedef struct { |
| 25 |
const double *const t; |
| 26 |
const double *const y; |
| 27 |
double (*const g) (const double t, const double *par); |
| 28 |
} lmcurve_data_struct; |
| 29 |
|
| 30 |
|
| 31 |
void lmcurve_evaluate( |
| 32 |
const double *const par, const int m_dat, const void *const data, |
| 33 |
double *const fvec, int *const info) |
| 34 |
{ |
| 35 |
(void)(info); |
| 36 |
for (int i = 0; i < m_dat; i++ ) |
| 37 |
fvec[i] = |
| 38 |
((lmcurve_data_struct*)data)->y[i] - |
| 39 |
((lmcurve_data_struct*)data)->g( |
| 40 |
((lmcurve_data_struct*)data)->t[i], par ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
void lmcurve( |
| 45 |
const int n_par, double *const par, const int m_dat, |
| 46 |
const double *const t, const double *const y, |
| 47 |
double (*const g)(const double t, const double *const par), |
| 48 |
const lm_control_struct *const control, lm_status_struct *const status) |
| 49 |
{ |
| 50 |
lmcurve_data_struct data = {t, y, g}; |
| 51 |
lmmin(n_par, par, m_dat, NULL, (const void *const) &data, |
| 52 |
lmcurve_evaluate, control, status); |
| 53 |
} |