各位大俠好:
下面是我自己編寫的一段matlab曲線拟合程式:
clc
clear
xx = 1:1:20;
xx = xx';
yy = exp(xx);
hold on;
plot(xx,yy,'o');
start = [1 1 1];
s = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[-Inf,-Inf,-Inf],...
'Upper',[Inf,Inf,Inf],...
'Startpoint',start);
f = fittype('a*x^b+c','options',s);
[coe,gof] = fit(xx,yy,f);
coe
gof
plot(coe,'r');
grid on;
拟合的結果很差:
General model:
coe(x) = a*x^b+c
Coefficients (with 95% confidence bounds):
a = 2.035 (-6885, 6889)
b = 4.595 (-1139, 1148)
c = 1.006 (-8.515e+07, 8.515e+07)
gof =
sse: 2.6959e+17
rsquare: -0.1105
dfe: 17
adjrsquare: -0.2411
rmse: 1.2593e+08
後來我采用matlab toolbox 中的curve fitting tool 拟合的結果很好
General model Power2:
f(x) = a*x^b+c
Coefficients (with 95% confidence bounds):
a = 5.2e-17 (1.568e-17, 8.832e-17)
b = 19.19 (18.96, 19.43)
c = 0 (-6.271e+05, 6.271e+05)
Goodness of fit:
SSE: 2.398e+13
R-square: 0.9999
Adjusted R-square: 0.9999
RMSE: 1.188e+06
查找原因發現主要是startpoint設定的問題,當我把startpoint設定為[5.2e-17 19.19 0]時,用我原來的程式也可以得到同樣好的結果。
tool box中生成的m代碼如下:
% --- Create fit "fit 3"
ok_ = isfinite(xx) & isfinite(yy);
if ~all( ok_ )
warning( 'GenerateMFile:IgnoringNansAndInfs',...
'Ignoring NaNs and Infs in data.' );
end
st_ = [222.34950513063478184 4.0772761680206128787 28384745.637977682054 ];
ft_ = fittype('power2');
% Fit this model using new data
cf_ = fit(xx(ok_),yy(ok_),ft_,'Startpoint',st_);
但是我将這個代碼中的startpoint帶入我原來的代碼中卻得不到這樣的結果。
我想知道:
1. 為什麼我用與toobox中同樣的startpoint卻得不到與toolbox同樣的結果,而且toolbox中的startpoint與最終的abc值相差非常遠,問題出在哪裡,該怎麼解決。
2. matlab tool box中生成的代碼并沒有startpoint的生成方法,我想知道這個值是怎麼得出來的,我應該怎樣通過程式求得startopint的值。
期待您的解答,謝謝!