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.

A typical analysis using curve fitting methods proceeds as follows:

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 more specialized MATLAB functions for reading data from particular file types.(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 can specify the model by passing a string 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.You pass the data, a model (string, expression or

`fittype`

object), and (optionally) a fit options structure and an exclusion rule, to 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.You can then postprocess the

`cfit`

and`sfit`

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`

.

For more details on functions, see Curve and Surface Fitting Objects and Methods.

Was this topic helpful?