% 给个例子参考哈
% 实施函数拟合的较完整描述
x = 0:.1:1;
y = [2.1 2.3 2.5 2.9 3.2 3.3 3.8 4.1 4.9 5.4 5.8];
dy = 0.15;
for n = 1:6
[a,S] = polyfit(x,y,n);
A{n} = a;
da = dy*sqrt(diag(inv(S.R'*S.R)));
DA{n} = da';
freedom(n) = S.df;
[ye,delta] = polyval(a,x,S);
YE{n} = ye;
D{n} = delta;
chi2(n) = sum((y-ye).^2)/dy/dy;
end;
Q = 1-chi2cdf(chi2,freedom);
%
subplot(131); plot(1:6,abs(chi2-freedom),'b');
xlabel('阶次'); title('chi 2与自由度');
subplot(132); plot(1:6,Q,'r',1:6,ones(1,6)*0.5);
xlabel('阶次'); title('Q 与 0.5 线');
%
%clf;
subplot(133); plot(x,y,'b+');
axis([0 1 1 6]); hold on;
errorbar(x,YE{3},D{3},'r'); hold off;
title('较适当的三阶拟合');
text(0.1,5.5,['chi2 = ' num2str(chi2(3)) '~' int2str(freedom(3))]);
text(0.1,5.0,['freedom = ',int2str(freedom(3))]);
text(0.6,1.7,['Q = ' num2str(Q(3)) '~0.5']);
A{3},DA{3}
直接求值相减就行了。
自己编个函数,读入点,带进函数,用输出值减不就都得到了!干嘛非用不会的软件