www.gusucode.com > curvefit 案例源码程序 matlab代码 > curvefit/FitPolynomialModelsExample.m

    %% Fit Polynomials Using the Fit Function
%
% This example shows how to use the |fit| function to fit polynomials to
% data. The steps fit and plot polynomial curves and a surface, specify
% fit options, return goodness of fit statistics, calculate predictions,
% and show confidence intervals.
%
% The polynomial library model is an input argument to the fit and fittype
% functions. Specify the model type |poly| followed by the degree in x (up to
% 9), or x and y (up to 5). For example, you specify a quadratic curve with
% |'poly2'| , or a cubic surface with |'poly33'| .

% Copyright 2015 The MathWorks, Inc.


%% Create and Plot a Quadratic Polynomial Curve
% Load some data and fit a quadratic polynomial. Specify a quadratic, or
% second-degree polynomial, with the string |'poly2'| .
load census;
fitpoly2=fit(cdate,pop,'poly2')
% Plot the fit with the plot method.
plot(fitpoly2,cdate,pop)
% Move the legend to the top left corner.
legend('Location','NorthWest' );

%% Create a Cubic Curve
% Fit a cubic polynomial |'poly3'|.

fitpoly3=fit(cdate,pop,'poly3')
plot(fitpoly3,cdate,pop)

%% Specify Fit Options
% The cubic fit warns that the equation is badly conditioned, so you
% should try centering and scaling by specifying the |'Normalize'| option.
% Fit the cubic polynomial with both center and scale and robust fitting
% options. Robust |'on'| is a shortcut equivalent to |'Bisquare'| , the
% default method for robust linear least-squares fitting method.

fit3=fit(cdate, pop,'poly3','Normalize','on','Robust','on')
plot(fit3,cdate,pop)
%% 
% To find out what parameters you can set for the library model |'poly3'| ,
% use the fitoptions function.

fitoptions poly3

%% Get Goodness of Fit Statistics
% Specify the |'gof'| output argument to get the goodness-of-fit statistics
% for the cubic polynomial fit.
[fit4, gof]=fit(cdate, pop,'poly3','Normalize','on');
gof

%% Plot the Residuals to Evaluate the Fit
% To plot residuals, specify |'residuals'| as the plot type in the plot
% method.
plot(fit4,cdate, pop,'residuals');

%% Examine a Fit Beyond the Data Range
% 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( fit4 );
hold off

%% Plot Prediction Bounds
% To plot prediction bounds, use |'predobs'| or |'predfun'| as the plot
% type.
plot(fit4,cdate,pop,'predobs')
%% 
% Plot prediction bounds for the cubic polynomial up to year 2050.
plot( cdate, pop, 'o' );
xlim( [1900, 2050] )
hold on
plot( fit4, 'predobs' );
hold off

%% Get Confidence Bounds at New Query Points
% Evaluate the fit for some new query points.
cdateFuture = (2000:10:2020).';
popFuture = fit4( cdateFuture ) 

%% 
% Compute 95% confidence bounds on the prediction for the population in the
% future, using the predint method.
ci = predint( fit4, cdateFuture, 0.95, 'observation' )
%% 
% Plot the predicted future population, with confidence intervals, against
% the fit and data.
plot(cdate, pop, 'o');
xlim([1900, 2040])
hold on
plot(fit4)
h = errorbar(cdateFuture,popFuture,popFuture-ci(:,1),ci(:,2)-popFuture,'.');
hold off
legend('cdate v pop','poly3','prediction','Location','NorthWest')

%% Fit and Plot a Polynomial Surface
% Load some surface data and fit a fourth-degree polynomial in x and y.
load franke;
fitsurface=fit([x,y],z, 'poly44','Normalize','on')
plot(fitsurface, [x,y],z)