polynomial curve fitting -凯发k8网页登录
this example shows how to fit polynomials up to sixth degree to some census data using curve fitting toolbox™. it also shows how to fit a single-term exponential equation and compare this to the polynomial models.
the steps show how to:
load data and create fits using different library models.
search for the best fit by comparing graphical fit results, and by comparing numerical fit results including the fitted coefficients and goodness of fit statistics.
load and plot the data
the data for this example is the file census.mat
.
load census
the workspace contains two new variables:
cdate
is a column vector containing the years 1790 to 1990 in 10-year increments.pop
is a column vector with the u.s. population figures that correspond to the years incdate
.
whos cdate pop
name size bytes class attributes cdate 21x1 168 double pop 21x1 168 double
plot(cdate,pop,'o')
create and plot a quadratic
use the fit
function to fit a polynomial to data. you specify a quadratic, or second-degree polynomial, using 'poly2'
. the first output from fit
is the polynomial, and the second output, gof
, contains the goodness of fit statistics you will examine in a later step.
[population2,gof] = fit(cdate,pop,'poly2');
to plot the fit, use the plot
function. add a legend in the top left corner.
plot(population2,cdate,pop); legend('location','northwest');
create and plot a selection of polynomials
to fit polynomials of different degrees, change the fit type, e.g., for a cubic or third-degree polynomial use 'poly3'
. the scale of the input, cdate
, is quite large, so you can obtain better results by centering and scaling the data. to do this, use the 'normalize'
option.
population3 = fit(cdate,pop,'poly3','normalize','on'); population4 = fit(cdate,pop,'poly4','normalize','on'); population5 = fit(cdate,pop,'poly5','normalize','on'); population6 = fit(cdate,pop,'poly6','normalize','on');
a simple model for population growth tells us that an exponential equation should fit this census data well. to fit a single term exponential model, use 'exp1'
as the fittype.
populationexp = fit(cdate,pop,'exp1');
plot all the fits at once, and add a meaningful legend in the top left corner of the plot.
hold on plot(population3,'b'); plot(population4,'g'); plot(population5,'m'); plot(population6,'b--'); plot(populationexp,'r--'); hold off legend('cdate v pop','poly2','poly3','poly4','poly5','poly6','exp1', ... 'location','northwest');
plot the residuals to evaluate the fit
to plot residuals, specify 'residuals'
as the plot type in the plot
function.
plot(population2,cdate,pop,'residuals');
the fits and residuals for the polynomial equations are all similar, making it difficult to choose the best one.
if the residuals display a systematic pattern, it is a clear sign that the model fits the data poorly.
plot(populationexp,cdate,pop,'residuals');
the fit and residuals for the single-term exponential equation indicate it is a poor fit overall. therefore, it is a poor choice and you can remove the exponential fit from the candidates for best fit.
examine fits beyond the data range
examine the behavior of the fits up to the year 2050. the goal of fitting the census data is to extrapolate the best fit to predict future population values.
by default, the fit is plotted over the range of the data. to plot a fit over a different range, set the x-limits of the axes before plotting the fit. for example, to see values extrapolated from the fit, set the upper x-limit to 2050.
plot(cdate,pop,'o'); xlim([1900, 2050]); hold on plot(population6); hold off
examine the plot. the behavior of the sixth-degree polynomial fit beyond the data range makes it a poor choice for extrapolation and you can reject this fit.
plot prediction intervals
to plot prediction intervals, use 'predobs'
or 'predfun'
as the plot type. for example, to see the prediction bounds for the fifth-degree polynomial for a new observation up to year 2050:
plot(cdate,pop,'o'); xlim([1900,2050]) hold on plot(population5,'predobs'); hold off
plot prediction intervals for the cubic polynomial up to year 2050:
plot(cdate,pop,'o'); xlim([1900,2050]) hold on plot(population3,'predobs') hold off
examine goodness-of-fit statistics
the struct gof
shows the goodness-of-fit statistics for the 'poly2'
fit. when you created the 'poly2'
fit with the fit
function in an earlier step, you specified the gof
output argument.
gof
gof = struct with fields:
sse: 159.0293
rsquare: 0.9987
dfe: 18
adjrsquare: 0.9986
rmse: 2.9724
examine the sum of squares due to error (sse) and the adjusted r-square statistics to help determine the best fit. the sse statistic is the least-squares error of the fit, with a value closer to zero indicating a better fit. the adjusted r-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.
the large sse for 'exp1'
indicates it is a poor fit, which you already determined by examining the fit and residuals. the lowest sse value is associated with 'poly6'
. however, the behavior of this fit beyond the data range makes it a poor choice for extrapolation, so you already rejected this fit by examining the plots with new axis limits.
the next best sse value is associated with the fifth-degree polynomial fit, 'poly5'
, suggesting it might be the best fit. however, the sse and adjusted r-square values for the remaining polynomial fits are all very close to each other. which one should you choose?
compare the coefficients and confidence bounds to determine the best fit
resolve the best fit issue by examining the coefficients and confidence bounds for the remaining fits: the fifth-degree polynomial and the quadratic.
examine population2
and population5
by displaying the models, the fitted coefficients, and the confidence bounds for the fitted coefficients:
population2
population2 = linear model poly2: population2(x) = p1*x^2 p2*x p3 coefficients (with 95% confidence bounds): p1 = 0.006541 (0.006124, 0.006958) p2 = -23.51 (-25.09, -21.93) p3 = 2.113e 04 (1.964e 04, 2.262e 04)
population5
population5 = linear model poly5: population5(x) = p1*x^5 p2*x^4 p3*x^3 p4*x^2 p5*x p6 where x is normalized by mean 1890 and std 62.05 coefficients (with 95% confidence bounds): p1 = 0.5877 (-2.305, 3.48) p2 = 0.7047 (-1.684, 3.094) p3 = -0.9193 (-10.19, 8.356) p4 = 23.47 (17.42, 29.52) p5 = 74.97 (68.37, 81.57) p6 = 62.23 (59.51, 64.95)
you can also get the confidence intervals by using confint
:
ci = confint(population5)
ci = 2×6
-2.3046 -1.6841 -10.1943 17.4213 68.3655 59.5102
3.4801 3.0936 8.3558 29.5199 81.5696 64.9469
the confidence bounds on the coefficients determine their accuracy. check the fit equations (e.g. f(x)=p1*x p2*x...
) to see the model terms for each coefficient. note that p2
refers to the p2*x
term in 'poly2'
and the p2*x^4
term in 'poly5'
. do not compare normalized coefficients directly with non-normalized coefficients.
the bounds cross zero on the p1
, p2
, and p3
coefficients for the fifth-degree polynomial. this means you cannot be sure that these coefficients differ from zero. if the higher order model terms may have coefficients of zero, they are not helping with the fit, which suggests that this model over fits the census data.
the fitted coefficients associated with the constant, linear, and quadratic terms are nearly identical for each normalized polynomial equation. however, as the polynomial degree increases, the coefficient bounds associated with the higher degree terms cross zero, which suggests over fitting.
however, the small confidence bounds do not cross zero on p1
, p2
, and p3
for the quadratic fit, indicating that the fitted coefficients are known fairly accurately.
therefore, after examining both the graphical and numerical fit results, you should select the quadratic population2
as the best fit to extrapolate the census data.
evaluate the best fit at new query points
now you have selected the best fit, population2
, for extrapolating this census data, evaluate the fit for some new query points:
cdatefuture = (2000:10:2020).'; popfuture = population2(cdatefuture)
popfuture = 3×1
274.6221
301.8240
330.3341
to compute 95% confidence bounds on the prediction for the population in the future, use the predint
method:
ci = predint(population2,cdatefuture,0.95,'observation')
ci = 3×2
266.9185 282.3257
293.5673 310.0807
321.3979 339.2702
plot the predicted future population, with confidence intervals, against the fit and data.
plot(cdate,pop,'o'); xlim([1900,2040]) hold on plot(population2) h = errorbar(cdatefuture,popfuture,popfuture-ci(:,1),ci(:,2)-popfuture,'.'); hold off legend('cdate v pop','poly2','prediction', ... 'location','northwest')
for more information, see .