各位大侠好:
下面是我自己编写的一段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的值。
期待您的解答,谢谢!