Accelerating the pace of engineering and science

# Documentation Center

• Trials

## Interactive Curve and Surface Fitting

### Introducing the Curve Fitting App

You can fit curves and surfaces to data and view plots with the Curve Fitting app.

• Create, plot, and compare multiple fits.

• Use linear or nonlinear regression, interpolation, smoothing, and custom equations.

• View goodness-of-fit statistics, display confidence intervals and residuals, remove outliers and assess fits with validation data.

• Automatically generate code to fit and plot curves and surfaces, or export fits to the workspace for further analysis.

### Fit a Curve

1. Load some example data at the MATLAB® command line:

`load census`
2. Open the Curve Fitting app by entering:

`cftool`

Alternatively, click Curve Fitting on the Apps tab.

3. Select X data and Y data. For details, see Selecting Data to Fit in Curve Fitting App.

The Curve Fitting app creates a default polynomial fit to the data.

4. Try different fit options. For example, change the polynomial Degree to 3 to fit a cubic polynomial.

5. Select a different model type from the fit category list, e.g., Smoothing Spline. For information about models you can fit, see Model Types for Curves and Surfaces.

6. Select File > Generate Code.

The Curve Fitting app creates a file in the Editor containing MATLAB code to recreate all fits and plots in your interactive session.

 Tip   For a detailed workflow example, see Curve Fitting to Census Data.

To create multiple fits and compare them, see Interactive Fit Comparison.

### Fit a Surface

1. Load some example data at the MATLAB command line:

`load franke`
2. Open the Curve Fitting app:

`cftool`
3. Select X data, Y data and Z data. For more information, see Selecting Data to Fit in Curve Fitting App.

The Curve Fitting app creates a default interpolation fit to the data.

4. Select a different model type from the fit category list, e.g., Polynomial.

For information about models you can fit, see Model Types for Curves and Surfaces.

5. Try different fit options for your chosen model type.

6. Select File > Generate Code.

The Curve Fitting app creates a file in the Editor containing MATLAB code to recreate all fits and plots in your interactive session.

 Tip   For a detailed example, see Surface Fitting to Franke Data.

To create multiple fits and compare them, see Interactive Fit Comparison.

### Model Types for Curves and Surfaces

Based on your selected data, the fit category list shows either curve or surface fit categories. The following table describes the options for curves and surfaces.

Fit CategoryCurvesSurfaces
Regression Models
PolynomialYes (up to degree 9)Yes (up to degree 5)
ExponentialYes
FourierYes
GaussianYes
PowerYes
RationalYes
Sum of SineYes
WeibullYes
Interpolation
InterpolantYes
Methods:
Nearest neighbor
Linear
Cubic
Shape-preserving (PCHIP)
Yes
Methods:
Nearest neighbor
Linear
Cubic
Biharmonic (v4)
Thin-plate spline
Smoothing
Smoothing SplineYes
Lowess Yes
Custom
Custom EquationYesYes
Linear FittingYes

For information about these fit types, see:

### Selecting Data to Fit in Curve Fitting App

To select data to fit, use the drop-down lists in the Curve Fitting app to select variables in your MATLAB workspace.

• To fit curves:

• Select X data and Y data.

• Select only Y data to plot Y against index (x=1:length( y )).

• To fit surfaces, select X data, Y data and Z data.

You can use the Curve Fitting app drop-down lists to select any numeric variables (with more than one element) in your MATLAB workspace.

Similarly, you can select any numeric data in your workspace to use as Weights.

For curves, X, Y, and Weights must be matrices with the same number of elements.

For surfaces, X, Y, and Z must be either:

• Matrices with the same number of elements

• Data in the form of a table

For surfaces, weights must have the same number of elements as Z.

When you select variables, the Curve Fitting app immediately creates a curve or surface fit with the default settings. If you want to avoid time-consuming refitting for large data sets, you can turn off Auto fit by clearing the check box.

 Note:   The Curve Fitting app uses a snapshot of the data you select. Subsequent workspace changes to the data have no effect on your fits. To update your fit data from the workspace, first change the variable selection, and then reselect the variable with the drop-down controls.

If there are problems with the data you select, you see messages in the Results pane. For example, the Curve Fitting app ignores Infs, NaNs, and imaginary components of complex numbers in the data, and you see messages in the Results pane in these cases.

If you see warnings about reshaping your data or incompatible sizes, read Selecting Compatible Size Surface Data and Troubleshooting Data Problems for information.

#### Overview

You can save and reload sessions for easy access to multiple fits. The session file contains all the fits and variables in your session and remembers your layout.

#### Saving Sessions

To save your session, first select File > Save Session to open your file browser. Next, select a name and location for your session file (with file extension .sfit).

After you save your session once, you can use File > Save MySessionName to overwrite that session for subsequent saves.

To save the current session under a different name, select File > Save Session As .