| Curve Fitting Toolbox™ | ![]() |
| On this page… |
|---|
The Curve Fitting Tool is a graphical user interface (GUI) that allows you to
Visually explore one or more data sets and fits as scatter plots.
Graphically evaluate the goodness of fit using residuals and prediction bounds.
Access additional interfaces for
Importing, viewing, and smoothing data
Fitting data, and comparing fits and data sets
Marking data points to be excluded from a fit
Selecting which fits and data sets are displayed in the tool
Interpolating, extrapolating, differentiating, or integrating fits
You open Curve Fitting Tool with the cftool command.
cftool

Before you can import data into Curve Fitting Tool, the data variables must exist in the MATLAB workspace. For this example, the data is stored in the MATLAB file census.mat.
load census
The workspace now contains two new variables, cdate and pop:
cdate is a column vector containing the years 1790 to 1990 in 10-year increments.
pop is a column vector with the US population figures that correspond to the years in cdate.
You can import data into Curve Fitting Tool with the Data GUI. You open this GUI by clicking the Data button on Curve Fitting Tool. As shown below, the Data GUI consists of two panes: Data sets and Smooth. The Data Sets pane allows you to
Import predictor (X) data, response (Y) data, and weights. If you do not import weights, then they are assumed to be 1 for all data points.
Specify the name of the data set.
Preview the data.
To load cdate and pop into Curve Fitting Tool, select the appropriate variable names from the X Data and Y Data lists. The data is then displayed in the Preview window. Click the Create data set button to complete the data import process.

The Smooth pane is described in Preprocessing Data.
You fit data with the Fitting GUI. You open this GUI by clicking the Fitting button on Curve Fitting Tool. The Fitting GUI consists of two parts: the Fit Editor and the Table of Fits. The Fit Editor allows you to
Specify the fit name, the current data set, and the exclusion rule.
Explore various fits to the current data set using a library or custom equation, a smoothing spline, or an interpolant.
Override the default fit options such as the coefficient starting values.
Compare fit results including the fitted coefficients and goodness of fit statistics.
The Table of Fits allows you to
Keep track of all the fits and their data sets for the current session.
Display a summary of the fit results.
Save or delete the fit results.
For this example, begin by fitting the census data with a second degree polynomial. Then continue fitting the data using polynomial equations up to sixth degree, and a single-term exponential equation.
The data fitting procedure follows these general steps:
From the Fit Editor, click New Fit.
Note that this action always defaults to a linear polynomial fit type. You use New Fit at the beginning of your curve fitting session, and when you are exploring different fit types for a given data set.
Because the initial fit uses a second degree polynomial, select quadratic polynomial from the Polynomial list. Name the fit poly2.
Click the Apply button or select the Immediate apply check box. The library model, fitted coefficients, and goodness of fit statistics are displayed in the Results area.
Fit the additional library equations.
For fits of a given type (for example, polynomials), you should use Copy Fit instead of New Fit because copying a fit retains the current fit type state thereby requiring fewer steps than creating a new fit each time.
The Fitting GUI is shown below with the results of fitting the census data with a quadratic polynomial.

The data, fit, and residuals are shown below. You display the residuals as a line plot by selecting the menu item View > Residuals > Line plot from Curve Fitting Tool.

The residuals indicate that a better fit may be possible. Therefore, you should continue fitting the census data following the procedure outlined in the beginning of this section.
The residuals from a good fit should look random with no apparent pattern. A pattern, such as a tendency for consecutive residuals to have the same sign, can be an indication that a better model exists.
When you fit higher degree polynomials, the Results area displays this warning:
Equation is badly conditioned. Remove repeated data points or try centering and scaling.
The warning arises because the fitting procedure uses the cdate values as the basis for a matrix with very large values. The spread of the cdate values results in scaling problems. To address this problem, you can normalize the cdate data. Normalization is a process of scaling the predictor data to improve the accuracy of the subsequent numeric computations. A way to normalize cdate is to center it at zero mean and scale it to unit standard deviation.
(cdate - mean(cdate))./std(cdate)
To normalize data with Curve Fitting Tool, select the Center and scale X data check box.
Note Because the predictor data changes after normalizing, the values of the fitted coefficients also change when compared to the original data. However, the functional form of the data and the resulting goodness of fit statistics do not change. Additionally, the data is displayed in Curve Fitting Tool using the original scale. |
To determine the best fit, you should examine both the graphical and numerical fit results.
Examining the Graphical Fit Results. Your initial approach in determining the best fit should be a graphical examination of the fits and residuals. The graphical fit results shown below indicate that
The fits and residuals for the polynomial equations are all similar, making it difficult to choose the best one.
The fit and residuals for the single-term exponential equation indicate it is a poor fit overall. Therefore, it is a poor choice for extrapolation.

Use the Plotting GUI to remove exp1 from the scatter plot display.

Because the goal of fitting the census data is to extrapolate the best fit to predict future population values, you should explore the behavior of the fits up to the year 2050. You can change the axes limits of Curve Fitting Tool by selecting the menu item Tools > Axes Limit Control.
The census data and fits are shown below for an upper abscissa limit of 2050. The behavior of the sixth degree polynomial fit beyond the data range makes it a poor choice for extrapolation.

As you can see, you should exercise caution when extrapolating with polynomial fits because they can diverge wildly outside the data range.
Examining the Numerical Fit Results. Because you can no longer eliminate fits by examining them graphically, you should examine the numerical fit results. There are two types of numerical fit results displayed in the Fitting GUI: goodness of fit statistics and confidence intervals on the fitted coefficients. The goodness of fit statistics help you determine how well the curve fits the data. The confidence intervals on the coefficients determine their accuracy.
Some goodness of fit statistics are displayed in the Results area of the Fit Editor for a single fit. All goodness of fit statistics are displayed in the Table of Fits for all fits, which allows for easy comparison.
In this example, the sum of squares due to error (SSE) and the adjusted R-square statistics are used to help determine the best fit. The SSE statistic is the least-squares error of the fit, with a value closer to zero indicating a better fit. The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.
You can modify the information displayed in the Table of Fits with the Table Options GUI. You open this GUI by clicking the Table options button on the Fitting GUI. As shown below, select the adjusted R-square statistic and clear the R-square statistic.

The numerical fit results are shown below. You can click the Table of Fits column headings to sort by statistics results.
The SSE for exp1 indicates it is a poor fit, which was already determined by examining the fit and residuals. The lowest SSE value is associated with poly6. However, the behavior of this fit beyond the data range makes it a poor choice for extrapolation. The next best SSE value is associated with the fifth degree polynomial fit, poly5, suggesting it may be the best fit. However, the SSE and adjusted R-square values for the remaining polynomial fits are all very close to each other. Which one should you choose?

To resolve this issue, examine the confidence bounds for the remaining fits. By default, 95% confidence bounds are calculated. You can change this level by selecting the menu item View > Confidence Level from Curve Fitting Tool.
The p1, p2, and p3 coefficients for the fifth degree polynomial suggest that it overfits the census data. However, the confidence bounds for the quadratic fit, poly2, indicate that the fitted coefficients are known fairly accurately. Therefore, after examining both the graphical and numerical fit results, it appears that you should use poly2 to extrapolate the census data.
Note The fitted coefficients associated with the constant, linear, and quadratic terms are nearly identical for each polynomial equation. However, as the polynomial degree increases, the coefficient bounds associated with the higher degree terms increase, which suggests overfitting. |
By clicking the Save to workspace button, you can save the selected fit and the associated fit results to the MATLAB workspace. The fit is saved as a MATLAB object and the associated fit results are saved as structures. This example saves all the fit results for the best fit, poly2.

fittedmodel1 is saved as a Curve Fitting Toolbox cfit object.
whos fittedmodel1 Name Size Bytes Class fittedmodel1 1x1 6178 cfit object Grand total is 386 elements using 6178 bytes
The cfit object display includes the model, the fitted coefficients, and the confidence bounds for the fitted coefficients.
fittedmodel1
fittedmodel1 =
Linear model Poly2:
fittedmodel1(x) = p1*x^2 + p2*x + p3
Coefficients (with 95% confidence bounds):
p1 = 0.006541 (0.006124, 0.006958)
p2 = -23.51 (-25.09, -21.93)
p3 = 2.113e+004 (1.964e+004, 2.262e+004)The goodness1 structure contains goodness of fit results.
goodness1
goodness1 =
sse: 159.0293
rsquare: 0.9987
dfe: 18
adjrsquare: 0.9986
rmse: 2.9724The output1 structure contains additional information associated with the fit.
output1
output1 =
numobs: 21
numparam: 3
residuals: [21x1 double]
Jacobian: [21x3 double]
exitflag: 1
algorithm: 'QR factorization and solve'You can evaluate (interpolate or extrapolate), differentiate, or integrate a fit over a specified data range with the Analysis GUI. You open this GUI by clicking the Analysis button on Curve Fitting Tool.
For this example, you will extrapolate the quadratic polynomial fit to predict the US population from the year 2000 to the year 2050 in 10 year increments, and then plot both the analysis results and the data. To do this:
Enter the appropriate MATLAB vector in the Analyze at Xi field.
Select the Evaluate fit at Xi check box.
Select the Plot results and Plot data set check boxes.
Click the Apply button.
The numerical extrapolation results are shown below.

The extrapolated values and the census data set are displayed together in a new figure window.

By clicking the Save to workspace button, you can save the extrapolated values as a structure to the MATLAB workspace.

The resulting structure is shown below.
analysisresults1
analysisresults1 =
xi: [6x1 double]
yfit: [6x1 double]Curve Fitting Toolbox software provides you with several options for saving your work. You can save one or more fits and the associated fit results as variables to the MATLAB workspace. You can then use this saved information for documentation purposes, or to extend your data exploration and analysis. In addition to saving your work to MATLAB workspace variables, you can
Before performing any of these tasks, you may want to remove unwanted data sets and fits from Curve Fitting Tool display. An easy way to do this is with the Plotting GUI. The Plotting GUI shown below is configured to display only the census data and the best fit, poly2.

The curve fitting session is defined as the current collection of fits for all data sets. You may want to save your session so that you can continue data exploration and analysis at a later time using Curve Fitting Tool without losing any current work.
Save the current curve fitting session by selecting the menu item File > Save Session from Curve Fitting Tool. The Save Session dialog is shown below.

The session is stored in binary form in a cfit file, and contains this information:
All data sets and associated fits
The state of the Fitting GUI, including Table of Fits entries and exclusion rules
The state of the Plotting GUI
To avoid saving unwanted data sets, you should delete them from Curve Fitting Tool. You delete data sets using the Data Sets pane of the Data GUI. If there are fits associated with the unwanted data sets, they are deleted as well.
You can load a saved session by selecting the menu item File > Load Session from Curve Fitting Tool. When the session is loaded, the saved state of Curve Fitting Tool display is reproduced, and may display the data, fits, residuals, and so on. If you open the Fitting GUI, then the loaded fits are displayed in the Table of Fits. Select a fit from this table to continue your curve fitting session.
You may want to generate an M-file that captures your work, so that you can continue your analysis outside of Curve Fitting Tool. The M-file can be used without modification, or it can be edited as needed.
To generate an M-file from a session in Curve Fitting Tool, select the menu item File > Generate M-file.
The M-file captures the following information from Curve Fitting Tool:
Names of variables, fits, and residuals
Fit options, such as whether the data should be normalized, initial values for the coefficients, and the fitting method
Curve fitting objects and methods used to create the fit
You can recreate your Curve Fitting Tool session by calling the M-file from the command line with your original data as input arguments. You can also call the M-file with new data, and automate the process of fitting multiple data sets.
For more information on working with a generated M-file, see Interactive Code Generation.
![]() | Product Overview | Programmatic Curve Fitting | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |