Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

fit

对数据进行曲线或曲面拟合

说明

示例

fitobject = fit(x,y,fitType) 使用 fitType 指定的模型对 xy 中的数据进行拟合。

示例

fitobject = fit([x,y],z,fitType) 对向量 xyz 中的数据进行曲面拟合。

示例

fitobject = fit(x,y,fitType,fitOptions) 使用 fitOptions 对象指定的算法选项对数据进行拟合。

示例

fitobject = fit(x,y,fitType,Name=Value) 使用库模型 fitType 以及由一个或多个 Name=Value 对组参量指定的附加选项对数据进行拟合。使用 fitoptions 可显示特定库模型的可用属性名称和默认值。

示例

[fitobject,gof] = fit(x,y,fitType) 返回结构体 gof 中的拟合优度统计量。

示例

[fitobject,gof,output] = fit(x,y,fitType) 返回结构体 output 中的拟合算法信息。

示例

全部折叠

加载 census 样本数据集。

load census;

向量 popcdate 分别包含人口规模和人口统计年份的数据。

对人口数据进行二次曲线拟合。

f=fit(cdate,pop,'poly2')
f = 
     Linear model Poly2:
     f(x) = p1*x^2 + p2*x + p3
     Coefficients (with 95% confidence bounds):
       p1 =    0.006541  (0.006124, 0.006958)
       p2 =      -23.51  (-25.09, -21.93)
       p3 =   2.113e+04  (1.964e+04, 2.262e+04)

f 包含拟合结果,包括 95% 置信边界的系数估计值。

绘制 f 中拟合的图以及数据散点图。

plot(f,cdate,pop)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

绘图显示,拟合曲线与人口数据高度吻合。

加载 franke 样本数据集。

load franke

向量 xyz 包含从 Franke 的二元测试函数生成的数据,并添加了噪声和缩放。

对数据进行多项式曲面拟合。为 x 项指定 2 次,为 y 项指定 3 次。

sf = fit([x, y],z,'poly23')
     Linear model Poly23:
     sf(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p21*x^2*y 
                    + p12*x*y^2 + p03*y^3
     Coefficients (with 95% confidence bounds):
       p00 =       1.118  (0.9149, 1.321)
       p10 =  -0.0002941  (-0.000502, -8.623e-05)
       p01 =       1.533  (0.7032, 2.364)
       p20 =  -1.966e-08  (-7.084e-08, 3.152e-08)
       p11 =   0.0003427  (-0.0001009, 0.0007863)
       p02 =      -6.951  (-8.421, -5.481)
       p21 =   9.563e-08  (6.276e-09, 1.85e-07)
       p12 =  -0.0004401  (-0.0007082, -0.0001721)
       p03 =       4.999  (4.082, 5.917)

sf 包含拟合结果,包括 95% 置信边界的系数估计值。

绘制 sf 中的拟合图以及数据散点图。

plot(sf,[x,y],z)

Figure contains an axes object. The axes object contains 2 objects of type surface, line. One or more of the lines displays its values using only markers

加载 franke 数据,并将其转换为一个 MATLAB® 表。

load franke
T = table(x,y,z);

将表中的变量指定为 fit 函数的输入,并绘制拟合图。

f = fit([T.x, T.y],T.z,'linearinterp');
plot( f, [T.x, T.y], T.z )

Figure contains an axes object. The axes object contains 2 objects of type surface, line. One or more of the lines displays its values using only markers

加载并绘制数据图,使用 fittypefitoptions 函数创建拟合选项和拟合类型,然后创建并绘制拟合图。

加载 census.mat 中的数据并绘制数据图。

load census
plot(cdate,pop,'o')

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

为自定义非线性模型 y=a(x-b)n 创建一个拟合选项对象和一个拟合类型,其中 ab 是系数,n 是问题相关参数。

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

使用拟合选项和值 n = 2 来拟合数据。

[curve2,gof2] = fit(cdate,pop,ft,'problem',2)
curve2 = 
     General model:
     curve2(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 来拟合数据。

[curve3,gof3] = fit(cdate,pop,ft,'problem',3)
curve3 = 
     General model:
     curve3(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

用数据绘制拟合结果图。

hold on
plot(curve2,'m')
plot(curve3,'c')
legend('Data','n=2','n=3')
hold off

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, n=2, n=3.

加载 carbon12alpha 核反应采样数据集。

load carbon12alpha

angle 是以弧度为单位的发射角度组成的向量。counts 是对应于 angle 中角度的原始 alpha 粒子计数组成的向量。

显示计数对角度的散点图。

scatter(angle,counts)

Figure contains an axes object. The axes object contains an object of type scatter.

散点图显示,计数会随着角度在 04.5 之间增大而发生振荡。要对数据进行多项式模型拟合,请将 fitType 输入参量指定为 "poly#",其中 # 是 1 到 9 之间的整数。您可以拟合高达九次的模型。有关详细信息,请参阅曲线和曲面拟合的库模型列表

对核反应数据进行五次、七次和九次多项式拟合。返回每个拟合的拟合优度统计量。

[f5,gof5] = fit(angle,counts,"poly5");
[f7,gof7] = fit(angle,counts,"poly7");
[f9,gof9] = fit(angle,counts,"poly9");

使用 linspace 函数生成一个由 04.5 之间的查询点组成的向量。计算查询点处的多项式拟合值,然后将它们与核反应数据一起绘图。

xq = linspace(0,4.5,1000);

figure
hold on
scatter(angle,counts,"k")
plot(xq,f5(xq))
plot(xq,f7(xq))
plot(xq,f9(xq))
ylim([-100,550])
legend("original data","fifth-degree polynomial","seventh-degree polynomial","ninth-degree polynomial")

Figure contains an axes object. The axes object contains 4 objects of type scatter, line. These objects represent original data, fifth-degree polynomial, seventh-degree polynomial, ninth-degree polynomial.

该绘图表明九次多项式最准确地描述了数据的情况。

使用 struct2table 函数显示每个拟合的拟合优度统计量。

gof = struct2table([gof5 gof7 gof9],RowNames=["f5" "f7" "f9"])
gof=3×5 table
             sse        rsquare    dfe    adjrsquare     rmse 
          __________    _______    ___    __________    ______

    f5    1.0901e+05    0.54614    18      0.42007       77.82
    f7         32695    0.86387    16      0.80431      45.204
    f9        3660.2    0.98476    14      0.97496      16.169

九次多项式拟合的误差平方和 (SSE) 小于五次和七次多项式拟合的 SSE。此结果证实九次多项式最准确地描述了数据的情况。

加载 census 样本数据集。进行三次多项式拟合,并指定 Normalize(中心化并缩放)和 Robust 拟合选项。

load census;
f = fit(cdate,pop,'poly3','Normalize','on','Robust','Bisquare')
f = 
     Linear model Poly3:
     f(x) = p1*x^3 + p2*x^2 + p3*x + p4
       where x is normalized by mean 1890 and std 62.05
     Coefficients (with 95% confidence bounds):
       p1 =     -0.4619  (-1.895, 0.9707)
       p2 =       25.01  (23.79, 26.22)
       p3 =       77.03  (74.37, 79.7)
       p4 =       62.81  (61.26, 64.37)

绘制拟合图。

plot(f,cdate,pop)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

在文件中定义一个函数,并使用它来创建拟合类型和进行曲线拟合。

在 MATLAB® 文件中定义一个函数。

function y = piecewiseLine(x,a,b,c,d,k)
% PIECEWISELINE   A line made of two pieces
% that is not continuous.

y = zeros(size(x));

% This example includes a for-loop and if statement
% purely for example purposes.
for i = 1:length(x)
    if x(i) < k,
        y(i) = a + b.* x(i);
    else
        y(i) = c + d.* x(i);
    end
end
end

保存该文件。

定义一些数据,通过指定函数 piecewiseLine 创建拟合类型,使用拟合类型 ft 创建一个拟合并绘制结果。

x = [0.81;0.91;0.13;0.91;0.63;0.098;0.28;0.55;...
0.96;0.96;0.16;0.97;0.96];
y = [0.17;0.12;0.16;0.0035;0.37;0.082;0.34;0.56;...
0.15;-0.046;0.17;-0.091;-0.071];
ft = fittype( 'piecewiseLine( x, a, b, c, d, k )' )
f = fit( x, y, ft, 'StartPoint', [1, 0, 1, 0, 0.5] )
plot( f, x, y ) 

加载一些数据并拟合一个指定要排除的点的自定义方程。绘制结果。

加载数据并定义一个自定义方程和一些起点。

[x, y] = titanium;

gaussEqn = 'a*exp(-((x-b)/c)^2)+d'
gaussEqn = 
'a*exp(-((x-b)/c)^2)+d'
startPoints = [1.5 900 10 0.6]
startPoints = 1×4

    1.5000  900.0000   10.0000    0.6000

使用自定义方程和起点创建两个拟合,并使用索引向量和表达式定义两组不同的排除点。使用 Exclude 从拟合中删除离群值。

f1 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', [1 10 25])
f1 = 
     General model:
     f1(x) = a*exp(-((x-b)/c)^2)+d
     Coefficients (with 95% confidence bounds):
       a =       1.493  (1.432, 1.554)
       b =       897.4  (896.5, 898.3)
       c =        27.9  (26.55, 29.25)
       d =      0.6519  (0.6367, 0.6672)
f2 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', x < 800)
f2 = 
     General model:
     f2(x) = a*exp(-((x-b)/c)^2)+d
     Coefficients (with 95% confidence bounds):
       a =       1.494  (1.41, 1.578)
       b =       897.4  (896.2, 898.7)
       c =       28.15  (26.22, 30.09)
       d =      0.6466  (0.6169, 0.6764)

对两个拟合进行绘图。

plot(f1,x,y)
title('Fit with data points 1, 10, and 25 excluded')

Figure contains an axes object. The axes object with title Fit with data points 1, 10, and 25 excluded, xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

figure
plot(f2,x,y)
title('Fit with data points excluded such that x < 800')

Figure contains an axes object. The axes object with title Fit with data points excluded such that x < 800, xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

在将排除的点作为拟合函数的输入提供之前,可以将这些点定义为变量。以下步骤重新创建前面示例中的拟合,并允许您对排除的点以及数据和拟合进行绘图。

加载数据并定义一个自定义方程和一些起点。

[x, y] = titanium;

gaussEqn = 'a*exp(-((x-b)/c)^2)+d'
gaussEqn = 
'a*exp(-((x-b)/c)^2)+d'
startPoints = [1.5 900 10 0.6]
startPoints = 1×4

    1.5000  900.0000   10.0000    0.6000

使用索引向量和表达式定义两组要排除的点。

exclude1 = [1 10 25];
exclude2 = x < 800;

使用自定义方程、起点和两个不同的排除点创建两个拟合。

f1 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', exclude1);
f2 = fit(x',y',gaussEqn,'Start', startPoints, 'Exclude', exclude2);

对两个拟合绘图并突出显示排除的数据。

plot(f1,x,y,exclude1)
title('Fit with data points 1, 10, and 25 excluded')

Figure contains an axes object. The axes object with title Fit with data points 1, 10, and 25 excluded, xlabel x, ylabel y contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent data, excluded data, fitted curve.

figure; 
plot(f2,x,y,exclude2)
title('Fit with data points excluded such that x < 800')

Figure contains an axes object. The axes object with title Fit with data points excluded such that x < 800, xlabel x, ylabel y contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent data, excluded data, fitted curve.

要创建一个具有排除点的曲面拟合示例,可加载某曲面数据,然后通过指定排除的数据来创建并绘制拟合。

load franke
f1 = fit([x y],z,'poly23', 'Exclude', [1 10 25]);
f2 = fit([x y],z,'poly23', 'Exclude', z > 1);

figure
plot(f1, [x y], z, 'Exclude', [1 10 25]);
title('Fit with data points 1, 10, and 25 excluded')

Figure contains an axes object. The axes object with title Fit with data points 1, 10, and 25 excluded contains 3 objects of type surface, line. One or more of the lines displays its values using only markers

figure
plot(f2, [x y], z, 'Exclude', z > 1);
title('Fit with data points excluded such that z > 1')

Figure contains an axes object. The axes object with title Fit with data points excluded such that z > 1 contains 3 objects of type surface, line. One or more of the lines displays its values using only markers

使用 membranerandn 函数生成一些含噪数据。

n = 41;
M = membrane(1,20)+0.02*randn(n);
[X,Y] = meshgrid(1:n);

矩阵 M 包含添加了噪声的 L 形膜的数据。矩阵 XY 分别包含 M 中对应元素的行和列索引值。

显示数据的曲面图。

figure(1)
surf(X,Y,M)

Figure contains an axes object. The axes object contains an object of type surface.

该图显示一个带褶皱的 L 形膜。膜上的褶皱是由数据中的噪声引起的。

使用线性插值通过带褶皱的膜对两个曲面进行拟合。对于第一个曲面,指定线性外插方法。对于第二个曲面,将外插方法指定为最近邻点。

flinextrap = fit([X(:),Y(:)],M(:),"linearinterp",ExtrapolationMethod="linear");
fnearextrap = fit([X(:),Y(:)],M(:),"linearinterp",ExtrapolationMethod="nearest");

通过使用 meshgrid 函数计算从 XY 数据的凸包延伸出去的查询点处的拟合值,以调查这些外插方法的不同之处。

[Xq,Yq] = meshgrid(-10:50);

Zlin = flinextrap(Xq,Yq);
Znear = fnearextrap(Xq,Yq);

绘制计算的拟合值图。

figure(2)
surf(Xq,Yq,Zlin)
title("Linear Extrapolation")
xlabel("X")
ylabel("Y")
zlabel("M")

Figure contains an axes object. The axes object with title Linear Extrapolation, xlabel X, ylabel Y contains an object of type surface.

figure(3)
surf(Xq,Yq,Znear)
title("Nearest Neighbor Extrapolation")
xlabel("X")
ylabel("Y")
zlabel("M")

Figure contains an axes object. The axes object with title Nearest Neighbor Extrapolation, xlabel X, ylabel Y contains an object of type surface.

线性外插方法会在凸包外部生成尖峰。形成尖峰的平面区段沿着凸包边界上的点的梯度生成。最近邻点外插方法使用边界上的数据向各个方向延伸曲面。这种外插方法会生成模拟边界的波。

进行平滑样条曲线拟合,并返回拟合优度统计量和有关拟合算法的信息。

加载 enso 样本数据集。enso 样本数据集包含复活节岛和澳大利亚达尔文之间的月平均大气压力差的数据。

load enso;

monthpressure 中的数据进行平滑样条曲线拟合,并返回拟合优度统计量和 output 结构体。

[curve,gof,output] = fit(month,pressure,"smoothingspline");

绘制拟合曲线图以及用于拟合该曲线的数据。

plot(curve,month,pressure);
xlabel("Month");
ylabel("Pressure");

Figure contains an axes object. The axes object with xlabel Month, ylabel Pressure contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

绘制残差对 x 数据 (month) 的图。

plot(curve,month,pressure,"residuals")
xlabel("Month")
ylabel("Residuals")

Figure contains an axes object. The axes object with xlabel Month, ylabel Residuals contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, zero line.

使用 output 结构体中的 residuals 数据绘制残差对 y 数据 (pressure) 的图。要访问 outputresiduals 字段,请使用圆点表示法。

residuals = output.residuals;
plot( pressure,residuals,".")
xlabel("Pressure")
ylabel("Residuals")

Figure contains an axes object. The axes object with xlabel Pressure, ylabel Residuals contains a line object which displays its values using only markers.

生成具有指数趋势的数据,然后使用曲线拟合指数模型库中的第一个方程(单项指数模型)对数据进行拟合。绘制结果。

x = (0:0.2:5)';
y = 2*exp(-0.2*x) + 0.5*randn(size(x));
f = fit(x,y,'exp1');
plot(f,x,y)

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent data, fitted curve.

您可以使用匿名函数来更轻松地将其他数据传递到 fit 函数中。

在定义匿名函数之前,加载数据并将 Emax 设置为 1

data = importdata( 'OpioidHypnoticSynergy.txt' );
Propofol      = data.data(:,1);
Remifentanil  = data.data(:,2);
Algometry     = data.data(:,3);
Emax = 1;

将模型方程定义为匿名函数:

Effect = @(IC50A, IC50B, alpha, n, x, y) ...
    Emax*( x/IC50A + y/IC50B + alpha*( x/IC50A )...
    .* ( y/IC50B ) ).^n ./(( x/IC50A + y/IC50B + ...
    alpha*( x/IC50A ) .* ( y/IC50B ) ).^n  + 1);

使用匿名函数 Effect 作为 fit 函数的输入,并绘制结果:

AlgometryEffect = fit( [Propofol, Remifentanil], Algometry, Effect, ...
    'StartPoint', [2, 10, 1, 0.8], ...
    'Lower', [-Inf, -Inf, -5, -Inf], ...
    'Robust', 'LAR' )
plot( AlgometryEffect, [Propofol, Remifentanil], Algometry )

有关使用匿名函数和其他自定义模型进行拟合的更多示例,请参阅 fittype 函数。

对于属性 UpperLowerStartPoint,您需要找到系数项的顺序。

创建一个拟合类型。

ft = fittype('b*x^2+c*x+a');

使用 coeffnames 函数获取系数的名称和顺序。

coeffnames(ft)
ans = 3x1 cell
    {'a'}
    {'b'}
    {'c'}

请注意,这与使用 fittype 创建 ft 的表达式中系数的顺序不同。

加载数据,创建一个拟合并设置起点。

load enso
fit(month,pressure,ft,'StartPoint',[1,3,5])
ans = 
     General model:
     ans(x) = b*x^2+c*x+a
     Coefficients (with 95% confidence bounds):
       a =       10.94  (9.362, 12.52)
       b =   0.0001677  (-7.985e-05, 0.0004153)
       c =     -0.0224  (-0.06559, 0.02079)

这会将初始值赋给系数,如下所示:a = 1b = 3c = 5

您也可以获取拟合选项并设置起点和下界,然后使用新选项重新拟合。

options = fitoptions(ft)
options = 
  nlsqoptions with properties:

       StartPoint: []
            Lower: []
            Upper: []
        Algorithm: 'Trust-Region'
    DiffMinChange: 1.0000e-08
    DiffMaxChange: 0.1000
          Display: 'Notify'
      MaxFunEvals: 600
          MaxIter: 400
           TolFun: 1.0000e-06
             TolX: 1.0000e-06
           Robust: 'Off'
        Normalize: 'off'
          Exclude: []
          Weights: []
           Method: 'NonlinearLeastSquares'

options.StartPoint = [10 1 3];
options.Lower = [0 -Inf 0];
fit(month,pressure,ft,options)
ans = 
     General model:
     ans(x) = b*x^2+c*x+a
     Coefficients (with 95% confidence bounds):
       a =       10.23  (9.448, 11.01)
       b =   4.335e-05  (-1.82e-05, 0.0001049)
       c =   5.523e-12  (fixed at bound)

输入参数

全部折叠

要拟合的数据,指定为包含一列(曲线拟合)或两列(曲面拟合)的矩阵。您可以使用 tablename.varname 指定 MATLAB 表中的变量。不能包含 InfNaN。拟合中仅使用复数数据的实部。

示例: x

示例: [x,y]

数据类型: double

要拟合的数据,指定为列向量,其行数与 x 相同。您可以使用 tablename.varname 在 MATLAB 表中指定变量。不能包含 InfNaN。拟合中仅使用复数数据的实部。

如果您的数据不是列向量形式,请使用 prepareCurveDataprepareSurfaceData

数据类型: double

要拟合的数据,指定为列向量,其行数与 x 相同。您可以使用 tablename.varname 在 MATLAB 表中指定变量。不能包含 InfNaN。拟合中仅使用复数数据的实部。

如果您的数据不是列向量形式,请使用 prepareSurfaceData。例如,如果您有 3 个矩阵,或您的数据是网格向量形式,其中 length(X) = n, length(Y) = msize(Z) = [m,n]

数据类型: double

要拟合的模型类型,指定为表示库模型名称或 MATLAB 表达式的字符向量或字符串标量、线性模型项的字符串数组或这些项的字符向量元胞数组、匿名函数或用 fittype 函数创建的 fittype。您可以将 fittype 的任何有效的第一个输入用作 fit 的输入。

有关库模型名称的列表,请参阅模型名称和方程

要拟合自定义模型,请使用 MATLAB 表达式、线性模型项的元胞数组或匿名函数。您还可以使用 fittype 函数创建 fittype,然后将其用作 fitType 输入参量的值。有关示例,请参阅使用匿名函数拟合自定义模型。有关使用线性模型项的示例,请参阅 fittype 函数。

示例: "poly2"

使用 fitoptions 函数构造的算法选项。这是为拟合选项指定名称-值对组参量的替代方法。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: Lower=[0,0],Upper=[Inf,max(x)],StartPoint=[1 1] 指定拟合方法、边界和起点。

所有拟合方法的选项

全部折叠

用于中心化和缩放数据的选项,指定为由 'Normalize''on''off' 组成的以逗号分隔的对组。

数据类型: char

要从拟合中排除的点,指定为由 'Exclude' 和以下项之一组成的以逗号分隔的对组:

  • 描述逻辑向量的表达式,例如 x > 10

  • 对要排除的点进行索引的整数组成的向量,例如 [1 10 25]

  • 所有数据点的逻辑向量,其中 true 表示离群值,由 excludedata 创建。

有关示例,请参阅从拟合中排除点

数据类型: logical | double

拟合的权重,指定为以逗号分隔的对组,其中包含 'Weights' 和与响应数据 y(曲线)或 z(曲面)大小相同的向量。

数据类型: double

要赋给问题相关常量的值,指定为由 'problem' 和一个元胞数组组成的以逗号分隔的对组,该元胞数组中每个元素对应于一个与问题相关的常量。有关详细信息,请参阅 fittype

数据类型: cell | double

平滑选项

全部折叠

平滑参数,指定为以逗号分隔的对组,其中包含 'SmoothingParam' 和介于 0 和 1 之间的标量值。默认值取决于数据集。仅当拟合类型为 smoothingspline 时才可用。

数据类型: double

用于局部回归的数据点的比例,指定为以逗号分隔的对组,其中包含 'Span' 和介于 0 和 1 之间的标量值。仅当拟合类型为 lowessloess 时才可用。

数据类型: double

插值选项

全部折叠

插值拟合的外插方法,指定为以下值之一。

描述支持的拟合
"auto"

所有插值拟合类型的默认值。将 ExtrapolationMethod 设置为 "auto" 以便在拟合过程中自动指定外插方法。

所有插值拟合类型和 cubicspline 曲线拟合

"none"

无外插。拟合数据凸包外部的查询点的计算结果为 NaN

当您为 cubicinterplinearinterp 曲面拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "none"

曲线拟合 - cubicsplinepchipinterp

曲线拟合和曲面拟合 - cubicinterplinearinterpnearestinterp

"linear"

基于边界梯度的线性外插。

当您为 "linearinterp" 曲线拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "linear"

曲面拟合 - cubicinterpnearestinterp

曲线拟合和曲面拟合 - linearinterp

"nearest"

最近邻点外插。这种方法的计算结果为拟合数据凸包边界上最近点的值。

当您为 "nearestinterp" 曲线拟合和曲面拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "nearest"

曲线拟合 - cubicsplinepchipinterp

曲线拟合和曲面拟合 - cubicinterplinearinterpnearestinterp

"thinplate"

薄板样条外插。这种方法可将薄板内插样条扩展到拟合数据的凸包之外。有关详细信息,请参阅 tpaps

当您为 "thinplateinterp" 曲面拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "thinplate"

曲面拟合 - thinplateinterp

"biharmonic"

双调和样条外插。这种方法可将双调和内插样条扩展到拟合数据的凸包之外。

当您为 "biharmonicinterp" 曲面拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "biharmonic"

曲面拟合 - biharmonicinterp

"pchip"

分段三次埃尔米特插值多项式 (PCHIP) 外插。这种方法可将保形 PCHIP 扩展到拟合数据的凸包之外。有关详细信息,请参阅 pchip

当您为 pchipinterp 曲线拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "pchip"

曲线拟合 - pchipinterp

"cubic"

三次样条外插。这种方法可将三次插值样条扩展到拟合数据的凸包之外。

当您为 cubicinterpcubicspline 曲线拟合将 ExtrapolationMethod 设置为 "auto" 时,fit 将外插方法设置为 "cubic"。有关详细信息,请参阅 spline

曲线拟合 - cubicinterp cubicspline

数据类型: char | string

线性和非线性最小二乘选项

全部折叠

稳健线性最小二乘拟合方法,指定为以逗号分隔的对组,其中包含 'Robust' 和以下值之一:

  • 'LAR' 指定最小绝对残差方法。

  • 'Bisquare' 指定 bisquare 权重方法。

当拟合类型 MethodLinearLeastSquaresNonlinearLeastSquares 时可用。

数据类型: char

要拟合的系数的下界,指定为以逗号分隔的对组,其中包含 'Lower' 和向量。默认值为一个空向量,表示拟合不受下界的约束。如果指定边界,则向量长度必须等于系数数目。使用 coeffnames 函数找出向量值中系数项的顺序。有关示例,请参阅获取系数的顺序以便设置起点和边界。各个无约束下界可以由 -Inf 指定。

MethodLinearLeastSquaresNonlinearLeastSquares 时可用。

数据类型: double

要拟合的系数的上界,指定为以逗号分隔的对组,其中包含 'Upper' 和向量。默认值为一个空向量,表示拟合不受上界的约束。如果指定边界,则向量长度必须等于系数数目。使用 coeffnames 函数找出向量值中系数项的顺序。有关示例,请参阅获取系数的顺序以便设置起点和边界。各个无约束上界可以由 +Inf 指定。

MethodLinearLeastSquaresNonlinearLeastSquares 时可用。

数据类型: logical

非线性最小二乘选项

全部折叠

系数的初始值,指定为以逗号分隔的对组,其中包含 'StartPoint' 和向量。使用 coeffnames 函数找出向量值中系数项的顺序。有关示例,请参阅获取系数的顺序以便设置起点和边界

如果起点(空向量的默认值)未传递给 fit 函数,则某些库模型的起点是通过启发式方法确定的。对于有理和威布尔模型,以及所有自定义的非线性模型,工具箱从区间 (0,1) 中均匀随机地选择系数的默认初始值。因此,使用相同数据和模型的多个拟合可能导致不同的拟合系数。要避免这种情况,请使用 fitoptions 对象为系数指定初始值,或为 StartPoint 值指定向量值。

MethodNonlinearLeastSquares 时可用。

数据类型: double

用于拟合过程的算法,指定为以逗号分隔的对组,其中包含 'Algorithm''Levenberg-Marquardt''Trust-Region'

MethodNonlinearLeastSquares 时可用。

数据类型: char

有限差分梯度系数的最大变化,指定为以逗号分隔的对组,其中包含 'DiffMaxChange' 和标量。

MethodNonlinearLeastSquares 时可用。

数据类型: double

有限差分梯度系数的最小变化,指定为以逗号分隔的对组,其中包含 'DiffMinChange' 和标量。

MethodNonlinearLeastSquares 时可用。

数据类型: double

命令行窗口中的显示选项,指定为以逗号分隔的对组,其中包含 'Display' 和以下选项之一:

  • 仅当拟合未收敛时,'notify' 才显示输出。

  • 'final' 仅显示最终输出。

  • 'iter' 在每次迭代时显示输出。

  • 'off' 不显示输出。

MethodNonlinearLeastSquares 时可用。

数据类型: char

允许的模型计算的最大次数,指定为以逗号分隔的对组,其中包含 'MaxFunEvals' 和标量。

MethodNonlinearLeastSquares 时可用。

数据类型: double

拟合允许的最大迭代次数,指定为以逗号分隔的对组,其中包含 'MaxIter' 和标量。

MethodNonlinearLeastSquares 时可用。

数据类型: double

模型值的终止容差,指定为以逗号分隔的对组,其中包含 'TolFun' 和标量。

MethodNonlinearLeastSquares 时可用。

数据类型: double

系数值的终止容差,指定为以逗号分隔的对组,其中包含 'TolX' 和标量。

MethodNonlinearLeastSquares 时可用。

数据类型: double

输出参量

全部折叠

拟合结果,以 cfit(对于曲线拟合)或 sfit(对于曲面拟合)对象形式返回。有关绘制、评估、计算置信区间、积分、微分或修改拟合对象的函数,请参阅拟合后处理

拟合优度统计量,以包括下表中字段的 gof 结构体形式返回。

字段

sse

误差平方和

rsquare

R 方(决定系数)

dfe

误差自由度

adjrsquare

自由度调整后的决定系数

rmse

均方根误差(标准误差)

示例: gof.rmse

拟合算法信息,以包含与拟合算法相关联的信息的 output 结构体形式返回。

字段取决于具体算法。例如,非线性最小二乘算法的 output 结构体包括下表中显示的字段。

字段

numobs

观测值(响应值)的数目

numparam

要拟合的未知参数(系数)的数目

residuals

原始残差向量(观测值减去拟合值)

Jacobian

雅可比矩阵

exitflag

描述算法的退出条件。正值标志表示在容差范围内收敛。零标志表示超出了函数计算或迭代的最大次数。负标志表示算法未收敛于解。

iterations

迭代次数

funcCount

函数计算次数

firstorderopt

一阶最优性的度量(梯度分量的绝对最大值)

algorithm

使用的拟合算法

示例: output.Jacobian

版本历史记录

在 R2006a 之前推出

全部展开