%% Evaluate a Curve Fit
% This example shows how to work with a curve fit.
%% Load Data and Fit a Polynomial Curve

load census
curvefit = fit(cdate,pop,'poly3','normalize','on')

%
% The output displays the fitted model equation, the fitted coefficients, and the confidence bounds for the fitted coefficients.
%% Plot the Fit, Data, Residuals, and Prediction Bounds

plot(curvefit,cdate,pop)

%
% Plot the residuals fit.

plot(curvefit,cdate,pop,'Residuals')

%
% Plot the prediction bounds on the fit.

plot(curvefit,cdate,pop,'predfunc')

%
%% Evaluate the Fit at a Specified Point
% Evaluate the fit at a specific point by specifying a value for x , using this form: y = fittedmodel(x).

curvefit(1991)

%
%% Evaluate the Fit Values at Many Points
% Evaluate the model at a vector of values to extrapolate to the year 2050.

xi = (2000:10:2050).';
curvefit(xi)

%
% Get prediction bounds on those values.

ci = predint(curvefit,xi)

%
% Plot the fit and prediction intervals across the extrapolated fit range. By default, the fit is plotted over the range of the data. To see values extrapolated from the fit, set the upper x-limit of the axes to 2050 before plotting the fit. To plot prediction intervals, use predobs or predfun as the plot type.

plot(cdate,pop,'o')
xlim([1900,2050])
hold on
plot(curvefit,'predobs')
hold off

%
%% Get the Model Equation
% Enter the fit name to display the model equation, the fitted coefficients, and the confidence bounds for the fitted coefficients.

curvefit

%
% To get only the model equation, use formula.

formula(curvefit)

%
%% Get Coefficient Names and Values
% Specify a coefficient by name.

p1 = curvefit.p1
p2 = curvefit.p2

%
% Get all the coefficient names. Look at the fit equation (for example, f(x) = p1*x^3+... ) to see the model terms for each coefficient.

coeffnames(curvefit)

%
% Get all the coefficient values.

coeffvalues(curvefit)

%
%% Get Confidence Bounds on the Coefficients
% Use confidence bounds on coefficients to help you evaluate and compare fits. The confidence bounds on the coefficients determine their accuracy. Bounds that are far apart indicate uncertainty. If the bounds cross zero for linear coefficients, this means you cannot be sure that these coefficients differ from zero. If some model terms have coefficients of zero, then they are not helping with the fit.

confint(curvefit)

%
%% Examine Goodness-of-Fit Statistics
% To get goodness-of-fit statistics at the command line, you can either:
% Open Curve Fitting app and select Fit > Save to Workspace to export your fit and goodness of fit to the workspace.
% Specify the gof output argument using the fit function.
% Recreate the fit specifying the gof and output arguments to get goodness-of-fit statistics and fitting algorithm information.

[curvefit,gof,output] = fit(cdate,pop,'poly3','normalize','on')

%
% Plot a histogram of the residuals to look for a roughly normal distribution.

histogram(output.residuals,10)

%
%% Plot the Fit, Data, and Residuals

plot(curvefit,cdate,pop,'fit','residuals')
legend Location SouthWest
subplot(2,1,1)
legend Location NorthWest

%
%% Find Methods
% List every method that you can use with the fit.

methods(curvefit)

%
% Use the help command to find out how to use a fit method.

help cfit/differentiate

%
% Copyright 2018 The MathWorks, Inc.