builddir::bui...man::lmcurve2(3)
π NAME π
lmcurve2 β Levenberg-Marquardt least-squares fit of a curve (t,y,dy)
okay so bestie, welcome to the lmfit manual π β specifically the lmcurve2(3) page, and honestly? it's giving math that actually slaps. sit down, grab a strawberry π, we're fitting curves today.
β¨ SYNOPSIS β¨
#include <lmcurve2.h>
void lmcurve2(
const int n_par, double *par,
double *parerr, double *covar,
const int m_dat, const double *t,
const double *y, const double *dy,
double (*f)( const double ti, const double *par ),
const lm_control_struct *control,
lm_status_struct *status);
extern const lm_control_struct lm_control_double;
extern const lm_control_struct lm_control_float;
extern const char *lm_infmsg[];
extern const char *lm_shortmsg[];
no cap, that's the whole flex. β¨
π DESCRIPTION π
so lmcurve2() is literally a lil wrapper around the more generic minimization function lmmin2(), except it's got main character energy for curve fitting specifically π
here's the tea: lmcurve2() determines a vector par that minimizes the sum of squared elements of a residue vector r[i] := (y[i] - f(t[i];par)) / dy[i]. typically you use it to approximate a data set t,y,dy β where dy is the standard deviation of the empirical data y β by a parametric function f(ti;par). π§
β οΈ vibe check, listen up: on success,parrepresents a local minimum, not necessarily a global one. it may depend on its starting value. and you, yes YOU, must ensure that alldy[i]are positive. that's on you bestie, not on the function. π
π«Ά Function arguments
n_parβ number of free variables. i.e. the length of the parameter vectorpar.parβ the parameter vector itself. on input, it must contain a reasonable guess (lowkey important, don't just send it into the void). on output, it contains the solution found to minimize||r||.parerrβ parameter uncertainties vector. it's an array of lengthn_par, orNULL. on output β unless it orcovarisNULLβ it contains the weighted parameter uncertainties for the found parameters.covarβ the covariance matrix. array of lengthn_par * n_par, orNULL. on output, unless it isNULL, it contains the covariance matrix.m_datβ number of data points. the length of vectorst,y,dy. must satisfyn_par <= m_dat(yes, fr fr, that condition is non-negotiable).tβ array of lengthm_dat. contains the abcissae (time, or "x") for which functionfwill be evaluated. β°yβ array of lengthm_dat. contains the ordinate values that shall be fitted. πdyβ array of lengthm_dat. contains the standard deviations of the valuesy.fβ a user-supplied parametric functionf(ti;par). this one's yours to write, bestie π«΅controlβ parameter collection for tuning the fit procedure. in most cases, the default&lm_control_doubleis adequate. iffis only computed with single-precision accuracy,&lm_control_floatshould be used instead. the parameters themselves are explained inlmmin2(3).statusβ a record used to return information about the minimization process. for details, seelmmin2(3).
π¦ EXAMPLE π¦
fit a data set y(x) with standard deviations dy(x) by a curve f(x;p):
#include "lmcurve2.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 */
double parerr[3];
double covar[3*3];
/* 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 };
double dy[9] = { 4, 3, 2, 1, 2, 3, 4, 5, 6 };
lm_control_struct control = lm_control_double;
lm_status_struct status;
control.verbosity = 1;
printf( "Fitting ...\n" );
/* now the call to lmfit */
lmcurve2( n, par, parerr, covar, m, t, y, dy, 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 uncertainty = %12g\n", i, par[i], parerr[i]);
printf("obtained norm:\n %12g\n", status.fnorm );
printf("fitting data as follows:\n");
for ( i = 0; i < m; ++i)
printf(
" t[%1d]=%2g y=%5.1f+-%4.1f fit=%8.5f residue=%8.4f weighed=%8.4f\n",
i, t[i], y[i], dy[i], f(t[i],par), y[i] - f(t[i],par),
(y[i] - f(t[i],par))/dy[i] );
return 0;
}
iconic, honestly. copy-paste and it works. π β¨
π COPYING π
- Copyright (C) 2009-2015 Joachim Wuttke, Forschungszentrum Juelich GmbH
- Software: FreeBSD License
- Documentation: Creative Commons Attribution Share Alike
respect the license bestie, it's the law (literally) π
πΈ SEE ALSO πΈ
go read the homepage, iykyk π¦
π BUGS π
Please send bug reports and suggestions to the author <j.wuttke@fz-juelich.de>.
like, please actually send them, don't just suffer in silence π
and that's the whole page, bestie π this gorgeous little manual came courtesy of the lmfit manual, and the footer tea is: perl v5.42.0, dated 2026-01-16. slay, go fit some curves. πβ¨
π¦ where this page comes from
- fedora 44: lmfit-devel 10.0-2.fc44 Β· LicenseRef-Callaway-BSD AND LicenseRef-Callaway-CC-BY
- fedora 44: lmfit-devel 10.0-2.fc44 Β· LicenseRef-Callaway-BSD AND LicenseRef-Callaway-CC-BY
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms