Curve and Surface Fitting

Fitting a Curve

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

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.

Fitting a Surface

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

2. 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' )
3. 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.

Model Types and Fit Analysis

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

Workflow for Command Line Fitting

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:

1. 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.

2. (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.

3. 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.

4. (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.

5. (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.

6. 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.

7. 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 MethodDescription

argnames

Get input argument names

category

Get fit category

coeffnames

Get coefficient names

coeffvalues

Get coefficient values

confint

Get confidence intervals for fit coefficients

dependnames

Get dependent variable name

differentiate

Differentiate fit

excludedata

Exclude data from fit

feval

Evaluate model at specified predictors

fittype

Construct fittype object

formula

Get formula

indepnames

Get independent variable name

integrate

Integrate curve fit

islinear

Determine if model is linear

numargs

Get number of input arguments

numcoeffs

Get number of coefficients

plot

Plot fit

predint

Get prediction intervals

probnames

Get problem-dependent parameter names

probvalues

Get problem-dependent parameter values

Numerically integrate surface fit (sfit object)

setoptions

Set model fit options

type

Get name of model