|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
<PRE>% General Polynomial Fitting Method
function Ans=polyfit2(x,y,order)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Polynomial Fitting Method
% x,y are two set of data, and order is the
% max degree of fitting polynomial
%
% FangQ
% 2000/11/30
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=4
if nargin==3
n=order;
end
if size(x)~=size(y)
warning('Bad input data');
end
n=n+1;
a=zeros(1,n);
A=zeros(n);
Y=zeros(n,1);
for i=1:n
for j=1:n
A(i,j)=sum(x.^(i+j-2));
end
end
for i=1:n
Y(i,1)=sum(y.*x.^(i-1));
end
Ans=inv(A)*Y;
XI=min(x):min(diff(x))/3:max(x);
Poly=num2str(Ans(1));
for i=2:n
Poly=strcat(Poly,'+(',num2str(Ans(i)),'*x^',num2str(i-1),')');
end
disp(sprintf('Polynomial: \n\t %s',Poly));
FitError=sum((y-polyval(fliplr(Ans'),x)).^2);
disp(sprintf('\nTotal error is %f',FitError));
hold on;
plot(XI,polyval(fliplr(Ans'),XI),'r-',x,y,'o');
</PRE> |
|