View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
LMFsolve.m: Levenberg-Marquardt-Fletcher algorithm for nonlinear least squares problems

3.3 | 12 ratings Rate this file 108 Downloads (last 30 days) File Size: 8.21 KB File ID: #16063 Version: 1.3
image thumbnail

LMFsolve.m: Levenberg-Marquardt-Fletcher algorithm for nonlinear least squares problems


Miroslav Balda (view profile)


23 Aug 2007 (Updated )

LMFsolve.m finds least-squares solution of an overdetermined system of nonlinear equations

| Watch this File

File Information

The function LMFsolve.m serves for finding optimal solution of an overdetermined system of nonlinear equations in the least-squares sense. The standard Levenberg- Marquardt algorithm was modified by Fletcher and coded in FORTRAN many years ago. LMFsolve is its essentially shortened version implemented in MATLAB and complemented by setting iteration parameters as options. This part of the code has been strongly influenced by Duane Hanselman's function mmfsolve.m. Next to it, a finite difference approximation of Jacobian matrix is appended to it as a nested subfunction as well as a function for dispaying of intermediate results.

Calling of the function is rather simple:
     [x,ssq,cnt] = LMFsolve(Equations,X0); % or
     [x,ssq,cnt] = LMFsolve(Equations,X0,'Name',Value,...); % or
     [x,ssq,cnt] = LMFsolve(Equations,X0,Options) % .
In all cases, the applied variables have the following meaning:
* Equations is a function name (string) or a handle defining a set of equations,
* X0 is vector of initial estimates of solutions,
* x is the least-squares solution,
* ssq is sum of squares of equation residuals,
* cnt is a number of iterations

In the first case of call, default values of options are used. The second form of call defines selected options as a set of Name/Value pairs. The last alternative simplifies the statement by introducing earlier defined structure Options of Name\Value pairs.

Field names of the structure options are:
'Display' for control of iteration results,
'MaxIter' for setting maximum number of iterations,
'ScaleD' for defining diagonal matrix of scales,
'FunTol' for tolerance of final function values,
'XTol' for tolerance of final solution increments.

The general Rosenbrock's function has the form
   f(x) = 100(x(2)-x(1)^2)^2 + (1-x(1))^2
Optimum solution gives f(x)=0 for x(1)=x(2)=1. Function f(x) can be expressed in the form
   f(x) = f1(x)^2 + f2(x)^2, where f1(x) = 10(x(2)-x(1)^2) and f2(x) = 1-x(1).
Values of the functions f1(x) and f2(x) can be used as residuals.
LMFsolve finds the solution of this problem in 19 iterations. The more complicated problem sounds:
Find the least squares solution of the Rosenbrock valey inside a circle of the unit diameter centered at the origin. It is necessary to build third function, which is zero inside the circle and increasing outside it. This property has, say, the next penalty function:
   f3(x) = sqrt(x(1)^2 + x(2)^2) - r, where r is a radius of the circle.
Its implementation using anonymous functions has the form
   R = @(x) sqrt(x'*x)-.5; % A distance from the radius r=0.5
   ros= @(x) [10*(x(2)-x(1)^2); 1-x(1); (R(x)>0)*R(x)*1000];
Solution: x = [0.4556; 0.2059], |x| = 0.5000
sum of squares: ssq = 0.2966,
number of iterations: cnt = 51.

* Users with old MATLAB versions, which have no anonymous functions implemented, have to call LMFsolve with named function for residuals.
For above example it is
where the function rosen.m is for the given problem of the form
    function r = rosen(x)
% Rosenbrock's valey with a constraint R = sqrt(x(2)^2+x(1)^2)-.5;
% Residuals:
    r = [10*(x(2)-x(1)^2) % first part
           1-x(1) % second part
           (R>0)*R*1000 % penalty
* The new version of the function LMFsolve is without erroneous part of analytical form of Jacobian matrix.
* The internal function printit.m has been replaced by the function of the same name taken from the more advanced function LMFnlsq (FEX Id 17534) because of much better form of output.
* An error causing an inclination of the previous version to instability has been removed. this step improved stability essentially, however, the a number of iterations increased, if the old version converged at all. However, much better behaviour has the full version of the Fletcher's algorithm, which is implemented in the function LMFnlsq (Id 17534).
* The old (unstable) version of the function is also inclided under the name LMFsolveOLD for those users who liked it.

Fletcher, R., (1971): A Modified Marquardt Subroutine for Nonlinear Least Squares. Rpt. AERE-R 6799, Harwell


Lm Fnlsq Solution Of Nonlinear Least Squares inspired this file.

This file inspired Nlsqbnd and Lm Fnlsq Solution Of Nonlinear Least Squares.

MATLAB release MATLAB 7.3 (R2006b)
Other requirements The demo script LMFsolvetest exploids the function inp.m which may be found in FEX under Id number 9033. Please, inform me on bugs or possible improvements straight by e-mail.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (21)

LMFsave.m Do any1 have implemented the code for LM Method for QMF design..urgent help required

30 Oct 2016 ahmed talib

Hi every one
please any one can tell me how I used this package . I download the file but I cannot find the procedure to run the main file and put my data set and equation

16 Aug 2013 Sam Fincham

Fantastic submission, thank you. : )

26 Mar 2013 Miroslav Balda

Miroslav Balda (view profile)

Sorry for late reply. I recommend you to use the function LMFnlsq or LMFnlsq2, that are also in File Exchange. The advantage is that both functions are maintained, while LMFsolve not. Both functions are also more stable and use almost equal data files. As far as any constraines is concerned, they may be solved by introduction of additional "penalty" residual for k-th unknown, say


where w is a suitable weight, the value of which is found by experiment. If you have more troubles, you may send me your data and i'll try to solve it.

Comment only
12 Mar 2013 Tomas

Tomas (view profile)

Dear Dr. Balda,
I've been trying to implement your code to a calibration problem I have with many variables. 9 fixed and one more for each measurement. I believe it is for this reason that your function has not been giving me consistent results and I end up iterating more than 1000 times. I already defined the funtions I want to minimize(the residuals) but I would like to add a restriction to some of the variables. The restriction would be something like I don't want to allow the absolut value of some of the x's to be greater than 30. How would you go about that?

Comment only
06 Oct 2011 Julien Piot

Thank you, Professor Balda. Very useful.

21 Oct 2010 Sandra Glisic

Dear Dr Balda, I need help. Could you tell me can I use this LM application (algorithm) to sole a 6x6 matrix? (6 nonlinear equation, 6 variable to find 6 constant). Thank you in advance

Comment only
16 Apr 2010 Kristofer Kusano

01 Jun 2009 jeyasenthil

24 May 2009 Kevin

Kevin (view profile)

Professor Balda:
I truly appreciate you for helping me out to solve non-linear equations. Your code beautifully worked out for me. Also, thanks again to correct matrix in your weekend time.
Dr. Balda, Thanks again for your great help and prompt support.
D. Kim

Comment only
18 May 2009 Hossein SOLEIMANI

18 May 2009 Hossein SOLEIMANI

18 May 2009 Hossein SOLEIMANI


I want to solve a set of nonlinear equations that has a degree of freedom of 4 , and I want to use the experimental data in order to find the best results.
1. I have read the explanation about the FUN and I dont know how to put the funtions and also where I should include my experimental data?

please help me,


Comment only
11 Feb 2009 Miroslav Balda

Miroslav Balda (view profile)

To Judith. What is wrong with your computer is difficult to find from your message. If you used the script LMFsolvetest, which is a part of the zipped file, you would get the solution. The stability has been reached in this case by a careful selection of optional parameters. It is written in the description, that the code is not perfectly stable and that it is better to use the function LMFnlsq (See the comment dated 06 Dec 2007).
If you try the old version (LMFsolveOLD), you would get also the answer. The solution of the problems could be solved much easier, if you read the last sentence in the section "Other requirements".

Comment only
10 Feb 2009 Judith Brands

Analysing the example in the introduction of the file (Rosenbrock) gives different results on my computer as given in the example (cnt=7; ssq much larger). Furthermore, the results using this M-File are identical as to the results obtained with fminsearch? What goes wrong on my computer?

16 Apr 2008 Nanang Firman

24 Jan 2008 Ki-Young Shin

Wow!! I have been looking for LM algorithm (m file) for a long time.

06 Dec 2007 Miroslav Balda

The function LMFsolve can be replaced by far more stable function LMFnlsq, FEX Id 17534

Comment only
08 Oct 2007 Miroslav Balda

The new extended description and help solves both notes.

Comment only
05 Oct 2007 Ilpo Nicholson

I cannot either understand the example (Rosenbrock function minimization with constraint) and the code (first & second part, penalty function). Maybe a some other curve fitting example would be more rewarding...?

Comment only
04 Oct 2007 Xie Gang

Excuse me, sir. I can't catch the command:
R = @(x) sqrt(x'*x)-.5; % A distance from the radius r=0.5
ros= @(x) [10*(x(2)-x(1)^2); 1-x(1); (R(x)>0)*R(x)*1000];
I use MATLAB6.5.

04 Oct 2007

improved description and help part of the code

04 Oct 2007

Improvements in description and help part

08 Oct 2007

Extended description

09 Jan 2009 1.1

Removed erroneous part for analytical form of evaluation of Jacobian matrix. Introduced new function printit.m for better display of iteration results. Removed bug, which cased lower stability of the iteration process.

09 Jan 2009 1.2

Removed part of analytical Jacobian matrix causing errors. Removed bug destabilizing the iteration process. Implemented new function for printing intermediate results.

11 Feb 2009 1.3

Improved description of the function behavior. Removed bugs in the function LMFsolveOLD names within the code.

Contact us