fitoptions - Create or modify fit options structure

Syntax

options = fitoptions
options = fitoptions(model)
options = fitoptions(model,fld1,val1,fld2,val2,...)
options = fitoptions('Method',method)
options = fitoptions('Method',method,fld1,val1,fld2,val2,...)
newoptions = fitoptions(options,fld1,val1,fld2,val2,...)
newoptions = fitoptions(options1,options2)

Description

options = fitoptions creates the default fit options structure options. Fields in the options structure, listed in the table below with their default values, are supported by all fitting methods.

Field Name

Values

Normalize

Specifies whether the data is centered and scaled. Values are 'off' or 'on'. The default is 'off'.

Exclude

A logical vector indicating data points to exclude from the fit. The excludedata function can be used to create this vector. The default is empty.

Weights

A vector of weights the same size as the response data. The default is empty.

Method

The fitting method. A complete list of supported fitting methods is given below. The default is 'None'.

options = fitoptions(model) creates the default fit options structure for the library or custom model specified by the string model. You can display library model names with the cflibhelp function.

options = fitoptions(model,fld1,val1,fld2,val2,...) creates a fit options structure for the specified model with the fields specified by the strings fld1, fld2, ... set to the values val1, val2, ..., respectively.

options = fitoptions('Method',method) creates the default fit options structure for the fitting method specified by the string method. Supported fitting methods are listed in the table below.

method

Description

'NearestInterpolant'

Nearest neighbor interpolation

'LinearInterpolant'

Linear interpolation

'PchipInterpolant'

Piecewise cubic Hermite interpolation

'CubicSplineInterpolant'

Cubic spline interpolation

'SmoothingSpline'

Smoothing spline

'LinearLeastSquares'

Linear least squares

'NonlinearLeastSquares'

Nonlinear least squares

options = fitoptions('Method',method,fld1,val1,fld2,val2,...) creates the default fit options structure for the fitting method specified by the string method with the fields specified by the strings fld1, fld2, ... set to the values val1, val2, ..., respectively.

newoptions = fitoptions(options,fld1,val1,fld2,val2,...) modifies the existing fit options structure options by setting the fields specified by the strings fld1, fld2, ... set to the values val1, val2, ..., respectively. The new options structure is returned in newoptions.

newoptions = fitoptions(options1,options2) combines the input fit options structures options1 and options2 to create the output fit options structure newoptions. If the input structures have Method fields set to the same value, the nonempty values for the fields in options2 override the corresponding values in options1 in the output structure. If the input structures have Method fields set to different values, the output structure will have the same Method as options1, and only the values of the Normalize, Exclude, and Weights fields of options2 will override the corresponding values in options1.

Remarks

Field values in a fit options structure can be referenced with the get method and assigned with the set method. For example:

options = fitoptions('fourier1');
get(options,'Method')
ans =
NonlinearLeastSquares
get(options,'MaxIter')
ans =
   400
set(options,'Maxiter',1e3);
get(options,'MaxIter')
ans =
        1000

Field values can also be referenced and assigned using the dot notation. For example:

options.MaxIter
ans =
        1000
options.MaxIter = 500;
options.MaxIter
ans =
   500

Additional Fit Options

Additional fields in the fit options structure, beyond the default fields Normalize, Exclude, Weights, and Method, are available according to the fitting method.

If the Method field has the value 'NearestInterpolant', 'LinearInterpolant', 'PchipInterpolant', or 'CubicSplineInterpolant', there are no additional fields in the fit options structure.

If the Method field has the value SmoothingSpline, the SmoothingParam field is available to configure the smoothing parameter. Its value must be between 0 and 1. The default value depends on the data set.

If the Method field has the value LinearLeastSquares, the additional fields available in the fit options structure are listed in the table below.

Field

Description

Robust

Specifies the robust linear least-squares fitting method to be used. Values are 'on', 'off', 'LAR', or 'Bisquare'. The default is 'off'. 'LAR' specifies the least absolute residual method and 'Bisquare' specifies the bisquare weights method. 'on' is equivalent to 'Bisquare', the default method.

Lower

A vector of lower bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by lower bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained lower bounds can be specified by -Inf.

Upper

A vector of upper bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by upper bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained upper bounds can be specified by Inf.

If the Method field has the value NonlinearLeastSquares, the additional fields available in the fit options structure are listed in the table below.

Property

Description

Robust

Specifies the robust linear least-squares fitting method to be used. Values are 'on', 'off', 'LAR', or 'Bisquare'. The default is 'off'. 'LAR' specifies the least absolute residual method and 'Bisquare' specifies the bisquare weights method. 'on' is equivalent to 'Bisquare', the default method.

Lower

A vector of lower bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by lower bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained lower bounds can be specified by -Inf.

Upper

A vector of upper bounds on the coefficients to be fitted. The default value is an empty vector, indicating that the fit is unconstrained by upper bounds. If bounds are specified, the vector length must equal the number of coefficients. Individual unconstrained upper bounds can be specified by Inf.

StartPoint

A vector of initial values for the coefficients. The default value of StartPoint is an empty vector. If the default value is passed to the fit function, starting points for some library models are determined heuristically. For other models, the values are selected uniformly at random on the interval (0,1).

Algorithm

The algorithm used for the fitting procedure. Values are 'Levenberg-Marquardt', 'Gauss-Newton', or 'Trust-Region'. The default is 'Trust-Region'.

DiffMaxChange

The maximum change in coefficients for finite difference gradients. The default is 0.1.

DiffMinChange

The minimum change in coefficients for finite difference gradients. The default is 10–8.

Display

Controls the display in the command window. 'notify', the default, displays output only if the fit does not converge. 'final' displays only the final output. 'iter' displays output at each iteration. 'off' displays no output.

MaxFunEvals

The maximum number of evaluations of the model allowed. The default is 600.

MaxIter

The maximum number of iterations allowed for the fit. The default is 400.

TolFun

The termination tolerance on the model value. The default is 10–6.

TolX

The termination tolerance on the coefficient values. The default is 10–6.

Example

Create the default fit options structure and set the option to center and scale the data before fitting:

options = fitoptions;
options.Normal = 'on';
options
options =
    Normalize: 'on'
    Exclude: [1x0 double]
    Weights: [1x0 double]
    Method: 'None'

Modifying the default fit options structure is useful when you want to set the Normalize, Exclude, or Weights fields, and then fit your data using the same options with different fitting methods. For example:

load census
f1 = fit(cdate,pop,'poly3',options);
f2 = fit(cdate,pop,'exp1',options);
f3 = fit(cdate,pop,'cubicsp',options);

Data-dependent fit options are returned in the third output argument of the fit function. For example:

[f,gof,out] = fit(cdate,pop,'smooth');
smoothparam = out.p
smoothparam =
    0.0089

The default smoothing parameter can be modified for a new fit:

options = fitoptions('Method','Smooth',...
                     'SmoothingParam',0.0098);
[f,gof,out] = fit(cdate,pop,'smooth',options);

Example

Create a noisy sum of two Gaussian peaks—one with a small width, and one with a large width:

a1 = 1; b1 = -1; c1 = 0.05;
a2 = 1; b2 = 1; c2 = 50;
x = (-10:0.02:10)';
gdata = a1*exp(-((x-b1)/c1).^2) + ...
        a2*exp(-((x-b2)/c2).^2) + ...
        0.1*(rand(size(x))-.5);
plot(x,gdata)

Fit the data using the two-term Gaussian library model:

f = fittype('gauss2');
gfit = fit(x,gdata,f)
gfit =
     General model Gauss2:
       gfit(x) =  a1*exp(-((x-b1)/c1)^2) + 
                  a2*exp(-((x-b2)/c2)^2)
     Coefficients (with 95% confidence bounds):
       a1 =    -0.05388  (-0.136, 0.02826)
       b1 =      -2.651  (-2.718, -2.584)
       c1 =     0.05373  (-0.04106, 0.1485)
       a2 =       1.012  (1.006, 1.018)
       b2 =      0.6703  (0.06681, 1.274)
       c2 =        41.2  (36.54, 45.85)

The algorithm is having difficulty, as indicated by the wide confidence intervals for some of the coefficients. To help the algorithm, we could specify lower bounds for the nonnegative amplitudes a1, a2 and widths c1, c2:

options = fitoptions('gauss2');
options.Lower = [0 -Inf 0 0 -Inf 0]; 

Recompute the fit with the bound constraints on the coefficients:

gfit = fit(x,gdata,ftype,options)
gfit =
     General model Gauss2:
       gfit(x) =  a1*exp(-((x-b1)/c1)^2) + 
                  a2*exp(-((x-b2)/c2)^2)
     Coefficients (with 95% confidence bounds):
       a1 =       1.003  (0.9641, 1.042)
       b1 =          -1  (-1.002, -0.9987)
       c1 =     0.04972  (0.04748, 0.05197)
       a2 =       1.002  (0.999, 1.004)
       b2 =       1.136  (0.725, 1.547)
       c2 =       48.89  (45.32, 52.47)

This is a much better fit. The fit can be further improved by assigning reasonable values to other fields in the fit options structure.

See Also

cflibhelp, fit, get, set

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS