Lmfit
A C library for Levenberg-Marquardt minimization and least-squares fitting. From https://jugit.fz-juelich.de/mlz/lmfit/
Install / Use
/learn @NSLS-II/LmfitREADME
lmfit
This is the home page of lmfit, a self-contained C library for Levenberg-Marquardt least-squares minimization and curve fitting.
Background information
History
The core algorithm of lmfit has been invented by K Levenberg (1944) and D W Marquardt (1963). The seminal FORTRAN implementation of MINPACK is due to J M Moré, B S Garbow, and K E Hillstrom (1980). An automatic translation into C was first published by S Moshier. The present library lmfit started from a similar automatic translation. With time, every single line was hand edited to convert the code into genuine, readable C. The API was modified; examples, man pages, and build scripts were added. Corrections and refinements were contributed by users (see CHANGELOG).
Mathematics
For a gentle introduction to the Levenberg-Marquardt algorithm, see e.g. K Madsen, H B Nielsen, O Tingleff (2004): Methods for non-linear least squares problems.
License
Citation
When using lmfit as a tool in scientific work, no acknowledgement is needed: If fit results are robust, it does not matter by which software they have been obtained. If results are not robust, they should not be published anyway. In methodological publications, e g when describing software that interacts with lmfit, the preferred form of citation is:
Joachim Wuttke: lmfit – a C library for Levenberg-Marquardt least-squares minimization and curve fitting. Version […], retrieved on […] from https://jugit.fz-juelich.de/mlz/lmfit.
Contact
For bug reports or suggestions, contact the maintainer: j.wuttke@fz-juelich.de.
Installation
From source
Get the latest source archive (tgz) from Repository > Tags.
Build&install are based on CMake. Out-of-source build is enforced. After unpacking the source, go to the source directory and do:
mkdir build
cd build
cmake ..
ctest
make
make install
RPM Binaries
- https://apps.fedoraproject.org/packages/lmfit maintained by Miro Hrončok
- http://fr2.rpmfind.net/linux/rpm2html/search.php?query=lmfit
Language bindings:
- https://github.com/jvail/lmfit.js by Jan Vaillant
Usage
Manual pages:
- lmfit(7): Summary.
- lmmin2(3): Generic routine for minimizing whatever sum of squares.
- lmmin(3): Simpler legacy API without error estimates.
- lmcurve(3): Simpler interface for fitting a parametric function f(x;p) to a data set y(x).
Sample code:
- curve fitting with lmcurve()
- surface fitting as example for minimization with lmmin()
- nonlinear equations solving with lmmin()
FAQ
How to constrain parameters?
In many applications, it is desirable to impose constraints like p0>=0 or 10<p1<10.
In lmfit, there is no explicit for mechanism for this, and it is unlikely that I will ever add one: In my experience, it is sufficient to mimick constraints by variable transform or by adding a penalty to the sum of squares. Variable transform seems to be the better solution.
In the above examples: To enforce p0'>0, use e.g. p0'=exp(p0) or p0'=p0^2. To enforce -10<p1'<10, use p1'=10*tanh(p1).
How to weigh data points?
In least-squares curve fitting, data points y(x) are approximated by function values f(x;p). Sometimes, one wants to assign data points a weight w(x). One then has to minimize the sum of squares of w*(y-f).
How to implement
This can be implemented easily by copying lmcurve.h and lmcurve.c, and modifying them in obvious ways, just introducing a new parameter w.
How to choose weights
Assuming that data points y(x) follow Gaussian distributions with standard deviations dy(x), the appropriate weight is w(x) = 1 / dy(x).
How to deal with Poisson-distributed data
Least-squares fitting is not justified for Poisson-distributed data -- except if the data are large enough (say, on average y of the order of 10 or larger) so that the Poisson distribution is reasonably well approximated by a Gaussian with standard deviation dy=sqrt(y). When experimental data have on average too low values y per channel, they should be binned into fewer channels with better statistics before any least-squares fitting.
Related Skills
node-connect
351.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
