Foortran Toolkit

Fortran toolkit
Borja Petit


Download toolkit

Back to index

lmmin

subroutine 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:

Optionally, the user can also supply:

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.