lmcurve - Levenberg-Marquardt least-squares curve fitting
#include <lmcurve.h>
void lmcurve( int n_par, double *par, int m_dat, const double *t, const double *y, double (*f)( double ti, const double *par ), const lm_control_struct *control, const lm_princon_struct *princon, lm_status_struct *status);
extern const lm_control_struct lm_control_double;
extern const lm_control_struct lm_control_float;
extern const lm_princon_struct lm_princon_std;
extern const char *lm_infmsg[];
extern const char *lm_shortmsg[];
lmcurve() wraps the more generic minimization function lmmin(), for use in curve fitting.
lmcurve() determines a vector par that minimizes the sum of squared elements of a residue vector r[i] := y[i] - f(t[i];par). Typically, lmcurve() is used to approximate a data set t,y by a parametric function f(ti;par). On success, par represents a local minimum, not necessarily a global one; it may depend on its starting value.
Function arguments:
Number of free variables. Dimension of parameter vector par.
Parameter vector. On input, it must contain a reasonable guess; on output, it contains the solution found to minimize ||r||.
Number of data points. Dimension of vectors t and y. Must statisfy n_par <= m_dat.
Array of length m_dat. Contains the abcissae (time, or "x") for which function f will be evaluated.
Array of length m_dat. Contains the ordinate values that shall be fitted.
A user-supplied parametric function f(ti;par).
Parameter collection for tuning the fit procedure. In most cases, the default &lm_control_double is adequate. If f is only computed with single-precision accuracy, &lm_control_float should be used. Parameters are explained in lmmin(3).
Parameter collection for tuning monitoring messages. For values NULL or &lm_princon_def, the fit is silent. Parameters are explained in lmmin(3).
A record used to return information about the minimization process: For details, see lmmin(3).
Fit a data set y(x) by a curve f(x;p):
#include "lmcurve.h" #include <stdio.h>
/* model function: a parabola */
double f( double t, const double *p ) { return p[0] + p[1]*t + p[2]*t*t; }
int main() { int n = 3; /* number of parameters in model function f */ double par[3] = { 100, 0, -10 }; /* really bad starting value */
/* data points: a slightly distorted standard parabola */ int m = 9; int i; double t[9] = { -4., -3., -2., -1., 0., 1., 2., 3., 4. }; double y[9] = { 16.6, 9.9, 4.4, 1.1, 0., 1.1, 4.2, 9.3, 16.4 };
lm_control_struct control = lm_control_double; lm_status_struct status; control.verbosity = 7;
printf( "Fitting ...\n" ); lmcurve( n, par, m, t, y, f, &control, &status );
printf( "Results:\n" ); printf( "status after %d function evaluations:\n %s\n", status.nfev, lm_infmsg[status.outcome] );
printf("obtained parameters:\n"); for ( i = 0; i < n; ++i) printf(" par[%i] = %12g\n", i, par[i]); printf("obtained norm:\n %12g\n", status.fnorm );
printf("fitting data as follows:\n"); for ( i = 0; i < m; ++i) printf( " t[%2d]=%4g y=%6g fit=%10g residue=%12g\n", i, t[i], y[i], f(t[i],par), y[i] - f(t[i],par) );
return 0; }
Copyright (C) 2009-2013 Joachim Wuttke, Forschungszentrum Juelich GmbH
Software: FreeBSD License
Documentation: Creative Commons Attribution Share Alike
Homepage: http://apps.jcns.fz-juelich.de/lmfit
Please report bugs to the author <j.wuttke@fz-juelich.de>