5.0

5.0 | 5 ratings Rate this file 38 Downloads (last 30 days) File Size: 210 KB File ID: #39564
image thumbnail

LMFnlsq2

by

 

21 Dec 2012 (Updated )

Solution of one or more nonlinear equations in the least squares sense.

| Watch this File

File Information
Description

The function is an improved version of the function LMFnlsq widely tested on the nonlinear regression, curve fitting and identification problems. Better description is in the complementing file LMFnlsq2test.pdf.

Warning: The option 'Display' is changed for better control of intermediate results output. See help LMFnlsq2.

Required Products MATLAB
MATLAB release MATLAB 7.3 (R2006b)
Other requirements The demo program LMFnlsq2test needs functions inp, fig, separator and LMFnlsq2 from File Exchange
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (16)
15 Jul 2014 Miroslav Balda

@Liu
The answer is trivial:
Let you have one set of 12 values of lefthand side, say in the column vector hmnp. Make a function for evaluation of differences (residuals) between righthand sides and lefthand sides, say resid.m in the form

function r = resid(x)
r = [x(1)+x(2)*p+ ... +(x(11)+x(12)*p)*n] - hmnp;

Here x(1) up to x(6) correspond C1 to C6 and x(7) up to x(12) correspon D1 up to D6. The simplest call of the function to solve the problem could be

[x,ssq,cnt] = LMFnlsq2('resid',x0);

A better way would be

[x,ssq,cnt] = LMFnlsq2('resid',x0,'Display',-50);

Of course that you have to supply a good estimate of the solution - coefficients C1, ..., D6. It is very important that you are able to do it, say from a nature of your experiment. The elements of x0 must not be zeros!

Since THe solution of your task has as many unknowns as equations, there is no
any degree of freedom. In consequence of it the solution should yield ssq->0.

I wish you good luck.

Mira

15 Jul 2014 Liu

Hi, Balda. Good day.
My curve fitting prblem is to calibrate the model like this:
h(m,n,p)=[C1+C2*P+(C3+C4*P)*m+(C5+C6*P)*n]/)[D1+D2*P+(D3+D4*P)*m+(D5+D6*P)*n]
The parameters to be estimate are C1,C2,C3,C4,C5,C6,D1,D2,D3,D4,D5,D6. And we have obtained 12 sets of data of h(m,n,p). So how to write the L-M expression using the LMFnlsq2.m ?

15 Jul 2014 Liu

Worked flawless for my application, cheers

23 Jun 2014 Miroslav Balda

@Mihail
Yes, LMFnlsq2 is able to solve both real and complex problems, however, the complex task should be split into real and imagimary parts in the function for evaluating residuals. Thus, the dimension of the residual vector is double the number of equations, provided there are no additional conditions. The column vector of residuals should be real.

See the function cxroot.m:

www.mathworks.com/matlabcentral/fileexchange/22661

23 Jun 2014 Mihail Cherkasskii

Hi, Balda!

Does LMFnlsq2 work with complex parameters, which are required to estimate?

22 Oct 2013 James

Worked flawless for my application, cheers.

28 Aug 2013 xinxing  
26 Aug 2013 Miroslav Balda

@xinxing
Build M-function, say resid.m, for evaluation of problem residuals:

function res = resid(p)
%xxxxxxxxxxxxxxxxxxxxxx
global t Y Wp % Wp = penalty weight
% If you wish to work with the native names of variables (not with the elements of the sought parameters p(i), use the following command:
[A,B,C1,C2,m,w,Tc] = deal2('_elm_',p); % deal2 is the function from the File Exchange
% http://www.mathworks.com/matlabcentral/fileexchange/32547
Tct = Tc - t;
lTt = log(Tct);
res = [A + B*Tct.^m + C1*cos(w*lTt) + C2*sin(w*lTt) - Y
(B>0)*B*Wp
(abs(C1)>1)*(abs(C1)-1)*Wp
(abs(C2)>1)*(abs(C2)-1)*Wp
(Tc<=max(t))*(Tc-max(t))*Wp
(m<0.1)*(m+0.1)*Wp
(m>0.9)*(m-0.9)*Wp
(w<6)(w+6)*Wp
(w>13)*(w-13)*Wp];

The user should choose propper weight(s) of penalties expressing constraints put on parameters.

21 Aug 2013 xinxing

Hi, Balda
Thanks for your reply. My curve fitting prblem is to calibrate the model like this:
Y(i)=A+B*(Tc-t(i))^m+C1*cos(w*ln(Tc-t(i))) +C2*sin(w*ln(Tc-t(i)))
The parameters to be estimate is A,B,C1,C2,m,w,Tc,
and the Constraints is:
B<0, |C1|<1, |C2|<1, Tc>max(t(i)), 0.1<m<0.9, 6<w<13, So how to write the L-M expression using the LMFnlsq2.m ?

21 Aug 2013 Miroslav Balda

Yes, you may make curve-fitting with constraints, if you introduce convenient penalty functions, which generate additional elements in the vector of residuals. It has been used in the solution of the Problem 3, Constrained Rosenbrock valey, described in the file LMFnlsq2test.m.

21 Aug 2013 xinxing

Can the function do Curve fiting with constraints?
How?

02 Apr 2013 Jose

what is the use of Basdx if it is not used
plus it takes the value 0.25*xf.Xtol so Xtol should be enough
and in the comments it should be 25e-9 ???

02 Apr 2013 Jose

actually LMFnlsqtest.m should have all the LMFnlsq replaced with LMFnlsq2

LMFnlsq.m:
line 191 xf.Basdx = 0.25*xf.Xtol;
should be xf.Basdx = 0.25*xf.XTol;

02 Apr 2013 Jose

LMFnlsqtest.m on line 29 should be:
if ~exist('LMFnlsq2.m','file'), Id = [Id 'LMFnlsq2 (Id=39564 )']; end

01 Apr 2013 Jose

Line 286, it should be elseif lb~=n

elseif le~=n
error(['Dimensions of vector dx ',num2str(lb),'~=',num2str(lx)]);
end

01 Apr 2013 Jose

Lines 301 and 311 do the same, one should be enough:

SS = r'*r;

feval(printf,ipr,-1); % Table header
dx = zeros(n,1);
res= 1;
cnt=0;
feval(printf,ipr,cnt,res,SS,x,dx,l,lc) % Initial state

[A,v] = getAv(FUN,JAC,x,r,bdx);
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SS = r'*r;

Updates
15 Apr 2013

Display of intermediate results has been improved to fulfill better users' requirements.

Thanks Jose's detailed study of the code, small bugs were repaired and the code made more clear.

16 Apr 2013

Repaired several bugs found by Jose. Improved description of the function and of the demo program LMFnlsq2test.

22 Apr 2013

The new version of the function is a slight modification of the recent update. One statement, dated 2012-12-01, has been commented out because of its erroneous function under certain circumstances. The function is now fast and much more stable.

23 Apr 2013

Improved the printout of results. The previous versions did not display the final solution if the iteration count were not integer multiple of required step of printing.

Contact us