Mathematics Previous page   Next Page

The Basic Fitting Interface

MATLAB supports curve fitting through the Basic Fitting interface. Using this interface, you can quickly perform many curve fitting tasks within the same easy-to-use environment. The interface is designed so that you can

Depending on your specific curve fitting application, you can use the Basic Fitting interface, the command line functionality, or both.

You can use the Basic Fitting interface only with 2-D data. However, if you plot multiple data sets as a subplot, and at least one data set is 2-D, then the interface is enabled.

Improving Fitting Efficiency

MATLAB does not sort your data before fitting it with the Basic Fitting interface, although it did so in prior releases. This change in behavior was made to improve handling of residual plots. However, as sorted data is faster to fit and plot, you can improve performance of the Basic Fitting interface with large data sets by presorting your data so that the x values are in ascending order. If your original data consists of the vectors x and y, you can create sorted vectors x_sorted and y_sorted as follows:

Overview of the Basic Fitting Interface

The full Basic Fitting interface is shown below. To reproduce this state, follow these three steps:

  1. Plot some data.
  2. Select Basic Fitting from the Tools menu.
  3. Click the right arrow button twice.

    figure showing the basic fitting interface

Select data - This parameter list is populated with the names of all the data sets you display in the figure window associated with the Basic Fitting interface.

Use this list to select the current data set -- the data set that you want to fit. You can fit only one data set at a time. However, you can perform multiple fits for the current data set. Use the Plot Editor to change the name of a data set.

Center and scale X data - If checked, the data is centered at zero mean and scaled to unit standard deviation. You may need to center and scale your data to improve the accuracy of the subsequent numerical computations. MATLAB displays a warning is displayed if a fit produces results that might be inaccurate.

Plot fits - This panel enables you to visually explore one or more fits to the current data set:

Numerical results - This panel allows you to explore the numerical results of a single fit to the current data set without plotting the fit:

Example: Using the Basic Fitting Interface

This example illustrates the features of the Basic Fitting interface by fitting a cubic polynomial to the census data. You may want to repeat this example using different equations and compare results. To launch the interface:

  1. Plot some data.
  2. Select Basic Fitting from the Tools menu in the figure.sub menu showing basic fitting as a choice

Configure the Basic Fitting interface to:

This configuration is shown below.

configured basic fitting interface

The Plot fits panel enables you to visually explore multiple fits to the current data set. For comparison, try fitting additional equations to the census data by selecting the appropriate check boxes. If an equation produces results that might be numerically inaccurate, MATLAB displays a warning. In this case, you should select the Center and scale X data check box to improve the numerical accuracy.

The resulting fit and the residuals are shown in the following plot.

resulting fit and the residuals

The plot legend indicates the name of the data set and the equation. If the legend covers part of the plot, you can click and drag it to another location. The legend is automatically updated as you add or remove data sets or fits. Additionally, fits are displayed using a default set of line styles and colors. You can change any of the default plot settings using the Plot Editor. However, any changes you make are undone if you subsequently perform another fit. To retain changes, you should wait until after you have finished fitting your data.

By selecting the right arrow button, you can examine the fit coefficients and the norm of the residuals.

fit coefficients and the norm of the residuals

The Fit menu enables you to explore numerical fit results for the current data set without plotting the fit. For comparison, you can display the numerical results for other fits by selecting the desired equation. Note that if you want to display a fit in the data plot, you have to select the associated check box in Plot fits.

You can save the fit results to the MATLAB workspace by selecting the Save to workspace button.

The save fit to workspace window allows you to save the fit as a MATLAB structure and save the norm of the residuals as a MATLAB variable.

The fit structure is

You may want to use this structure for subsequent display or analysis. For example, you can use the saved coefficients and the polyval function to evaluate the cubic polynomial at the command line.

By selecting the right arrow button again, you can specify a vector of x-values at which to evaluate the current fit. Enter the vector in the field next to the Evaluate button, and then click Evaluate. For example, if you enter the vector 2000:10:2050, the population for the years 2000 to 2050 is evaluated in increments of 10. The x-values and the corresponding values for f(x), evaluated from the fit, are displayed in the pane below Evaluate, as shown in the following figure.

The x-values and the corresponding values for f(x), evaluated from the fit

Select the Plot evaluated results check box to display the evaluated points along with the current data set in the data plot, as shown in the following figure.

the evaluated points along with the current data set in the data plot

You can save the evaluated data to the MATLAB workspace by selecting the Save to workspace button.

save results to work space dialog box


Previous page  Error Bounds Difference Equations and Filtering Next page

© 1994-2005 The MathWorks, Inc.