lmminsubroutine lmmin(func,x,y,iy,exitcode,x0,itermax,damp,tol,toleach,shock,usebro,iprint,tolrel,maxstall,lb,ub,maxstep)
implicit none
external :: func ! user-supplied function to be minimize
real(kind=8) , intent(out) :: x(:) ! values of "x" at minimum
real(kind=8) , intent(out) :: y(:) ! valuf of "func" at "x"
integer , intent(out) :: iy ! number of function evaluations
integer , intent(out) :: exitcode ! indicator of convergence
real(kind=8) , intent(in) :: x0(:) ! initial guess
real(kind=8) , intent(in) , optional :: shock ! shock to parameter values (as %)
real(kind=8) , intent(in) , optional :: damp ! initial damping factor
real(kind=8) , intent(in) , optional :: tol ! tolerance level for the sum of squared errors
real(kind=8) , intent(in) , optional :: toleach ! tolerance level for each equation
real(kind=8) , intent(in) , optional :: tolrel ! relative improvement below which an iteration is unproductive
integer , intent(in) , optional :: itermax ! max number of function evaluations
integer , intent(in) , optional :: iprint ! indicator for printing behaviour
integer , intent(in) , optional :: usebro ! indicator for the use of Broyden method to update Jacobian
integer , intent(in) , optional :: maxstall ! max number of consecutive unproductive iterations
real(kind=8) , intent(in) , optional :: lb(:) ! lower bounds for x
real(kind=8) , intent(in) , optional :: ub(:) ! upper bounds for x
real(kind=8) , intent(in) , optional :: maxstep ! max step in each unknown
This subroutine applies the Levenberg–Marquardt algorithm (click here for more information) which minimizes the sum of squared errors of a (possibly nonlinear) system of multivariate equations.
Given the current point, with residuals y and (numerical) Jacobian J, the step dx solves (J'J + lambda D) dx = -J'y, where D is a diagonal scaling matrix (the largest diagonal of J'J observed so far for each unknown, with a positive floor; Moré, 1978) and lambda is the damping factor. The linear system is solved with qrsolve (QR decomposition with column pivoting), after scaling it by its diagonal so that all the unknowns have the same scale. The damping factor is updated with the gain ratio (actual over predicted reduction in the error), as in Nielsen (1999).
The user should input the function func and the initial guess x0. The function func must be of the form:
function func(xvec) result(resid)
real(kind=8) :: xvec(:)
real(kind=8) , allocatable :: resid(:)
...
end function func
The size of x must be equal to the size of x0 (number of unknowns), and the size of y must be equal to the size of the vector returned by func (number of equations); the latter is not checked by the subroutine.
The subroutine returns the value(s) of x with the lowest sum of squared errors found, the residuals at that point (y), the number of function evaluations (iy), and an indicator, exitcode:
exitcode = 0: the algorithm found a root (sum of squared errors below tol, or every residual below toleach in absolute value)exitcode = 1: the gradient J'y (the projected gradient, with bounds) is close to zero (or the linear system cannot be solved)exitcode = 2: the step in x is close to zeroexitcode = 3: no progress: maxstall consecutive unproductive iterationsexitcode = 9: maximum number of function evaluations reachedOptionally, the user can also supply:
shock (double precision): relative shock to parameter values to compute the numerical Jacobian, default = 0.05. If the residuals do not change, the shock is doubled (at most 5 times); if they still do not change, the corresponding column of the Jacobian is set to zero.damp (double precision): initial damping factor, default = 1.0tol (double precision): tolerance level for the sum of squared errors, default = 1.0d-8toleach (double precision): tolerance level for each equation, default = 1.0d-8tolrel (double precision): relative improvement in the error below which an iteration is considered unproductive, default = 1.0d-6maxstall (integer): number of consecutive unproductive iterations after which the algorithm stops, default = 10itermax (integer): max number of function evaluations, default = 500iprint (integer): indicator for printing behaviour
iprint = 0: don’t print anything (default)iprint = 1: print main resultsiprint = 2: print main results and each iterationusebro (integer): indicator for the use of Broyden method to update Jacobian
usebro = 0: re-compute the Jacobian numerically after every successful step (default)usebro = 1: update the existing Jacobian using the Broyden method; a new numerical Jacobian is computed when a Broyden-updated Jacobian fails to produce a better pointlb, ub (double precision vectors of dimension “n”): lower and upper bounds for x (box constraints, lb <= x <= ub). Either or both can be supplied. With bounds, the algorithm is a projected Levenberg–Marquardt with an active set:
exitcode = 1) is checked with the projected gradient, which is zero at an optimum on the bound.Without lb and ub, the algorithm is exactly the unconstrained one.
maxstep (double precision): maximum size of the step in each unknown (a trust region), as a fraction of the width of the box (ub-lb) for unknowns with both bounds, and of max(1,|x|) otherwise. If a component of the step exceeds it, the whole step is scaled down, keeping its direction. By default there is no limit. A value such as 0.25 makes the algorithm more conservative when the objective is noisy or the Jacobian is a poor approximation far from the current point.Note: for constrained optimization problems, one can either supply lb and ub or make use of the normalize and denormalize subroutines. With lb and ub, the variables are not transformed.
Example with bounds
! minimize the sum of squares of "func" with 0 <= x(1) <= 1 and x(2) >= -5
call lmmin(func,x,y,iy,exitcode,x0,lb=(/0.0d0,-5.0d0/),ub=(/1.0d0,huge(1.0d0)/))
Internal dependencies: qrsolve, broyden
Updates: November 2026. (1) The shock used to compute a column of the Jacobian is enlarged at most 5 times when the residuals do not change (it was enlarged without limit, until itermax, and the test was on the sum of squared errors instead of the residuals); (2) scaling with a positive floor and Cholesky solve, instead of the inverse of (1+lambda) diag(J'J), which was singular with a zero column; (3) damping updated with the gain ratio; (4) stop after maxstall unproductive iterations (exitcode = 3) and relative test on the step; (5) points evaluated to compute the Jacobian are used when they improve on the current point, and the best point evaluated is returned; (6) optional box constraints lb and ub; (7) optional maximum step maxstep.