.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
.    if \nF \{
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "lmmin 3"
.TH lmmin 3 "2015-11-27" "perl v5.20.2" "lmfit manual"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
lmmin \- Levenberg\-Marquardt least\-squares minimization
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
\&\fB#include <lmmin.h\fR>
.PP
\&\fBvoid lmmin( const int\fR \fIn_par\fR\fB, double *\fR\fIpar\fR\fB, const int\fR \fIm_dat\fR\fB,
            const\ void *\fR\fIdata\fR\fB,
            void *\fR\fIevaluate\fR\fB(
                 const\ double *\fR\fIpar\fR\fB, const int \fR\fIm_dat\fR\fB,
                 const\ void *\fR\fIdata\fR\fB, double *\fR\fIfvec\fR\fB, int *\fR\fIuserbreak\fR\fB),
            const\ lm_control_struct *\fR\fIcontrol\fR\fB,
            lm_status_struct *\fR\fIstatus\fR\fB );\fR
.PP
\&\fBextern const lm_control_struct lm_control_double;\fR
.PP
\&\fBextern const lm_control_struct lm_control_float;\fR
.PP
\&\fBextern const char *lm_infmsg[];\fR
.PP
\&\fBextern const char *lm_shortmsg[];\fR
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\fB\f(BIlmmin()\fB\fR determines a vector \fIpar\fR that minimizes the sum of squared elements of a vector \fIfvec\fR that is computed by a user-supplied function \fIevaluate\fR().
On success, \fIpar\fR represents a local minimum, not necessarily a global one; it may depend on its starting value.
.PP
For applications in curve fitting, the wrapper function \fB\f(BIlmcurve\fB\|(3)\fR offers a simplified \s-1API.\s0
.PP
The Levenberg-Marquardt minimization starts with a steepest-descent exploration of the parameter space, and achieves rapid convergence by crossing over into the Newton-Gauss method.
.PP
Function arguments:
.IP "\fIn_par\fR" 4
.IX Item "n_par"
Number of free variables.
Length of parameter vector \fIpar\fR.
.IP "\fIpar\fR" 4
.IX Item "par"
Parameter vector.
On input, it must contain a reasonable guess.
On output, it contains the solution found to minimize ||\fIfvec\fR||.
.IP "\fIm_dat\fR" 4
.IX Item "m_dat"
Length of vector \fIfvec\fR.
Must statisfy \fIn_par\fR <= \fIm_dat\fR.
.IP "\fIdata\fR" 4
.IX Item "data"
This pointer is ignored by the fit algorithm,
except for appearing as an argument in all calls to the user-supplied
routine \fIevaluate\fR.
.IP "\fIevaluate\fR" 4
.IX Item "evaluate"
Pointer to a user-supplied function that computes \fIm_dat\fR elements of vector \fIfvec\fR for a given parameter vector \fIpar\fR.
If \fIevaluate\fR return with *\fIuserbreak\fR set to a negative value, \fB\f(BIlmmin()\fB\fR will interrupt the fitting and terminate.
.IP "\fIcontrol\fR" 4
.IX Item "control"
Parameter collection for tuning the fit procedure.
In most cases, the default &\fIlm_control_double\fR is adequate.
If \fIf\fR is only computed with single-precision accuracy,
\&\fI&lm_control_float\fR should be used.
See also below, \s-1NOTES\s0 on initializing parameter records.
.Sp
\&\fIcontrol\fR has the following members (for more details, see the source file \fIlmstruct.h\fR):
.RS 4
.IP "\fBdouble\fR \fIcontrol.ftol\fR" 4
.IX Item "double control.ftol"
Relative error desired in the sum of squares.
Recommended setting: somewhat above machine precision; less if \fIfvec\fR is computed with reduced accuracy.
.IP "\fBdouble\fR \fIcontrol.xtol\fR" 4
.IX Item "double control.xtol"
Relative error between last two approximations.
Recommended setting: as \fIftol\fR.
.IP "\fBdouble\fR \fIcontrol.gtol\fR" 4
.IX Item "double control.gtol"
A measure for degeneracy.
Recommended setting: as \fIftol\fR.
.IP "\fBdouble\fR \fIcontrol.epsilon\fR" 4
.IX Item "double control.epsilon"
Step used to calculate the Jacobian.
Recommended setting: as \fIftol\fR, but definitely less than the accuracy of \fIfvec\fR.
.IP "\fBdouble\fR \fIcontrol.stepbound\fR" 4
.IX Item "double control.stepbound"
Initial bound to steps in the outer loop, generally between 0.01 and 100; recommended value is 100.
.IP "\fBint\fR \fIcontrol.patience\fR" 4
.IX Item "int control.patience"
Used to set the maximum number of function evaluations to patience*n_par.
.IP "\fBint\fR \fIcontrol.scale_diag\fR" 4
.IX Item "int control.scale_diag"
Logical switch (0 or 1).
If 1, then scale parameters to their initial value.
This is the recommended setting.
.IP "\fBFILE*\fR \fIcontrol.msgfile\fR" 4
.IX Item "FILE* control.msgfile"
Progress messages will be written to this file.
Typically \fIstdout\fR or \fIstderr\fR.
The value \fI\s-1NULL\s0\fR will be interpreted as \fIstdout\fR.
.IP "\fBint\fR \fIcontrol.verbosity\fR" 4
.IX Item "int control.verbosity"
If nonzero, some progress information from within the \s-1LM\s0 algorithm
is written to control.stream.
.IP "\fBint\fR \fIcontrol.n_maxpri\fR" 4
.IX Item "int control.n_maxpri"
\&\-1, or maximum number of parameters to print.
.IP "\fBint\fR \fIcontrol.m_maxpri\fR" 4
.IX Item "int control.m_maxpri"
\&\-1, or maximum number of residuals to print.
.RE
.RS 4
.RE
.IP "\fIstatus\fR" 4
.IX Item "status"
A record used to return information about the minimization process:
.RS 4
.IP "\fBdouble\fR \fIstatus.fnorm\fR" 4
.IX Item "double status.fnorm"
Norm of the vector \fIfvec\fR;
.IP "\fBint\fR \fIstatus.nfev\fR" 4
.IX Item "int status.nfev"
Actual number of iterations;
.IP "\fBint\fR \fIstatus.outcome\fR" 4
.IX Item "int status.outcome"
Status of minimization;
for the corresponding text message, print \fIlm_infmsg\fR\fB[\fR\fIstatus.outcome\fR\fB]\fR;
for a short code, print \fIlm_shortmsg\fR\fB[\fR\fIstatus.outcome\fR\fB]\fR.
.IP "\fBint\fR \fIstatus.userbreak\fR" 4
.IX Item "int status.userbreak"
Set when termination has been forced by the user-supplied routine \fIevaluate\fR.
.RE
.RS 4
.RE
.SH "NOTES"
.IX Header "NOTES"
.SS "Initializing parameter records."
.IX Subsection "Initializing parameter records."
The parameter record \fIcontrol\fR should always be initialized
from supplied default records:
.PP
.Vb 1
\&    lm_control_struct control = lm_control_double; /* or _float */
.Ve
.PP
After this, parameters may be overwritten:
.PP
.Vb 2
\&    control.patience = 500; /* allow more iterations */
\&    control.verbosity = 15; /* for verbose monitoring */
.Ve
.PP
An application written this way is guaranteed to work even if new parameters
are added to \fIlm_control_struct\fR.
.PP
Conversely, addition of parameters is not considered an \s-1API\s0 change; it may happen without increment of the major version number.
.SH "EXAMPLES"
.IX Header "EXAMPLES"
.SS "Fitting a surface"
.IX Subsection "Fitting a surface"
Fit a data set y(t) by a function f(t;p) where t is a two-dimensional vector:
.PP
.Vb 2
\&    #include "lmmin.h"
\&    #include <stdio.h>
\&
\&    /* fit model: a plane p0 + p1*tx + p2*tz */
\&    double f( double tx, double tz, const double *p )
\&    {
\&        return p[0] + p[1]*tx + p[2]*tz;
\&    }
\&
\&    /* data structure to transmit data arays and fit model */
\&    typedef struct {
\&        double *tx, *tz;
\&        double *y;
\&        double (*f)( double tx, double tz, const double *p );
\&    } data_struct;
\&
\&    /* function evaluation, determination of residues */
\&    void evaluate_surface( const double *par, int m_dat,
\&        const void *data, double *fvec, int *userbreak )
\&    {
\&        /* for readability, explicit type conversion */
\&        data_struct *D;
\&        D = (data_struct*)data;
\&
\&        int i;
\&        for ( i = 0; i < m_dat; i++ )
\&        fvec[i] = D\->y[i] \- D\->f( D\->tx[i], D\->tz[i], par );
\&    }
\&
\&    int main()
\&    {
\&        /* parameter vector */
\&        int n_par = 3; /* number of parameters in model function f */
\&        double par[3] = { \-1, 0, 1 }; /* arbitrary starting value */
\&
\&        /* data points */
\&        int m_dat = 4;
\&        double tx[4] = { \-1, \-1,  1,  1 };
\&        double tz[4] = { \-1,  1, \-1,  1 };
\&        double y[4]  = {  0,  1,  1,  2 };
\&
\&        data_struct data = { tx, tz, y, f };
\&
\&        /* auxiliary parameters */
\&        lm_status_struct status;
\&        lm_control_struct control = lm_control_double;
\&        control.verbosity = 3;
\&
\&        /* perform the fit */
\&        printf( "Fitting:\en" );
\&        lmmin( n_par, par, m_dat, (const void*) &data, evaluate_surface,
\&               &control, &status );
\&
\&        /* print results */
\&        printf( "\enResults:\en" );
\&        printf( "status after %d function evaluations:\en  %s\en",
\&                status.nfev, lm_infmsg[status.outcome] );
\&
\&        printf("obtained parameters:\en");
\&        int i;
\&        for ( i=0; i<n_par; ++i )
\&        printf("  par[%i] = %12g\en", i, par[i]);
\&        printf("obtained norm:\en  %12g\en", status.fnorm );
\&
\&        printf("fitting data as follows:\en");
\&        double ff;
\&        for ( i=0; i<m_dat; ++i ){
\&            ff = f(tx[i], tz[i], par);
\&            printf( "  t[%2d]=%12g,%12g y=%12g fit=%12g residue=%12g\en",
\&                    i, tx[i], tz[i], y[i], ff, y[i] \- ff );
\&        }
\&
\&        return 0;
\&    }
.Ve
.SS "More examples"
.IX Subsection "More examples"
For more examples, see the homepage and directories demo/ and test/ in the source distribution.
.SH "COPYING"
.IX Header "COPYING"
Copyright (C):
   1980\-1999 University of Chicago
   2004\-2015 Joachim Wuttke, Forschungszentrum Juelich GmbH
.PP
Software: FreeBSD License
.PP
Documentation: Creative Commons Attribution Share Alike
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\fBlmcurve\fR(3)
.PP
Homepage: http://apps.jcns.fz\-juelich.de/lmfit
.SH "BUGS"
.IX Header "BUGS"
Please send bug reports and suggestions to the author <j.wuttke@fz\-juelich.de>.