2 Select Basic Fitting from the Tools menu.
3 Click the
button twice.
13-31
13 Data Analysis and Statistics
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 current data set is defined as
the data set that is to be 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. A warning
is displayed if a fit produces results that may be inaccurate.
Plot fits – This panel allows you to visually explore one or more fits to the
current data set:
• Check to display fits on figure – Select the fits you want to display for the
current data set. There are two types of fits to choose from: interpolants and
polynomials. The spline interpolant uses the spline function, while the
13-32
Case Study: Curve Fitting
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.
• Show equations – If checked, the fit equation is displayed on the plot.
- Significant digits – Select the significant digits associated with the
equation display.
• Plot residuals – If checked, the fit residuals are displayed. The fit residuals
are defined as the difference between the ordinate data point and the
resulting fit for each abscissa data point. You can display the residuals as a
bar plot, as a scatter plot, or as a line plot in the same figure window as the
data or in a separate figure window. If you use subplots to plot multiple data
sets, then residuals can be plotted only in a separate figure window.
- Show norm of residuals – If checked, the norm of residuals are displayed.
The norm of residuals is a measure of the goodness of fit, where a smaller
value indicates a better fit than a larger value. It is calculated using the
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:
• Fit – Select the equation to fit to the current data set. The fit results are
displayed in the list box below the menu. Note that selecting an equation in
this menu does not affect the state of the Plot fits panel. Therefore, if you
want to display the fit in the data plot, you may need to select the associated
check box in Plot fits.
• Coefficients and norm of residuals – Display the numerical results for the
equation selected in Fit. Note that when you first open the Numerical
Results panel, the results of the last fit you selected in Plot fits are
displayed.
• Save to workspace – Launch a dialog box that allows you to save the fit
results to workspace variables.
• Find Y = f(X) – Interpolate or extrapolate the current fit.
13-33
13 Data Analysis and Statistics
- Enter value(s) – Enter a MATLAB expression to evaluate for the current
fit. The expression is evaluated after you press the Evaluate button, and
the results are displayed in the associated table. The current fit is
displayed in the Fit menu.
- Save to workspace – Launch a dialog box that allows you to save the
evaluated results to workspace variables.
- Plot results – If checked, the evaluated results are displayed on the data
plot.
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.
plot(cdate,pop,'ro')
2 Select Basic Fitting from the Tools menu in the figure.
13-34
Case Study: Curve Fitting
Configure the Basic Fitting interface to:
• Fit a cubic polynomial to the data.
• Display the equation in the data plot.
• Plot the fit residuals as a bar plot, and display the residuals as a subplot of
the data figure window.
• Display the norm of the residuals.
This configuration is shown below.
Current data set
Fit a cubic polynomial to the data
Show the equation
Plot the residuals as a bar plot in
the data figure window
Show the norm of the residuals
The Plot fits panel allows 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 may
be numerically inaccurate, a warning is displayed. In this case, you should
select the Center and scale X data check box to improve the numerical
accuracy.
13-35
13 Data Analysis and Statistics
The resulting fit and the residuals are shown below.
The plot legend indicates the name of the data set and the equation. 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.