Main Content

To programmatically fit a curve, follow the steps in this simple example:

Load some data.

load hahn1

Create a fit using the

`fit`

function, specifying the variables and a model type (in this case`rat23`

is the model type).f = fit( temp, thermex, 'rat23' )

Plot your fit and the data.

plot( f, temp, thermex ) f( 600 )

For an example comparing various polynomial fits, see Polynomial Curve Fitting.

To programmatically fit a surface, follow the steps in this simple example:

Load some data.

load franke

Create a fit using the

`fit`

function, specifying the variables and a model type (in this case`poly23`

is the model type).f = fit( [x, y], z, 'poly23' )

Plot your fit and the data.

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

For an example fitting custom equations, see Surface Fitting With Custom Equations to Biopharmaceutical Data.

For details and examples of specific model types and fit analysis, see the following sections:

Curve Fitting Toolbox™ software provides a variety of methods for data analysis and modeling.

**Tip**

To quickly assemble MATLAB^{®} code for curve and surface fits
and plots, use Curve Fitting app and then generate code. You can transform
your interactive analysis of a single data set into a reusable function
for command-line analysis or for batch processing of multiple data
sets. See Generate Code and Export Fits to the Workspace.

To use curve fitting functions for programmatic fitting and analysis, follow this workflow:

Import your data into the MATLAB workspace using the

`load`

command (if your data has previously been stored in MATLAB variables) or any of the MATLAB functions for reading data from particular file types. You might need to reshape your data: see`prepareCurveData`

or`prepareSurfaceData`

.(Optional) If your data is noisy, you might want to smooth it using the

`smooth`

function. Smoothing is used to identify major trends in the data that can assist you in choosing an appropriate family of parametric models. If a parametric model is not evident or appropriate, smoothing can be an end in itself, providing a nonparametric fit of the data.**Note**Smoothing estimates the center of the distribution of the response at each predictor. It invalidates the assumption that errors in the data are independent, and so also invalidates the methods used to compute confidence and prediction intervals. Accordingly, once a parametric model is identified through smoothing, the

*original*data should be passed to the`fit`

function.Specify a parametric model for the data—either a Curve Fitting Toolbox library model or a custom model that you define. You specify the model by passing a model name or expression to the

`fit`

function or (optional) with a`fittype`

object you create with the`fittype`

function.To view available library models, see List of Library Models for Curve and Surface Fitting.

(Optional) You can create a fit options structure for the fit using the

`fitoptions`

function. Fit options specify things like weights for the data, fitting methods, and low-level options for the fitting algorithm.(Optional) You can create an exclusion rule for the fit using the

`excludedata`

function. Exclusion rules indicate which data values will be treated as outliers and excluded from the fit.Specify the x and y (and z, if surface fitting) data, a model (name, expression or

`fittype`

object), and (optionally) a fit options structure and an exclusion rule, with the`fit`

function to perform the fit.The

`fit`

function returns a`cfit`

(for curves) or`sfit`

(for surfaces) object that encapsulates the computed coefficients and the fit statistics. If you want to learn more about the fit objects, see Curve and Surface Fitting Objects and Methods.You can postprocess the fit objects returned by the

`fit`

function, by passing them to a variety of functions, such as`feval`

,`differentiate`

,`integrate`

,`plot`

,`coeffvalues`

,`probvalues`

,`confint`

, and`predint`

.

Use the following functions to work with curve and surface fits.

Curve or Surface Fit Method | Description |
---|---|

Get input argument names | |

Get fit category | |

Get coefficient names | |

Get coefficient values | |

Get confidence intervals for fit coefficients | |

Get dependent variable name | |

Differentiate fit | |

`excludedata` | Exclude data from fit |

Evaluate model at specified predictors | |

Construct | |

Get formula | |

Get independent variable name | |

Integrate curve fit | |

Determine if model is linear | |

Get number of input arguments | |

Get number of coefficients | |

Plot fit | |

Get prediction intervals | |

Get problem-dependent parameter names | |

Get problem-dependent parameter values | |

Numerically integrate surface fit ( | |

Set model fit options | |

Get name of model |

`fit`

| `fittype`

| `fitoptions`

| `excludedata`

| `prepareCurveData`

| `prepareSurfaceData`