Main Content

自定义模型

自定义模型vs. Library Models

如果工具箱库不包含所需的参数方程,则可以创建自己的自定义方程。但是,图书馆模型为快速收敛提供了最佳机会。这是因为:

  • 对于大多数库模型,工具箱计算最佳默认系数起点。对于自定义模型,工具箱选择间隔[0,1]上的随机默认起点。您需要为自定义模型找到合适的起点。

  • 图书馆模型使用分析性雅各布。定制模型使用有限的差异。

线性和非线性拟合

You can create custom general equations with the Custom Equation fit type. General models are nonlinear combinations of (perhaps nonlinear) terms. They are defined by equations that might be nonlinear in the parameters. The custom equation fit uses the nonlinear least-squares fitting procedure.

You can define a custom linear equation using the Custom Equation fit type, though the nonlinear fitting is less efficient and usually slower than linear least-squares fitting.

  • 如果您不知道方程是否可以表示为一组线性函数,请选择自定义方程。You might need to search for suitable start points.

  • 如果您需要线性最小二乘适合自定义方程,请选择Linear Fittingmodel type instead. See自定义线性拟合

交互选择自定义方程式拟合

In the Curve Fitting app, select自定义方程from the model type list.

使用自定义方程式拟合来定义您自己的方程式。选择时出现一个自定义方程式自定义方程从列表中,如下所示,曲线数据。

如果you have surface data, the example custom equation uses bothXandy

  1. 您可以编辑X,,,,y,,,,andzto any valid variable names.

  2. 在下部框中,编辑示例以定义您自己的自定义方程。您可以输入任何有效的MATLAB®根据您的变量名称表达。您可以指定函数或脚本名称(请参阅Fitting a Curve Defined by a File in the Curve Fitting App)。

  3. 点击Fit Options如果you want to specify start points or bounds. By default, the starting values are randomly selected on the interval [0,1] and are unconstrained. You might need to search for suitable start points and bounds. For an example, see自定义非线性ENSO数据分析

    如果you set fit options and then alter other fit settings, the app remembers your choices for lower and upper bounds and start points, if possible. For custom equations Curve Fitting app always remembers user values, but for many library models if you change fit settings then the app automatically calculates new best values for start points or lower bounds.

您可以作为保存曲线拟合应用程序会话的一部分保存自定义方程。

Your function can execute a number of times, both during fitting and during preprocessing before fitting. Be aware this may be time-consuming if you are using functions with side effects such as writing data to a file, or displaying diagnostic information to the Command Window.

For examples, see:

Fitting a Curve Defined by a File in the Curve Fitting App

此示例显示了如何在曲线拟合应用中提供函数或脚本名称作为拟合模型。定义文件中的功能并使用它来拟合曲线。

  1. Define a function in a MATLAB file.

    functiony =分段(x,a,b,c,d,k)%零工一条由两件制成的行%这不是连续的。y =零(size(x));%此示例包括一个循环和if语句%纯粹的目的。为了i = 1:length(x)如果X(i) < k, y(i) = a + b.* x(i);别的y(i) = c + d.* x(i);结尾结尾结尾

    Save the file on the MATLAB path.

  2. 定义一些数据并打开曲线拟合应用程序。

    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.56;...0.15; -0.046; 0.17; -0.091; -0.071];CFTool
  3. In the Curve Fitting app, selectXandyin thex数据andy数据lists.

  4. 用你的piecewiseLine通过选择“自定义方程式拟合类型”,然后在“自定义方程式文本”框中输入函数表达式中的功能。该函数采用X数据和一些参数进行拟合。

    PICEEWISELINE(X,A,B,C,D,K)

    The Curve Fitting app creates a fit using your function.

小费

如果you want to use the same function for fitting at the command line, use the same expression as an input to合身type,然后使用合身type作为输入合身

ft = fittype('pieceWiseline(x,a,b,c,d,k)');f = fit(x,y,ft)
有关更多示例,请参阅合身功能。

选择ing a Custom Equation Fit at the Command Line

To fit custom models, either:

  • Supply a custom model to the合身功能在合身Type输入参数。您可以使用MATLAB表达式(包括任何。m文件),线性模型项的单元格数组或匿名函数。

  • 创建一个合身typeobject with the合身typefunction to use as an input argument for the合身功能。

此示例加载了一些数据并使用定义Weibull模型作为输入的自定义方程式合身功能:

时间= (0.1;0.1;0.3;0.3;1.3;1.7;2.1;2.6;3.。9; 3.9; ... 5.1; 5.6; 6.2; 6.4; 7.7; 8.1; 8.2; 8.9; 9.0; 9.5; ... 9.6; 10.2; 10.3; 10.8; 11.2; 11.2; 11.2; 11.7; 12.1; 12.3; ... 12.3; 13.1; 13.2; 13.4; 13.7; 14.0; 14.3; 15.4; 16.1; 16.1; ... 16.4; 16.4; 16.7; 16.7; 17.5; 17.6; 18.1; 18.5; 19.3; 19.7;]; conc = [0.01; 0.08; 0.13; 0.16; 0.55; 0.90; 1.11; 1.62; 1.79; 1.59; ... 1.83; 1.68; 2.09; 2.17; 2.66; 2.08; 2.26; 1.65; 1.70; 2.39; ... 2.08; 2.02; 1.65; 1.96; 1.91; 1.30; 1.62; 1.57; 1.32; 1.56; ... 1.36; 1.05; 1.29; 1.32; 1.20; 1.10; 0.88; 0.63; 0.69; 0.69; ... 0.49; 0.53; 0.42; 0.48; 0.41; 0.27; 0.36; 0.33; 0.17; 0.20;]; f = fit( time, conc, 'c*a*b*x^(b-1)*exp(-a*x^b)', 'StartPoint', [0.01, 2, 5] ) plot( f, time, conc )

使用自定义模型使用合身type,使用表格:

f = fittype(expr
which constructs a custom model合身typeobject for the MATLAB expression contained in the string, cell array, or anonymous functionexpr

See the合身typereference page for details on:

  • Specifying dependent and independent variables, problem parameters, and coefficients using合身type

  • 为您的自定义方程式使用线性拟合算法指定单元格数组。如果expr是字符串或匿名函数,然后工具箱使用非线性拟合算法。

    有关线性拟合的更多详细信息,请参阅选择ing Linear Fitting at the Command Line

  • Examples of linear and nonlinear custom models.

有关逐步的示例,请参见自定义非线性人口普查配件