fitoptions
Create or modify fit options object
Syntax
Description
creates
the default fit options object fitOptions
= fitoptionsfitOptions
.
creates the default fit options object for the library model.fitOptions
= fitoptions(libraryModelName
)
creates fit options for the specified library model with additional options
specified by one or more fitOptions
= fitoptions(libraryModelName
,Name,Value
)Name,Value
pair arguments.
gets the fit options object for the specified fitOptions
= fitoptions(fitType
)fitType
.
Use this syntax to work with fit options for custom models.
creates fit options with additional options specified by one or more
fitOptions
= fitoptions(Name,Value
)Name,Value
pair arguments.
modifies the existing fit options object newOptions
= fitoptions(fitOptions
,Name,Value
)fitOptions
and
returns updated fit options in newOptions
with new
options specified by one or more Name,Value
pair
arguments.
combines the existing fit options objects newOptions
= fitoptions(options1
,options2
)options1
and
options2
in newOptions
.
If
Method
agrees, the nonempty values for the properties inoptions2
override the corresponding values inoptions1
innewOptions
.If
Method
differs,newOptions
contains theoptions1
value forMethod
and values fromoptions2
forNormalize
,Exclude
, andWeights
.
Examples
Modify Default Fit Options to Normalize Data
Create the default fit options object and set the option to center and scale the data before fitting.
options = fitoptions;
options.Normal = 'on'
options = basefitoptions with properties: Normalize: 'on' Exclude: [] Weights: [] Method: 'None'
Create Default Fit Options for Gaussian Fit
options = fitoptions('gauss2')
options = nlsqoptions with properties: StartPoint: [] Lower: [Inf Inf 0 Inf Inf 0] Upper: [] Algorithm: 'TrustRegion' DiffMinChange: 1.0000e08 DiffMaxChange: 0.1000 Display: 'Notify' MaxFunEvals: 600 MaxIter: 400 TolFun: 1.0000e06 TolX: 1.0000e06 Robust: 'Off' Normalize: 'off' Exclude: [] Weights: [] Method: 'NonlinearLeastSquares'
Set Polynomial Fit Options
Create fit options for a cubic polynomial and set center and scale and robust fitting options.
options = fitoptions('poly3', 'Normalize', 'on', 'Robust', 'Bisquare')
options = llsqoptions with properties: Lower: [] Upper: [] Robust: 'Bisquare' Normalize: 'on' Exclude: [] Weights: [] Method: 'LinearLeastSquares'
Create Fit Options for Linear Least Squares
options = fitoptions('Method', 'LinearLeastSquares')
options = llsqoptions with properties: Lower: [] Upper: [] Robust: 'Off' Normalize: 'off' Exclude: [] Weights: [] Method: 'LinearLeastSquares'
Specify Extrapolation Method for Interpolant Fits
Create a fitoptions
object for a linear interpolant fit with nearest neighbor extrapolation.
linearoptions = fitoptions("linearinterp",ExtrapolationMethod="nearest")
linearoptions = linearinterpoptions with properties: ExtrapolationMethod: 'nearest' Normalize: 'off' Exclude: [] Weights: [] Method: 'LinearInterpolant'
Create a second fitoptions
object for a cubic interpolant fit with nearest neighbor extrapolation.
cubicoptions = fitoptions("cubicinterp",ExtrapolationMethod="nearest")
cubicoptions = cubicsplineinterpoptions with properties: ExtrapolationMethod: 'nearest' Normalize: 'off' Exclude: [] Weights: [] Method: 'CubicSplineInterpolant'
You can use the fit options in linearoptions
to create a linearinterp
fit object using the fit
function. Use cubicoptions
to create a cubicinterp
fit.
Use Identical Fit Options in Multiple Fits
Modifying the default fit options object is useful when you want to set the Normalize
, Exclude
, or Weights
properties, and then fit your data using the same options with different fitting methods. For example, the following uses the same fit options to fit different library model types.
load census options = fitoptions; options.Normalize = 'on'; f1 = fit(cdate,pop,'poly3',options); f2 = fit(cdate,pop,'exp1',options); f3 = fit(cdate,pop,'cubicspline',options)
f3 = Cubic interpolating spline: f3(x) = piecewise polynomial computed from p with cubic extrapolation where x is normalized by mean 1890 and std 62.05 Coefficients: p = coefficient structure
Find and Change the Smoothing Fit Option
Find the smoothing parameter. Datadependent fit options such as the smooth
parameter are returned in the third output argument of the fit
function.
load census [f,gof,out] = fit(cdate,pop,'SmoothingSpline'); smoothparam = out.p
smoothparam = 0.0089
Modify the default smoothing parameter for a new fit.
options = fitoptions('Method','SmoothingSpline',... 'SmoothingParam',0.0098); [f,gof,out] = fit(cdate,pop,'SmoothingSpline',options);
Apply Coefficient Bounds to Improve Gaussian Fit
Create a Gaussian fit, inspect the confidence intervals, and specify lower bound fit options to help the algorithm.
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(((xb1)/c1).^2) + ... a2*exp(((xb2)/c2).^2) + ... 0.1*(rand(size(x)).5); plot(x,gdata)
Fit the data using the twoterm Gaussian library model.
gfit = fit(x,gdata,'gauss2')
gfit = General model Gauss2: gfit(x) = a1*exp(((xb1)/c1)^2) + a2*exp(((xb2)/c2)^2) Coefficients (with 95% confidence bounds): a1 = 0.145 (1.485, 1.195) b1 = 9.725 (14.7, 34.15) c1 = 7.117 (15.84, 30.07) a2 = 14.07 (1.959e+04, 1.962e+04) b2 = 607.2 (3.195e+05, 3.207e+05) c2 = 376 (9.74e+04, 9.815e+04)
plot(gfit,x,gdata)
The algorithm is having difficulty, as indicated by the wide confidence intervals for several coefficients.
To help the algorithm, specify lower bounds for the nonnegative amplitudes a1
and a2
and widths c1
, c2
.
options = fitoptions('gauss2', 'Lower', [0 Inf 0 0 Inf 0]);
Alternatively, you can set properties of the fit options using the form options.Property = NewPropertyValue
.
options = fitoptions('gauss2');
options.Lower = [0 Inf 0 0 Inf 0];
Recompute the fit using the bound constraints on the coefficients.
gfit = fit(x,gdata,'gauss2',options)
gfit = General model Gauss2: gfit(x) = a1*exp(((xb1)/c1)^2) + a2*exp(((xb2)/c2)^2) Coefficients (with 95% confidence bounds): a1 = 1.005 (0.966, 1.044) b1 = 1 (1.002, 0.9988) c1 = 0.0491 (0.0469, 0.0513) a2 = 0.9985 (0.9958, 1.001) b2 = 0.8059 (0.3879, 1.224) c2 = 50.6 (46.68, 54.52)
plot(gfit,x,gdata)
This is a much better fit. You can further improve the fit by assigning reasonable values to other properties in the fit options object.
Copy and Combine Fit Options
Create fit options and set lower bounds.
options = fitoptions('gauss2', 'Lower', [0 Inf 0 0 Inf 0])
options = nlsqoptions with properties: StartPoint: [] Lower: [0 Inf 0 0 Inf 0] Upper: [] Algorithm: 'TrustRegion' DiffMinChange: 1.0000e08 DiffMaxChange: 0.1000 Display: 'Notify' MaxFunEvals: 600 MaxIter: 400 TolFun: 1.0000e06 TolX: 1.0000e06 Robust: 'Off' Normalize: 'off' Exclude: [] Weights: [] Method: 'NonlinearLeastSquares'
Make a new copy of the fit options and modify the robust parameter.
newoptions = fitoptions(options, 'Robust','Bisquare')
newoptions = nlsqoptions with properties: StartPoint: [] Lower: [0 Inf 0 0 Inf 0] Upper: [] Algorithm: 'TrustRegion' DiffMinChange: 1.0000e08 DiffMaxChange: 0.1000 Display: 'Notify' MaxFunEvals: 600 MaxIter: 400 TolFun: 1.0000e06 TolX: 1.0000e06 Robust: 'Bisquare' Normalize: 'off' Exclude: [] Weights: [] Method: 'NonlinearLeastSquares'
Combine fit options.
options2 = fitoptions(options, newoptions)
options2 = nlsqoptions with properties: StartPoint: [] Lower: [0 Inf 0 0 Inf 0] Upper: [] Algorithm: 'TrustRegion' DiffMinChange: 1.0000e08 DiffMaxChange: 0.1000 Display: 'Notify' MaxFunEvals: 600 MaxIter: 400 TolFun: 1.0000e06 TolX: 1.0000e06 Robust: 'Bisquare' Normalize: 'off' Exclude: [] Weights: [] Method: 'NonlinearLeastSquares'
Change Custom Model Fit Options
Create a linear model fit type.
lft = fittype({'x','sin(x)','1'})
lft = Linear model: lft(a,b,c,x) = a*x + b*sin(x) + c
Get the fit options for the fit type lft
.
fo = fitoptions(lft)
fo = llsqoptions with properties: Lower: [] Upper: [] Robust: 'Off' Normalize: 'off' Exclude: [] Weights: [] Method: 'LinearLeastSquares'
Set the normalize fit option.
fo.Normalize = 'on'
fo = llsqoptions with properties: Lower: [] Upper: [] Robust: 'Off' Normalize: 'on' Exclude: [] Weights: [] Method: 'LinearLeastSquares'
Input Arguments
libraryModelName
— Library model to fit
character vector  string scalar
Library model to fit, specified as a character vector or string scalar. This table shows some common examples.
Library Model Name  Description 

 Linear polynomial curve 
 Linear polynomial surface 
 Quadratic polynomial curve 
 Piecewise linear interpolation 
 Piecewise cubic interpolation 
 Smoothing spline (curve) 
 Local linear regression (surface) 
For a list of library model names, see Model Names and Equations.
Example: 'poly2'
Data Types: char
 string
fitType
— Model type to fit
fittype
Model type to fit, specified as a fittype
constructed with the fittype
function. Use
this to work with fit options for custom models.
fitOptions
— Algorithm options
fitoptions
Algorithm options, specified as a fitoptions
object
created using the fitoptions
function.
options1
— Algorithm options to combine
fitoptions
Algorithm options to combine, constructed using the fitoptions
function.
options2
— Algorithm options to combine
fitoptions
Algorithm options to combine, constructed using the fitoptions
function.
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: 'Method','NonlinearLeastSquares','Lower',[0,0],'Upper',[Inf,max(x)],'Startpoint',[1
1]
specifies fitting method, bounds, and start
points.
Normalize
— Option to center and scale data
'off'
(default)  'on'
Option to center and scale the data, specified as the
commaseparated pair consisting of 'Normalize'
and 'on'
or 'off'
.
Data Types: char
Exclude
— Points to exclude from fit
expression  index vector  logical vector  empty
Points to exclude from the fit, specified as the commaseparated
pair consisting of 'Exclude'
and one of:
An expression describing a logical vector, e.g.,
x > 10
.A vector of integers indexing the points you want to exclude, e.g.,
[1 10 25]
.A logical vector for all data points where
true
represents an outlier, created byexcludedata
.
For examples, see fit
.
Weights
— Weights for fit
[ ] (default)  vector
Weights for the fit, specified as the commaseparated pair
consisting of 'Weights'
and a vector the same
size as number of data points.
Data Types: double
Method
— Fitting method
'None'
(default)  'NearestInterpolant
 'LinearInterpolant'
 'PchipInterpolant'
 CubicSplineInterpolant'
 ...
Fitting method, specified as the commaseparated pair consisting
of 'Method'
and one of the fitting methods in
this table.
Fitting Method  Description 

 Nearest neighbor interpolation 
 Linear interpolation 
 Piecewise cubic Hermite interpolation (curves only) 
 Cubic spline interpolation 
 Biharmonic surface interpolation 
 Smoothing spline 
 Lowess smoothing (surfaces only) 
 Linear least squares 
 Nonlinear least squares 
Data Types: char
 string
ExtrapolationMethod
— Extrapolation method
"auto"
(default)  "none"
 "linear"
 "nearest"
 "thinplate"
 "biharmonic"
 "pchip"
 "cubic"
Extrapolation method for an interpolant fit, specified as one of the following values:
Value  Description  Supported Fits 

"auto"  Default value for all interpolant fit
types. Set
 All interpolant fit types and

"none"  No extrapolation. When you use


"linear"  Linear extrapolation based on boundary gradients 

"nearest"  Nearest neighbor extrapolation. This extrapolation method evaluates to the value of the nearest neighbor on the boundary of the fitting data's convex hull. 

"thinplate"  Thinplate spline extrapolation. This
extrapolation method extends the thinplate
interpolating spline outside of the fitting data's
convex hull. For more information, see 

"biharmonic"  Biharmonic spline extrapolation. This extrapolation method extends the biharmonic interpolating spline outside of the fitting data's convex hull. 

"pchip"  Piecewise cubic hermite interpolating
polynomial (PCHIP) extrapolation. This
extrapolation method extends a shapepreserving
PCHIP outside of the fitting data's convex hull.
For more information, see 

"cubic"  Cubic spline extrapolation. This extrapolation method extends a cubic interpolating spline outside of the fitting data's convex hull. 

Data Types: char
 string
SmoothingParam
— Smoothing parameter
scalar value in the range (0,1)
Smoothing parameter, specified as the commaseparated pair
consisting of 'SmoothingParam'
and a scalar value
between 0 and 1. The default value depends on the data set. Only
available if the Method
is
SmoothingSpline
.
Data Types: double
Span
— Proportion of data points to use in local regressions
0.25 (default)  scalar value in the range (0,1)
Proportion of data points to use in local regressions, specified
as the commaseparated pair consisting of 'Span'
and a scalar value between 0 and 1. Only available if the
Method
is
LowessFit
.
Data Types: double
Robust
— Robust linear leastsquares fitting method
'off'
(default)  'LAR'
 'Bisquare'
Robust linear leastsquares fitting method, specified as the
commaseparated pair consisting of 'Robust'
and
one of these values:
'LAR'
specifies the least absolute residual method.'Bisquare'
specifies the bisquare weights method.
Available when the Method
is
LinearLeastSquares
or
NonlinearLeastSquares
.
Data Types: char
Lower
— Lower bounds on coefficients to be fitted
[ ] (default)  vector
Lower bounds on the coefficients to be fitted, specified as the
commaseparated pair consisting of 'Lower'
and a
vector. 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. Find the order
of the entries for coefficients in the vector value by using the
coeffnames
function. For an example, see fit
. Individual
unconstrained lower bounds can be specified by
Inf
.
Available when the Method
is
LinearLeastSquares
or
NonlinearLeastSquares
.
Data Types: double
Upper
— Upper bounds on coefficients to be fitted
[ ] (default)  vector
Upper bounds on the coefficients to be fitted, specified as the
commaseparated pair consisting of 'Upper'
and a
vector. 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. Find the order
of the entries for coefficients in the vector value by using the
coeffnames
function. For an example, see fit
. Individual
unconstrained upper bounds can be specified by
+Inf
.
Available when the Method
is
LinearLeastSquares
or
NonlinearLeastSquares
.
Data Types: logical
StartPoint
— Initial values for coefficients
[ ] (default)  vector
Initial values for the coefficients, specified as the
commaseparated pair consisting of 'StartPoint'
and a vector. Find the order of the entries for coefficients in the
vector value by using the coeffnames
function. For an example, see fit
.
If no start points (the default value of an empty vector) are
passed to the fit
function,
starting points for some library models are determined
heuristically. For rational and Weibull models, and all custom
nonlinear models, the toolbox selects default initial values for
coefficients uniformly at random from the interval (0,1). As a
result, multiple fits using the same data and model might lead to
different fitted coefficients. To avoid this, specify initial values
for coefficients with a vector value for the
StartPoint
property.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
Algorithm
— Algorithm to use for fitting procedure
'LevenbergMarquardt' (default)  'TrustRegion'
Algorithm to use for the fitting procedure, specified as the
commaseparated pair consisting of 'Algorithm'
and either 'LevenbergMarquardt'
or
'TrustRegion'
.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: char
DiffMaxChange
— Maximum change in coefficients for finite difference gradients
0.1
(default)
Maximum change in coefficients for finite difference gradients,
specified as the commaseparated pair consisting of
'DiffMaxChange'
and a scalar.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
DiffMinChange
— Minimum change in coefficients for finite difference gradients
10^{–8}
(default)
Minimum change in coefficients for finite difference gradients,
specified as the commaseparated pair consisting of
'DiffMinChange'
and a scalar.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
Display
— Display option in the Command Window
'notify'
(default)  'final'
 'iter'
 'off'
Display option in the command window, specified as the
commaseparated pair consisting of 'Display'
and
one of these options:
'notify'
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.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: char
MaxFunEvals
— Maximum number of evaluations of model allowed
600
(default)
Maximum number of evaluations of the model allowed, specified as
the commaseparated pair consisting of
'MaxFunEvals'
and a scalar.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
MaxIter
— Maximum number of iterations allowed for fit
400
(default)
Maximum number of iterations allowed for the fit, specified as the
commaseparated pair consisting of 'MaxIter'
and
a scalar.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
TolFun
— Termination tolerance on model value
10^{–6}
(default)
Termination tolerance on the model value, specified as the
commaseparated pair consisting of 'TolFun'
and a
scalar.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
TolX
— Termination tolerance on coefficient values
10^{–6} (default)
Termination tolerance on the coefficient values, specified as the
commaseparated pair consisting of 'TolX'
and a
scalar.
Available when the Method
is
NonlinearLeastSquares
.
Data Types: double
Output Arguments
fitOptions
— Algorithm options
options object
Algorithm options, returned as an options object.
newOptions
— New algorithm options
options object
New algorithm options, returned as an options object.
Version History
Introduced before R2006aR2023a: fitoptions
returns value objects
Starting in R2023a, fitoptions
returns value objects
instead of handle objects. For more information, see Comparison of Handle and Value
Classes.
R2023a: Specify extrapolation method for surface interpolant fits
Starting in 2023a, you can specify the extrapolation method for interpolant
fits by using the ExtrapolationMethod
namevalue argument.
For curve fits, Curve Fitting Toolbox™ supports only the default extrapolation methods available in
previous releases.
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)