使用多项式拟合函数polyfit(x,y,n),其中x是你要你和的自变量,y是你要拟合的因变量,n是你要用到的拟合多项式的最高次数,函数返回这个多项式。
具体你的问题:
x=[1,2,3,4,5,6,7,8,9,10,11,12]
y=[ 96.31, 135.44, 79.5, 56.54, 256.21, 350.68, 105.62, 185.03, 493.08, 1031.17, 860.06, 746.78]
z=polyfit(x,y,4)
ans =
-0.9026 22.3624 -171.6814 489.5975 -287.8153
y比x多出了两位,我删除了后两位;
代码如下:
function poly4(x,y)
%POLY4 Create plot of datasets and fits
% POLY4(X,Y)
% Creates a plot, similar to the plot in the main curve fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with cftool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
% Data from dataset "y vs. x":
% X = x:
% Y = y:
% Unweighted
%
% This function was automatically generated on 27-Mar-2009 21:43:52
% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[326 106 680 484]);
legh_ = []; legt_ = {}; % handles and text for legend
xlim_ = [Inf -Inf]; % limits of x axis
ax_ = axes;
set(ax_,'Units','normalized','OuterPosition',[0 0 1 1]);
set(ax_,'Box','on');
axes(ax_); hold on;
% --- Plot data originally in dataset "y vs. x"
x = x(:);
y = y(:);
h_ = line(x,y,'Parent',ax_,'Color',[0.333333 0 0.666667],...
'LineStyle','none', 'LineWidth',1,...
'Marker','.', 'MarkerSize',12);
xlim_(1) = min(xlim_(1),min(x));
xlim_(2) = max(xlim_(2),max(x));
legh_(end+1) = h_;
legt_{end+1} = 'y vs. x';
% Nudge axis limits beyond data limits
if all(isfinite(xlim_))
xlim_ = xlim_ + [-1 1] * 0.01 * diff(xlim_);
set(ax_,'XLim',xlim_)
else
set(ax_, 'XLim',[0.91000000000000003, 10.09]);
end
% --- Create fit "fit 1"
ok_ = isfinite(x) & isfinite(y);
if ~all( ok_ )
warning( 'GenerateMFile:IgnoringNansAndInfs', ...
'Ignoring NaNs and Infs in data' );
end
ft_ = fittype('poly4');
% Fit this model using new data
cf_ = fit(x(ok_),y(ok_),ft_);
% Or use coefficients from the original fit:
if 0
cv_ = { 1.9494012237762368, -37.46559731934768, 242.74933129370976, -576.15945804197111, 496.91083333334984};
cf_ = cfit(ft_,cv_{:});
end
% Plot this fit
h_ = plot(cf_,'fit',0.95);
legend off; % turn off legend from plot method call
set(h_(1),'Color',[1 0 0],...
'LineStyle','-', 'LineWidth',2,...
'Marker','none', 'MarkerSize',6);
legh_(end+1) = h_(1);
legt_{end+1} = 'fit 1';
% Done plotting data and fits. Now finish up loose ends.
hold off;
leginfo_ = {'Orientation', 'vertical', 'Location', 'NorthEast'};
h_ = legend(ax_,legh_,legt_,leginfo_{:}); % create legend
set(h_,'Interpreter','none');
xlabel(ax_,''); % remove x label
ylabel(ax_,''); % remove y label
结果:
Linear model Poly4:
f(x) = p1*x^4 + p2*x^3 + p3*x^2 + p4*x + p5
Coefficients (with 95% confidence bounds):
p1 = 1.949 (-0.1406, 4.039)
p2 = -37.47 (-83.7, 8.768)
p3 = 242.7 (-103.2, 588.7)
p4 = -576.2 (-1575, 422.8)
p5 = 496.9 (-389.4, 1383)
Goodness of fit:
SSE: 5.445e+004
R-square: 0.9319
Adjusted R-square: 0.8774
RMSE: 104.4
请使用多项式拟合函数polyfit(x,y,n),其中x是你要你和的自变量,y是你要拟合的因变量,n是你要用到的拟合多项式的最高次数,函数返回这个多项式。
详见MATLAB帮助文档:polyfit