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.
Load some example data at the MATLAB® command line:
load census
Open the Curve Fitting app by entering:
cftool
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.
Try different fit options. For example, change the polynomial
Degree to 3
to fit a cubic
polynomial.
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.
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.
For a detailed workflow example, see Compare Fits in Curve Fitting App.
To create multiple fits and compare them, see Create Multiple Fits in Curve Fitting App.
Load some example data at the MATLAB command line:
load franke
Open the Curve Fitting app:
cftool
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.
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.
Try different fit options for your chosen model type.
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.
For a detailed example, see Surface Fitting to Franke Data.
To create multiple fits and compare them, see Create Multiple Fits in Curve Fitting App.
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 Category | Curves | Surfaces |
---|---|---|
Regression Models | ||
Polynomial | Yes (up to degree 9) | Yes (up to degree 5) |
Exponential | Yes | |
Fourier | Yes | |
Gaussian | Yes | |
Power | Yes | |
Rational | Yes | |
Sum of Sine | Yes | |
Weibull | Yes | |
Interpolation | ||
Interpolant | Yes Methods: Nearest neighbor Linear Cubic Shape-preserving (PCHIP) | Yes Methods: Nearest neighbor Linear Cubic Biharmonic (v4) Thin-plate spline |
Smoothing | ||
Smoothing Spline | Yes | |
Lowess | Yes | |
Custom | ||
Custom Equation | Yes | Yes |
Linear Fitting | Yes |
For information about these fit types, see:
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.
For more information see Selecting Compatible Size Surface Data.
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.
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
Inf
s, NaN
s, 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.
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.
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 Session to overwrite that session for subsequent saves.
To save the current session under a different name, select File > Save Session As.
Use File > Load Session to open a file browser where you can select a saved curve fitting session file to load.
Use File > Clear Session to remove all fits from the current Curve Fitting app session.