自定义非线性人口普查拟合
此示例说明如何对人口普查数据进行自定义方程拟合,指定边界、系数和问题相关参数。
加载 census.mat 中的数据并绘图:
load census plot(cdate,pop,'o') hold on
为自定义非线性模型 y = a(x-b)n 创建一个拟合 options 结构体和一个 fittype 对象,其中 a 和 b 是系数,n 是与问题相关的参数。有关与问题相关的参数的更多详细信息,请参阅 fittype 函数页。
s = fitoptions('Method','NonlinearLeastSquares',... 'Lower',[0,0],... 'Upper',[Inf,max(cdate)],... 'Startpoint',[1 1]); f = fittype('a*(x-b)^n','problem','n','options',s);
使用拟合选项和值 n = 2 拟合数据:
[c2,gof2] = fit(cdate,pop,f,'problem',2)
c2 = General model: c2(x) = a*(x-b)^n Coefficients (with 95% confidence bounds): a = 0.006092 (0.005743, 0.006441) b = 1789 (1784, 1793) Problem parameters: n = 2
gof2 = struct with fields:
sse: 246.1543
rsquare: 0.9980
dfe: 19
adjrsquare: 0.9979
rmse: 3.5994
使用拟合选项和值 n = 3 拟合数据:
[c3,gof3] = fit(cdate,pop,f,'problem',3)
c3 = General model: c3(x) = a*(x-b)^n Coefficients (with 95% confidence bounds): a = 1.359e-05 (1.245e-05, 1.474e-05) b = 1725 (1718, 1731) Problem parameters: n = 3
gof3 = struct with fields:
sse: 232.0058
rsquare: 0.9981
dfe: 19
adjrsquare: 0.9980
rmse: 3.4944
绘制拟合结果和数据:
plot(c2,'m') plot(c3,'c') legend('data','fit with n=2','fit with n=3')