Lsqcurvefit and Fitnlm has different outputs while algorithm for Lsqcurvefit is specified as levenberg-marquardt

4 views (last 30 days)
Ruimin Dai
Ruimin Dai on 8 Jul 2020
Answered: Ruimin Dai on 14 Aug 2020
Hi guys! Has anyone tried to compare the outputs of Lsqcurvefit and Fitnlm? I have specified the algorithm as levenberg-marquardt for the Lsqcurvefit, since Fitnlm is also based on this algorithm, the outcomes of these two functiones should be the same. However, the results show that they are totally different, and Fitnlm gets the right answer. The codes are below, what should I do to make Lsqcurvefit output the same result as Fitnlm?
syms w real; % w acts as predictor
N_coeff=9; % 9 coefficients to be estimated
b=sym('b',[1 N_coeff],'real'); % b represents the coefficient to be estimated
beta0=[100e-6,3.506e-8,100e-6,3.506e-8,5,5,5e-6,5e-6,5e-6];% initial values for b
%Lsqcurvefit Start
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt'); % specify levenberg-marquardt for Lsqcurvefit
lb=[];
ub=[];
w=[2*pi*112000,2*pi*111000,2*pi*110000,2*pi*109000,2*pi*108000,2*pi*107000,2*pi*106000,2*pi*105000,2*pi*104000,2*pi*103000,2*pi*102000,2*pi*101000,2*pi*100000,2*pi*99000,2*pi*98000,2*pi*97000,2*pi*96000,2*pi*95000,2*pi*94000,2*pi*93000,2*pi*92000,2*pi*91000,2*pi*90000,2*pi*89000,2*pi*88000,2*pi*87000,2*pi*86000,2*pi*85000,2*pi*84000,2*pi*83000,2*pi*82000,2*pi*81000,2*pi*80000,2*pi*79000,2*pi*78000,2*pi*77000,2*pi*76000,2*pi*75000,2*pi*74000,2*pi*73000,2*pi*72000,2*pi*71000,2*pi*70000,2*pi*69000,2*pi*68000,2*pi*67000,2*pi*66000,2*pi*65000,2*pi*64000,2*pi*63000,2*pi*62000,2*pi*61000,2*pi*60000,2*pi*59000,2*pi*58000];
Respo=[1.05,1.06, 1.08, 1.09, 1.11, 1.13, 1.15, 1.17,1.20,1.22,1.25,1.28, 1.32,1.36,1.40,1.45,1.50,1.56,1.62,1.69,1.76,1.82,1.89,1.94,1.98,1.999,1.997,1.98,1.94, 1.88,1.81, 1.72, 1.62, 1.51, 1.40, 1.29, 1.18,1.09,1.00, 0.92, 0.85,0.79,0.73,0.68,0.64,0.60,0.56,0.53,0.50,0.48,0.46,0.44,0.42,0.41,0.39];
MolFun=@(b,w) ((300*b(1)*b(2)*w.^2 + 27*b(2)*b(4)*w.^2 + 300*b(3)*b(4)*w.^2 + 90*b(2)*b(4)*b(5)*w.^2 + 90*b(2)*b(4)*b(6)*w.^2 + 300*b(2)*b(4)*b(7)^2*w.^4 + 300*b(2)*b(4)*b(8)^2*w.^4 + 300*b(2)*b(4)*b(9)^2*w.^4 + 1000*b(2)*b(4)*b(5)*b(8)^2*w.^4 + 1000*b(2)*b(4)*b(6)*b(7)^2*w.^4 - 300*b(1)*b(2)*b(3)*b(4)*w.^4 + 300*b(2)*b(4)*b(5)*b(6)*w.^2 - 300).^2 + (90*b(2)*w + 90*b(4)*w + 1000*b(2)*b(7)^2*w.^3 + 1000*b(4)*b(8)^2*w.^3 + 300*b(2)*b(5)*w + 300*b(4)*b(6)*w - 90*b(1)*b(2)*b(4)*w.^3 - 90*b(2)*b(3)*b(4)*w.^3 - 1000*b(1)*b(2)*b(4)*b(8)^2*w.^5 - 1000*b(2)*b(3)*b(4)*b(7)^2*w.^5 - 300*b(1)*b(2)*b(4)*b(6)*w.^3 - 300*b(2)*b(3)*b(4)*b(5)*w.^3 + 2000*b(2)*b(4)*b(7)*b(8)*b(9)*w.^5).^2).^(1/2)./(10*((30*b(2)*w + 30*b(4)*w + 100*b(2)*b(5)*w + 100*b(4)*b(6)*w - 30*b(1)*b(2)*b(4)*w.^3 - 30*b(2)*b(3)*b(4)*w.^3 - 100*b(1)*b(2)*b(4)*b(6)*w.^3 - 100*b(2)*b(3)*b(4)*b(5)*w.^3).^2 + (100*b(1)*b(2)*w.^2 + 9*b(2)*b(4)*w.^2 + 100*b(3)*b(4)*w.^2 + 30*b(2)*b(4)*b(5)*w.^2 + 30*b(2)*b(4)*b(6)*w.^2 + 100*b(2)*b(4)*b(9)^2*w.^4 - 100*b(1)*b(2)*b(3)*b(4)*w.^4 + 100*b(2)*b(4)*b(5)*b(6)*w.^2 - 100).^2).^(1/2));
mdl_lsq=lsqcurvefit(MolFun,beta0,w, Respo,lb,ub,options)
% Lsqcurvefit End, fitnlm Start.
Pred=[2*pi*112000;2*pi*111000;2*pi*110000;2*pi*109000;2*pi*108000;2*pi*107000;2*pi*106000;2*pi*105000;2*pi*104000;2*pi*103000;2*pi*102000;2*pi*101000;2*pi*100000;2*pi*99000;2*pi*98000;2*pi*97000;2*pi*96000;2*pi*95000;2*pi*94000;2*pi*93000;2*pi*92000;2*pi*91000;2*pi*90000;2*pi*89000;2*pi*88000;2*pi*87000;2*pi*86000;2*pi*85000;2*pi*84000;2*pi*83000;2*pi*82000;2*pi*81000;2*pi*80000;2*pi*79000;2*pi*78000;2*pi*77000;2*pi*76000;2*pi*75000;2*pi*74000;2*pi*73000;2*pi*72000;2*pi*71000;2*pi*70000;2*pi*69000;2*pi*68000;2*pi*67000;2*pi*66000;2*pi*65000;2*pi*64000;2*pi*63000;2*pi*62000;2*pi*61000;2*pi*60000;2*pi*59000;2*pi*58000];
Obse=[1.05;1.06; 1.08; 1.09; 1.11; 1.13; 1.15; 1.17;1.20;1.22;1.25;1.28; 1.32;1.36;1.40;1.45;1.50;1.56;1.62;1.69;1.76;1.82;1.89;1.94;1.98;1.999;1.997;1.98;1.94; 1.88;1.81; 1.72; 1.62; 1.51; 1.40; 1.29; 1.18;1.09;1.00; 0.92; 0.85;0.79;0.73;0.68;0.64;0.60;0.56;0.53;0.50;0.48;0.46;0.44;0.42;0.41;0.39];
mdl_fitnlm=fitnlm(Pred,Obse,MolFun,beta0)
%Fitnlm end
The result of Lsqcurvefit is:
0.0001 0.0000 0.0001 0.0000 5.0000 5.0000 0.0000 0.0000 0.0000
The result of fitnlm is:
b1 : 9.9744e-05 b2:3.7083e-08 b3 : 9.7074e-05 b4:3.4057e-08 b5:10.01 b6:7.8665 b7: 5.8374e-06 b8: 5.1161e-06 b9:2.2653e-06
The two outcomes are totally different and the fitnlm gets the right answer. Is that because of the algorithm of fitnlm? In the help center https://www.mathworks.com/help/stats/nlinfit.html#btle092-6 it mentiones that for nonrobust estimation, nlinfit uses the Levenberg-Marquardt nonlinear least squares algorithm; while for robust estimation, nlinfit uses an iterative reweighted least squares algorithm. Is that because fitnlm use rative reweighted least squares algorithm instead of Levenberg-Marquardt in this case? What can I do to make Lsqcurvefit output the same answer as fitnlm?
Thanks in advance!
  4 Comments

Sign in to comment.

Accepted Answer

Ruimin Dai
Ruimin Dai on 14 Aug 2020
Hi,I'm not sure if I could answer the question by myself. Actually it's caused by a very very simple problem: format. By typing "format shortE" in the command window, then run the code agian, the result will be much more accurate.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by