Interactive Fitting

The Basic Fitting GUI

The MATLAB® Basic Fitting GUI allows you to interactively:

Preparing for Basic Fitting

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);

Opening the Basic Fitting GUI

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.

figure showing the basic fitting interface

The GUI consists of three panels:

To expand or collapse the panels, use the arrow button in the lower right corner of the interface.

Example: Using Basic Fitting GUI

The example in this section shows you how to use the Basic Fitting GUI.

Loading and Plotting Data

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:

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.

Fitting Data

  1. Open the Basic Fitting dialog box by selecting Tools > Basic Fitting in the Figure window.

    sub menu showing basic fitting as a choice

  2. 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
  3. Select the following options:

The resulting display is shown in the following figure:

resulting fit and the residuals

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.

Viewing and Saving Fit Parameters

In the Basic Fitting dialog box, click the arrow button right arrow to display the estimated coefficients and the norm of the residuals in the Numerical results panel.

fit coefficients and the norm of the residuals

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.

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:

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.

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.

Interpolating and Extrapolating Values

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.

  1. In the Enter value(s)... field, type the following value:

    1965
    

  2. Click Evaluate.

    The x values and the corresponding values for f(x) computed from the fit and displayed in a table, as shown below:

    The x-value and the corresponding f(x) value evaluated from the fit

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

    The interpolated value shown with the current in the data plot

  4. 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:

    save results to work space dialog box

Generating an M-file

After completing a Basic Fitting session, you can generate an M-file that recomputes fits and reproduces plots with new data.

  1. 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.

  2. 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

  3. Generate some new, randomly perturbed census data:

    randpop = pop + 10*randn(size(pop));
  4. Reproduce the plot with the new data and recompute the fit:

    censusplot(cdate,randpop,1965)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS