| MATLAB® | ![]() |
| On this page… |
|---|
The MATLAB® Basic Fitting GUI allows you to interactively:
Model data using a spline interpolant, a shape-preserving interpolant, or a polynomial up to the tenth degree
Plot one or more fits together with data
Plot the residuals of the fits
Compute model coefficients
Compute the norm of the residuals (a measure of the goodness of fit)
Use the model to interpolate or extrapolate outside of the data
Save coefficients and computed values to the MATLAB workspace for use outside of the GUI
Generate an M-file to recompute fits and reproduce plots with new data
Note The Basic Fitting GUI is only available for 2-D plots. For more advanced fitting and regression analysis, see the Curve Fitting Toolbox documentation and the Statistics Toolbox documentation. |
The Basic Fitting GUI sorts your data in ascending order before fitting. If your data set is large and the values are not sorted in ascending order, it will take longer for the Basic Fitting GUI to preprocess your data before fitting.
You can speed up the Basic Fitting GUI by first sorting your data. To create sorted vectors x_sorted and y_sorted from data vectors x and y, use the MATLAB sort function:
[x_sorted, i] = sort(x); y_sorted = y(i);
To use the Basic Fitting GUI, you must first plot your data in a figure window, using any MATLAB plotting command that produces (only) x and y data.
To open the Basic Fitting GUI, select Tools > Basic Fitting from the menus at the top of the figure window.

The GUI consists of three panels:
For selecting a model and plotting options
For examining and exporting model coefficients and norms of residuals
For examining and exporting interpolated and extrapolated values
To expand or collapse the panels, use the arrow button in the lower right corner of the interface.
The example in this section shows you how to use the Basic Fitting GUI.
The file census.mat contains U.S. population data for the years 1790 through 1990.
To load and plot the data, type the following commands at the MATLAB prompt:
load census plot(cdate,pop,'ro')
The load command adds the following two variables to the MATLAB workspace:
cdate is a column vector containing the years from 1790 to 1990 in increments of 10. This is the predictor variable.
pop is a column vector with U.S. population for each year in cdate. This is the response variable.
The data vectors are sorted in ascending order, by year. The plot shows the population as a function of year.
Now you are ready to fit the data.
Open the Basic Fitting dialog box by selecting Tools > Basic Fitting in the Figure window.

In the Plot fits area of the Basic Fitting dialog box, select the cubic check box to fit a cubic polynomial to the data.
MATLAB displays the following warning:
Polynomial is badly conditioned. Removing repeated data points or centering and scaling may improve results.
The warning indicates that the computed coefficients for the model will be highly sensitive to random errors in the response (in this case, the measured population). To improve model accuracy, it is helpful to transform the predictors (in this case, the dates) by normalizing their center and scale. This is done by computing the z-scores:
![]()
where x is the predictor data, μ is the mean of x, and σ is the standard deviation of x. This centers the data at 0, with a standard deviation of 1.
To perform this transformation on the predictor data, select the Center and scale x data check box.
After centering and scaling, model coefficients are computed for the y data as a function of z. These are different (and more robust) than the coefficients computed for y as a function of x. The form of the model, and the norm of the residuals, is unchanged. The Basic Fitting GUI automatically rescales the z-scores so that the fit is displayed on the same scale as the original x data.
The Basic Fitting GUI calls the MATLAB functions polyfit and polyval to compute and display the fit. To understand the way in which the centered and scaled data is used as an intermediary to create the final plot, type the following at the MATLAB command prompt:
load census x = cdate; y = pop; z = (x-mean(x))/std(x); % Compute z-scores of x data plot(x,y,'ro') % Plot data hold on zfit = linspace(z(1),z(end),100); pz = polyfit(z,y,3); % Compute conditioned fit yfit = polyval(pz,zfit); xfit = linspace(x(1),x(end),100); plot(xfit,yfit,'b-') % Plot conditioned fit vs. x data
Select the following options:
Display the model equation in the plot
Display the residuals as a subplot
Display the norm of the residuals in the plot

The resulting display is shown in the following figure:

The cubic fit is a poor predictor before the year 1790, where it indicates a decreasing population. The model seems to approximate the data reasonably well after 1790, but a pattern in the residuals shows that the model does not meet the assumption of normal error, which is a basis for the least-squares fitting carried out by the Basic Fitting GUI.
For comparison, try fitting another equation to the census data by selecting it in the Plot fits area.
Tip You can change the default plot settings or rename data sets with the Property Editor. |
In the Basic Fitting dialog box, click the arrow button
to display the estimated
coefficients and the norm of the residuals in the Numerical
results panel.

To view a specific fit, select it from the Fit list. This displays the coefficients in the Basic Fitting dialog box, but does not plot the fit in the figure window.
Note If you also want to display a fit on the plot, you must select the corresponding Plot fits check box. |
Save the fit data to the MATLAB workspace by clicking the Save to workspace button on the Numerical results panel. This opens the following dialog box:

Click OK to save the fit parameters as a MATLAB structure:
fit
fit =
type: 'polynomial degree 3'
coeff: [0.9210 25.1834 73.8598 61.7444]
You can now use the fit results in MATLAB programming, outside of the Basic Fitting GUI.
Suppose you wish to use the cubic model to interpolate the U.S. population in 1965 (not in the original data).
In the Basic Fitting dialog box, click the
button
to specify a vector of x values at which to evaluate
the current fit.
In the Enter value(s)... field, type the following value:
1965
Note Use unscaled and uncentered x values. You do not need to center and scale first, even though you selected to scale x values to obtain the coefficients in Fitting Data. Basic Fitting makes the necessary adjustments behind the scenes. |
Click Evaluate.
The x values and the corresponding values for f(x) computed from the fit and displayed in a table, as shown below:

Select the Plot evaluated results check box to display the interpolated value:

Save the interpolated population in 1965 to the MATLAB workspace by clicking Save to workspace.
This opens the following dialog box, where you specify the variable names:

After completing a Basic Fitting session, you can generate an M-file that recomputes fits and reproduces plots with new data.
In the Figure window, select File > Generate M-File.
This creates a function M-file and displays it in the MATLAB Editor. The code in the M-file shows you how to programmatically reproduce what you did interactively with the Basic Fitting dialog box.
Change the name of the function on the first line of the M-file from createfigure to something more specific, like censusplot. Save the file to your current directory with the file name censusplot.m
Generate some new, randomly perturbed census data:
randpop = pop + 10*randn(size(pop));
Reproduce the plot with the new data and recompute the fit:
censusplot(cdate,randpop,1965)

![]() | Linear Regression | Programmatic Fitting | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |