# Matlab通过自定义方程实现数据拟合

4.5
(4)

## fittype帮助文件

``````>> help fittype
fittype   Fittype for curve and surface fitting

A fittype encapsulates information describing a model. To create a
fit, you need data, a fittype, and (optionally) fit options and an
exclusion rule.

LIBRARY MODELS

fittype(LIBNAME) constructs a fittype for the library model
specified by LIBNAME.

Choices for LIBNAME include:

LIBNAME           DESCRIPTION
'poly1'           Linear polynomial curve
'poly11'          Linear polynomial surface
'linearinterp'    Piecewise linear interpolation
'cubicinterp'     Piecewise cubic interpolation
'smoothingspline' Smoothing spline (curve)
'lowess'          Local linear regression (surface)

or any of the names of library models described in "List of Library Models
for Curve and Surface Fitting" in the documentation.

CUSTOM MODELS

fittype(EXPR) constructs a fittype from the MATLAB expression
contained in the string, cell array or anonymous function EXPR.

The fittype automatically determines input arguments by searching
EXPR for variable names (see SYMVAR). In this case, the fittype
assumes 'x' is the independent variable, 'y' is the dependent
variable, and all other variables are coefficients of the model. If
no variable exists, 'x' is used.

All coefficients must be scalars. You cannot use the following
coefficient names in the expression string EXPR: i, j, pi, inf,
nan, eps.

If EXPR is a string or anonymous function, then the toolbox uses a
nonlinear fitting algorithm to fit the model to data. To use a
linear fitting algorithm, use a cell array of terms.

ANONYMOUS FUNCTIONS

If EXPR is an anonymous function, then the order of inputs must be
correct. The input order enables the fittype class to determine
which inputs are coefficients to estimate, problem-dependent
parameters and independent variables. The order of the input
arguments to the anonymous function must be:

EXPR = @(<coefficients>, <problem parameters>, <x>, <y>) expression

There must be at least one coefficient. The problem parameters and
y are optional. The last arguments, x and y, represent the
independent variables: just x for curves, but x and y for surfaces.
If you don't want to use x and/or y as the names of the independent
variables, then you can specify different names by using the
'independent' property name/value pair. However, whatever name or
names you choose, these arguments must be the last arguments to the
anonymous function.

Anonymous functions make it easier to pass other data into the
fittype and fit functions. For example, to create a fittype using
an anonymous function and variables xs and ys from the workspace:

% Variables in workspace
xs = (0:0.1:1).';
ys = [0; 0; 0.04; 0.1; 0.2; 0.5; 0.8; 0.9; 0.96; 1; 1];
% Create fittype
ft = fittype( @(b, h, x) interp1( xs, b+h*ys, x, 'pchip' ) )
xdata = [0.012;0.054;0.13;0.16;0.31;0.34;0.47;0.53;0.53;...
0.57;0.78;0.79;0.93];
ydata = [0.78;0.87;1;1.1;0.96;0.88;0.56;0.5;0.5;0.5;0.63;...
0.62;0.39];
% Fit the curve to the data
f = fit( xdata, ydata, ft, 'Start', [0, 1] )

LINEAR MODELS

To use a linear fitting algorithm specify EXPR as a cell array of
terms. That is, to specify a linear model of the following form:

coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...

(where no coefficient appears within any of term1, term2, etc) use
a cell array where each term, without coefficients, is specified in
a cell of EXPR, as follows:

EXPR = {'term1', 'term2', 'term3', ... }

For example, the model

a*x + b*sin(x) + c

is linear in 'a', 'b' and 'c'. It has three terms 'x', 'sin(x)' and
'1' (since c=c*1) and so EXPR is

EXPR = {'x','sin(x)','1'}

fittype(EXPR,PROP1,VALUE1,PROP2,VALUE2,....) uses the property
name/value pairs PROP1-VALUE1, PROP2-VALUE2 to specify property
values other than the default values.

PROPERTY         DESCRIPTION
'independent'    Specifies the independent variable name
'dependent'      Specifies the dependent variable name
'coefficients'   Specifies the coefficient names (in a cell array
if there are two or more). Note excluded names
above.
'problem'        Specifies the problem-dependent (constant) names
(in a cell array if there are two or more)
'options'        Specifies the default 'FITOPTIONS' for this
equation

Defaults: The independent variable is x.
The dependent variable is y.
There are no problem dependent variables.
Everything else is a coefficient name.

Multi-character symbol names may be used.

EXAMPLES

g = fittype('a*x^2+b*x+c')
g = fittype('a*x^2+b*x+c','coeff',{'a','b','c'})
g = fittype('a*time^2+b*time+c','indep','time')
g = fittype('a*time^2+b*time+c','indep','time','depen','height')
g = fittype('a*x+n*b','problem','n')
g = fittype({'cos(x)','1'})                            % linear
g = fittype({'cos(x)','1'}, 'coefficients', {'a','b'}) % linear
g = fittype( @(a,b,c,x) a*x.^2+b*x+c )
g = fittype( @(a,b,c,d,x,y) a*x.^2+b*x+c*exp(-(y-d).^2), ...
'independent', {'x', 'y'}, ...
'dependent', 'z' ); % for fitting surfaces``````

## 数据写入

``````load census
plot(cdate,pop,'o')
hold on``````

## 数据拟合

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

``[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
rmse: 3.5994``````

``[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 =

sse: 232.0058
rsquare: 0.9981
dfe: 19
rmse: 3.4944``````

``````plot(c2,'m')
plot(c3,'c')
legend('data','fit with n=2','fit with n=3')``````

(2)

0
古哥
2023年06月03日

0
智能AI
2023年10月03日

0
古哥
2020年11月12日

0
古哥
2020年12月30日