4.0

4.0 | 7 ratings Rate this file 339 downloads (last 30 days) File Size: 9.15 KB File ID: #16063

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

by Miroslav Balda

 

23 Aug 2007 (Updated 11 Feb 2009)

Code covered by BSD License  

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

Download Now | Watch this File

File Information
Description

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.

Example:
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];
   [x,ssq,cnt]=LMFsolve(ros,[-1.2,1],'Display',1,'MaxIter',50)
Solution: x = [0.4556; 0.2059], |x| = 0.5000
sum of squares: ssq = 0.2966,
number of iterations: cnt = 51.

Notes:
* 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
    [x,ssq,cnt]=LMFsolve('rosen',[-1.2,1]);
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.

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

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
LMFnlsq - Solution of nonlinear least squares
This submission has inspired the following:
LMFnlsq - Solution of nonlinear least squares, Nlsqbnd

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.
Zip File Content  
Other Files LMFsolve.m,
LMFsolveOLD.m,
LMFsolvetest.m,
rosen.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (13)
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.
Thanks.

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...?

08 Oct 2007 Miroslav Balda

The new extended description and help solves both notes.

06 Dec 2007 Miroslav Balda

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

24 Jan 2008 Ki-Young Shin

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

16 Apr 2008 Nanang Firman  
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?

11 Feb 2009 Miroslav Balda

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".

18 May 2009 Hossein SOLEIMANI

Hi

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,

regards

18 May 2009 Hossein SOLEIMANI  
18 May 2009 Hossein SOLEIMANI  
24 May 2009 Kevin

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

01 Jun 2009 jeyasenthil  
Please login to add a comment or rating.
Updates
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

15 Nov 2007

Repaired description, complemented screnshot.
A better solution, which is more stable, has been sent to FEX, see ID 17534.

09 Jan 2009

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

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

11 Feb 2009

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

Tag Activity for this File
Tag Applied By Date/Time
optimization Miroslav Balda 22 Oct 2008 09:24:05
levenberg Miroslav Balda 22 Oct 2008 09:24:05
marquardt Miroslav Balda 22 Oct 2008 09:24:05
fletcher Miroslav Balda 22 Oct 2008 09:24:05
nonlinear least square Miroslav Balda 22 Oct 2008 09:24:05
overdetermined Miroslav Balda 22 Oct 2008 09:24:05
optimization CARLOS GUSTAVO 29 Apr 2009 09:17:47
levenberg Hossein SOLEIMANI 18 May 2009 05:45:54
fletcher Hossein SOLEIMANI 19 May 2009 09:36:31
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com