matlab拟合一组数据到某一函数

x=[120 140 150 165 180 195 210 230 250 300];
y=[0.000025 0.000021 0.000020 0.000016 0.000010 0.000010 0.000010 0.0000095 0.0000095 0.0000093];
拟合到y=a/x+b
给出较为具体的程序或者提示

代码如下:
function fffff(x,y)
%FFFFF Create plot of datasets and fits
% FFFFF(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 13-Apr-2009 22:48:43

% Set up figure to receive datasets and fits
f_ = clf;
figure(f_);
set(f_,'Units','Pixels','Position',[160 92 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',[118.2, 301.80000000000001]);
end

% --- Create fit "fit 1"
ok_ = isfinite(x) & isfinite(y);
if ~all( ok_ )
warning( 'GenerateMFile:IgnoringNansAndInfs', ...
'Ignoring NaNs and Infs in data' );
end
st_ = [0.60081813045508603 0.69692322088040315 ];
ft_ = fittype('(a/x)+b',...
'dependent',{'y'},'independent',{'x'},...
'coefficients',{'a', 'b'});

% Fit this model using new data
cf_ = fit(x(ok_),y(ok_),ft_,'Startpoint',st_);

% Or use coefficients from the original fit:
if 0
cv_ = { 0.0036116876144370869, -5.953373280243035e-006};
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

运行fffff(x,y)得:
General model:
f(x) = (a/x)+b
Coefficients (with 95% confidence bounds):
a = 0.003612 (0.002468, 0.004756)
b = -5.953e-006 (-1.25e-005, 5.945e-007)

Goodness of fit:
SSE: 4.205e-011
R-square: 0.8688
Adjusted R-square: 0.8524
RMSE: 2.293e-006

如果你数据误差小一点,数据多一点,拟合效果会好些
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-04-13
y与1/x是一次线性关系。你和代码如下:
x=[120 140 150 165 180 195 210 230 250 300];
y=[0.000025 0.000021 0.000020 0.000016 0.000010 0.000010 0.000010 0.0000095 0.0000095 0.0000093];
x1=1./x;
[p,s]=polyfit(x1,y,1);
format long
p,s
给出结果:
p =

0.003611990072656 -0.000005955245122 分别是a,b的值

s =

R: [2x2 double]
df: 8
normr: 6.484448854082536e-006 拟合的残差标准值,说明拟合的好坏
第2个回答  2020-02-20
可以使用
polyfit2d,
polyval2d这两个函数来解决
使用方法参考polyfit2dtest
到网上搜索这三个函数名,源文件就可以下载到了
相似回答