# fit

## 语法

``fitobject = fit(x,y,fitType)``
``fitobject = fit([x,y],z,fitType)``
``fitobject = fit(x,y,fitType,fitOptions)``
``fitobject = fit(x,y,fitType,Name=Value)``
``[fitobject,gof] = fit(x,y,fitType)``
``[fitobject,gof,output] = fit(x,y,fitType)``

## 说明

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

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

``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` 中的拟合算法信息。 `

## 示例

```load census; 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) ```
`plot(f,cdate,pop)`

```load franke 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) ```
`plot(sf,[x,y],z)`

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

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

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

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

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

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

`load carbon12alpha`

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

`scatter(angle,counts)`

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

```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")```

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

```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)`

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

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

`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 plot(f2,x,y) title('Fit with data points excluded such that x < 800')```

```[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; plot(f2,x,y,exclude2) title('Fit with data points excluded such that x < 800')```

```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 plot(f2, [x y], z, 'Exclude', z > 1); title('Fit with data points excluded such that z > 1')```

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

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

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

```[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(3) surf(Xq,Yq,Znear) title("Nearest Neighbor Extrapolation") xlabel("X") ylabel("Y") zlabel("M")```

```load enso; [curve, goodness, output] = fit(month,pressure,'smoothingspline'); plot(curve,month,pressure); xlabel('Month'); ylabel('Pressure');```

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

```plot( pressure, output.residuals, '.' ) xlabel( 'Pressure' ) ylabel( 'Residuals' )```

```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)```

```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);```

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

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

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

```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) ```

`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) ```

## 输入参数

### 名称-值参数

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

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

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

`"auto"`

`"none"`

`linearinterp``nearestinterp``cubicinterp` 曲面拟合

`"linear"`

`linearinterp` 拟合、`nearestinterp` 曲面拟合和 `cubicinterp` 曲面拟合

`"nearest"`

`linearinterp` 曲面拟合、`nearestinterp` 拟合和 `cubicinterp` 曲面拟合

`"thinplate"`

`thinplateinterp` 拟合

`"biharmonic"`

`biharmonicinterp` 拟合

`"pchip"`

`pchipinterp` 拟合

`"cubic"`

`cubicspline` 拟合和 `cubicinterp` 曲线拟合

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

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

`Method``LinearLeastSquares``NonlinearLeastSquares` 时可用。

`Method``LinearLeastSquares``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

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

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

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

• `'off'` 不显示输出。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

`Method``NonlinearLeastSquares` 时可用。

## 输出参数

`sse`

`rsquare`

R 方（决定系数）

`dfe`

`adjrsquare`

`rmse`

`numobs`

`numparam`

`residuals`

`Jacobian`

`exitflag`

`iterations`

`funcCount`

`firstorderopt`

`algorithm`