切比雪夫多项式拟合matlab程序

标签:
切比雪夫多项式chebyshev拟合 |
分类: matlab |
function y = Chebyshev(y0,k,x0)
%切比雪夫多项式拟合
%written by Haidong Pan(OUC)
%输入参数介绍用k次切比雪夫多项式拟合输入数据(x0,y0)
% Attention: (1) 输入的y0数组中可以有NaN
(2)输入y0必须为列向量
L=length(y0);
gd=find(isfinite(y0(1:L)));
ngood=length(gd);
fprintf(' Points used: %d of
%d\n',ngood,L)
T=zeros(L,k+1);
for i=1:L
for
j=1:k+1
T(i,j)=cos((j-1)*acos(x0(i)));
end
end
coef=regress(y0(gd),T(gd,:));
y=T*coef;
%切比雪夫多项式拟合
%written by Haidong Pan(OUC)
%输入参数介绍用k次切比雪夫多项式拟合输入数据(x0,y0)
%
L=length(y0);
gd=find(isfinite(y0(1:L)));
ngood=length(gd);
fprintf('
T=zeros(L,k+1);
for i=1:L
end
coef=regress(y0(gd),T(gd,:));
y=T*coef;
例子:
x0=0:1:270;y0=sind(x0);x0=x0';y0=y0';
plot(x0,y0,'k')
hold on
y=Chebyshev(y0,1,x0);plot(x0,y,'r')
y=Chebyshev(y0,2,x0);plot(x0,y,'g')
y=Chebyshev(y0,3,x0);plot(x0,y,'b')
y=Chebyshev(y0,4,x0);plot(x0,y,'m')
legend('Given','1阶','2阶','3阶','4阶')
plot(x0,y0,'k')
hold on
y=Chebyshev(y0,1,x0);plot(x0,y,'r')
y=Chebyshev(y0,2,x0);plot(x0,y,'g')
y=Chebyshev(y0,3,x0);plot(x0,y,'b')
y=Chebyshev(y0,4,x0);plot(x0,y,'m')
legend('Given','1阶','2阶','3阶','4阶')