## Interactive Curve and Surface Fitting

### Introducing Curve Fitter App

You can fit curves and surfaces to data and view plots with the Curve Fitter 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 Curve

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

`load census`

2. Open the Curve Fitter app.

`curveFitter`
Alternatively, on the Apps tab, in the Math, Statistics and Optimization group, click Curve Fitter.

3. On the Curve Fitter tab, in the Data section, click . In the Select Fitting Data dialog box, select `cdate` as the X data value and `pop` as the Y data value. For details, see Selecting Data to Fit in Curve Fitter App.

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

4. Try different fit options. For example, in the Fit Options pane, change the polynomial Degree value to `3` to fit a cubic polynomial.

5. Select a different model type from the fit gallery in the Fit Type section on the Curve Fitter tab. For example, click the arrow to open the gallery, and click in the Smoothing group. For information about models you can fit, see Model Types for Curves and Surfaces.

6. In the Export section, click and select Generate Code.

The Curve Fitter app creates a file in the Editor containing MATLAB code to recreate the currently selected fit and its opened plots in your interactive session.

Tip

For a detailed workflow example, see Compare Fits in Curve Fitter App.

To create multiple fits and compare them, see Create Multiple Fits in Curve Fitter App.

### Fit Surface

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

`load franke`

2. Open the Curve Fitter app.

`curveFitter`

3. On the Curve Fitter tab, in the Data section, click . In the Select Fitting Data dialog box, select `x` as the X data value, `y` as the Y data value, and `z` as the Z data value. For more information, see Selecting Data to Fit in Curve Fitter App.

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

4. Select a different model type from the fit gallery in the Fit Type section on the Curve Fitter tab. For example, click the arrow to open the gallery, and click in the Regression Models group.

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. In the Export section, click and select Generate Code.

The Curve Fitter app creates a file in the Editor containing MATLAB code to recreate the currently selected fit and its opened plots in your interactive session.

Tip

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

To create multiple fits and compare them, see Create Multiple Fits in Curve Fitter App.

### Model Types for Curves and Surfaces

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

Fit GroupFit TypeCurvesSurfaces
Regression ModelsPolynomialYes (up to degree 9)Yes (up to degree 5)
ExponentialYesNo
LogarithmicYesNo
FourierYesNo
GaussianYesNo
PowerYesNo
RationalYesNo
Sum of SineYesNo
WeibullYesNo
SigmoidalYesNo
InterpolationInterpolant

Yes, with methods:

• Nearest neighbor

• Linear

• Cubic

• Shape-preserving (PCHIP)

Yes, with methods:

• Nearest neighbor

• Linear

• Natural neighbor

• Cubic

• Biharmonic (v4)

• Thin-plate spline

SmoothingSmoothing SplineYesNo
LowessNoYes
CustomCustom EquationYesYes
Custom Linear FittingYesNo

For information about these fit types, see:

### Selecting Data to Fit in Curve Fitter App

To interactively select data, import your data into a workspace variable from the command line. Then, open the Curve Fitter app and click . The button is in the Data section of the Curve Fitter tab.

The Curve Fitter 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.

#### Selecting Data for Curve Fitting

To select data for curve fitting, import the Y data in a vector, table variable, or array. If you have X data, import it also in a vector, table variable, or array. Each of the X and Y variables can be a vector, array, or table variable. However, X and Y must have the same number of elements. Then, do one of the following:

• In the Select Fitting Data dialog box, select the variables in the X data and Y data drop-down menus. To select data contained in a table variable, select the table name to display a second drop-down menu to the right. Then, select the variable from the second drop-down menu.

• To plot the Y variable against its indices, select only the Y variable from the Y data drop-down menu.

If you specify weights, the weights variable must have the same number of elements as the other data variables.

#### Selecting Data for Surface Fitting

To select data for surface fitting, save the X, Y and Z data each in a vector, table variable, or array. Each of the variables can be a vector, array, or table variable. However, they must satisfy one of the following:

• The X, Y, and Z variables contain the same number of elements.

Note

Curve Fitter app expects data variables to be the same size. If the sizes are different but the number of elements are the same, then the app reshapes the variables to create a fit and displays a warning in the Results pane.

• The X variable is a vector containing n elements, the Y variable is a vector containing m elements, and the Z variable is an m-by-n matrix. In this case, the Y and X data are treated as headers for table rows and columns, respectively. See Tabular Data for more information.

Note

If the `Z` variable is an n-by-m matrix, the app creates a fit by transposing `Z` and provides a warning about the data transformation. You can see the warning in the Results pane.

In the Select Fitting Data dialog box, select the variables from the X data, Y data, and Z data drop-down menus. If a variable is saved in a table, select the table name to display a second drop-down menu to the right. Then, select the variable from the second drop-down menu.

Similarly, you can select any numeric variable in your workspace to use as Weights, including a numeric table column. If you specify weights, the weights variable must have the same number of elements as the Z variable.

### Save and Reopen Sessions

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

To save your session, first click the button in the File section on the Curve Fitter tab to open your file browser. Next, select a name and location for your session file (with the file extension `.sfit`).

After you save your session once, you can click and select Save Session to overwrite that session with subsequent saves.

To save the current session under a different name, click and select Save Session As.

To reopen a session, click in the File section on the Curve Fitter tab to open a file browser where you can select a saved curve fitting session file to load.