Mathematics |
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:
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:
spline
function, while the shape-preserving interpolant uses the pchip
function. Refer to the pchip
online help for a comparison of these two functions. The polynomial fits use the polyfit
function. You can choose as many fits for a given data set as you want.
If your data set has N points, then you should use polynomials with, at most, N coefficients. If your fit uses polynomials with more than N coefficients, the interface automatically sets a sufficient number of coefficients to 0
during the calculation so that the system is not underdetermined.
norm
function, norm(V,2)
, where V
is the vector of residuals.
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:
Configure the Basic Fitting interface to:
This configuration is shown below.
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.
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.
Note If you change the name of a data set in the legend, then the name is automatically changed in the Select data menu. |
By selecting the button, you can examine the 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.
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 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.
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.
You can save the evaluated data to the MATLAB workspace by selecting the Save to workspace button.
Error Bounds | Difference Equations and Filtering |
© 1994-2005 The MathWorks, Inc.