MATLAB拟合中的fit用法

MATLAB 拟合

fit

Fit model to data

Syntax

cfun = fit(xdata,ydata,libname)
cfun = fit(…,PropName,PropVal,…)
cfun = fit(xdata,ydata,libname,options)
cfun = fit(xdata,ydata,ffun,…)
cfun = fit(…,’problem’,vals)
[cfun,gof] = fit(…)
[cfun,gof,output] = fit(…)

Description

cfun = fit(xdata,ydata,libname) fits the data in the column vectors xdata and ydata with the library model specified by libname. xdata and ydata cannot contain Inf or NaN. Only the real parts of complex data are used in the fit. You can display library model names with the cflibhelp function. The fit result is returned as a cfit object cfun.

cfun = fit(…,PropName,PropVal,…) fits the data using specified property name/value pairs. You can display the supported property names and values for specific library models with the fitoptions function.

cfun = fit(xdata,ydata,libname,options) fits the data using the options specified by the fit options structure options. Fit options structures are created with the fitoptions function.

cfun = fit(xdata,ydata,ffun,…) fits the data with the fittype object ffun. fittype objects are created with the fittype function.

cfun = fit(…,’problem’,vals) assigns vals to the problem-dependent parameters of the model before fitting. vals is a scalar or a cell array with one element per parameter.

[cfun,gof] = fit(…) returns goodness-of-fit statistics to the structure gof. The gof structure has the fields shown in the table below.

[cfun,gof,output] = fit(…)

returns the structure output, which contains information associated with the fitting algorithm. Fields depend on the algorithm. For example, the output structure for nonlinear least-squares algorithms has the fields shown in the table below.

Remarks

For some nonlinear library models (rational and Weibull), and all custom nonlinear models, default initial values for coefficients are selected uniformly at random from the interval (0,1). As a result, multiple fits using the same data and model may lead to different fitted coefficients. To avoid this, initial values for coefficients can be specified through a fitoptions structure or a vector value for the StartPoint property. Alternatively, the initial state of the random number generator rand can be set before fitting.

All other nonlinear library models automatically compute reasonable initial values. These initial values depend on the data, and are based on model-specific heuristics.

Example

Load and plot the data in census.mat:

load census
plot(cdate,pop,’o’)
hold on

Create a fit options structure and a fittype object for the custom nonlinear model y = a(x–b)n, where a and b are coefficients and n is a problem-dependent parameter:

s = fitoptions(‘Method’,’NonlinearLeastSquares’,…
‘Lower’,[0,0],…
‘Upper’,[Inf,max(cdate)],…
‘Startpoint’,[1 1]);
f = fittype(‘a*(x-b)^n’,’problem’,’n’,’options’,s);

Fit the data using the fit options and a value of 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 =
sse: 246.1543
rsquare: 0.9980
dfe: 19
adjrsquare: 0.9979
rmse: 3.5994

Fit the data using the fit options and a value of 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-005 (1.245e-005, 1.474e-005)
b = 1725 (1718, 1731)
Problem parameters:
n = 3
gof3 =
sse: 232.0058
rsquare: 0.9981
dfe: 19
adjrsquare: 0.9980
rmse: 3.4944

Plot the fit results with the data:

plot(c2,’m’)
plot(c3,’c’)

转载自 http://blog.sina.com.cn/s/blog_3ecbcc070101fjc6.html

猜你喜欢

转载自blog.csdn.net/starry_yi/article/details/81502111