![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/333492/image.jpeg)
Lsqcurvefit and Fitnlm has different outputs while algorithm for Lsqcurvefit is specified as levenberg-marquardt
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Alex Sha
2020년 7월 18일
Hi, Dai, the result is obtained by using 1stOpt, a software package rather than Matlab, with its unique global optimization algorithm, 1stOpt is much easy for using, and without guessing or providing initial-start values. There are too much solutions for your fitting problem without range bound, three more solutions are as below:
1:
Root of Mean Square Error (RMSE): 0.00223449392170631
Sum of Squared Residual: 0.000274612969737834
Correlation Coef. (R): 0.999989837328843
R-Square: 0.999979674760966
Adjusted R-Square: 0.999978893021003
Determination Coef. (DC): 0.999979674278087
Chi-Square: 0.000157144199684822
F-Statistic: 282874.61080769
Parameter Best Estimate
---------- -------------
b1 9.25410968873918
b2 3.99694082424586E-13
b3 0.000455605572938445
b4 7.25638845784608E-9
b5 956567.424365059
b6 38.0282336902714
b7 0.00177804385932587
b8 -1.10835924481827E-5
b9 -0.00149481781641168
2:
Root of Mean Square Error (RMSE): 0.00223449392525441
Sum of Squared Residual: 0.000274612970609939
Correlation Coef. (R): 0.999989837328834
R-Square: 0.999979674760949
Adjusted R-Square: 0.999978893020985
Determination Coef. (DC): 0.999979674278022
Chi-Square: 0.000157144200260759
F-Statistic: 282874.609356819
Parameter Best Estimate
---------- -------------
b1 -0.000318654886709198
b2 -1.16075824722554E-8
b3 -0.00348982974083378
b4 -9.47338758898367E-10
b5 32.6383346262049
b6 293.285077343668
b7 -1.04336329581581E-5
b8 -3.06752419084964E-5
b9 -2.42766250031831E-5
3
Root of Mean Square Error (RMSE): 0.00223451297263129
Sum of Squared Residual: 0.000274617652367164
Correlation Coef. (R): 0.999989837288749
R-Square: 0.999979674680779
Adjusted R-Square: 0.999978892937732
Determination Coef. (DC): 0.999979673931498
Chi-Square: 0.000157129678084711
F-Statistic: 282870.008321204
Parameter Best Estimate
---------- -------------
b1 0.00054550948071565
b2 6.78040292313274E-9
b3 0.0745578109891404
b4 4.43425612655323E-11
b5 56.1004573021241
b6 6272.91381330081
b7 -1.36422951294635E-5
b8 0.000141918274494963
b9 -0.000147419235734852
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!