资源描述:
《matlab代码--函数逼近.pdf》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、函数逼近1.Chebyshev用切比雪夫多项式逼近已知函数functionf=Chebyshev(y,k,x0)symst;T(1:k+1)=t;T(1)=1;T(2)=t;c(1:k+1)=0.0;c(1)=int(subs(y,findsym(sym(y)),sym('t'))*T(1)/sqrt(1-t^2),t,-1,1)/pi;c(2)=2*int(subs(y,findsym(sym(y)),sym('t'))*T(2)/sqrt(1-t^2),t,-1,1)/pi;f=c(1)+c(2)*t;fori=3:k+1T(i)=2*
2、t*T(i-1)-T(i-2);c(i)=2*int(subs(y,findsym(sym(y)),sym('t'))*T(i)/sqrt(1-t^2),t,-1,1)/2;f=f+c(i)*T(i);f=vpa(f,6);if(i==k+1)if(nargin==3)f=subs(f,'t',x0);elsef=vpa(f,6);endendend2.Legendre用勒让德多项式逼近已知函数functionf=Legendre(y,k,x0)symst;P(1:k+1)=t;P(1)=1;P(2)=t;c(1:k+1)=0.0;c(1)
3、=int(subs(y,findsym(sym(y)),sym('t'))*P(1),t,-1,1)/2;c(2)=int(subs(y,findsym(sym(y)),sym('t'))*P(2),t,-1,1)/2;f=c(1)+c(2)*t;fori=3:k+1P(i)=((2*i-3)*P(i-1)*t-(i-2)*P(i-2))/(i-1);c(i)=int(subs(y,findsym(sym(y)),t)*P(i),t,-1,1)/2;f=f+c(i)*P(i);if(i==k+1)if(nargin==3)f=subs(f,
4、't',x0);elsef=vpa(f,6);endendend3.Pade用帕德形式的有理分式逼近已知函数functionf=Pade(y,n,x0)symst;A=zeros(n,n);q=zeros(n,1);p=zeros(n+1,1);b=zeros(n,1);yy=0;a(1:2*n)=0.0;for(i=1:2*n)yy=diff(sym(y),findsym(sym(y)),n);a(i)=subs(sym(yy),findsym(sym(yy)),0.0)/factorial(i);end;for(i=1:n)for(j=
5、1:n)A(i,j)=a(i+j-1);end;b(i,1)=-a(n+i);end;q=Ab;p(1)=subs(sym(y),findsym(sym(y)),0.0);for(i=1:n)p(i+1)=a(n)+q(i)*subs(sym(y),findsym(sym(y)),0.0);for(j=2:i-1)p(i+1)=p(i+1)+q(j)*a(i-j);endendf_1=0;f_2=1;for(i=1:n+1)f_1=f_1+p(i)*(t^(i-1));endfor(i=1:n)f_2=f_2+q(i)*(t^i);end
6、if(nargin==3)f=f_1/f_2;f=subs(f,'t',x0);elsef=f_1/f_2;f=vpa(f,6);end4.lmz用列梅兹算法确定函数的最佳一致逼近多项式function[coff,err]=lmz(func,m,a,b,eps)if(nargin==4)eps=1.0e-6;endsymsv;maxv=0.0;max_x=a;%记录abs(f(x)-p(x))取最大值的xfork=0:mpx(k+1)=power(v,k);end%p(x)多项式fori=1:m+2x(i)=0.5*(a+b+(b-a)*c
7、os(3.14159265*(m+2-i)/(m+1)));fx(i)=subs(sym(func),findsym(sym(func)),x(i));end%初始的x和f(x)A=zeros(m+2,m+2);fori=1:m+2forj=1:m+1A(i,j)=power(x(i),j-1);endA(i,m+2)=(-1)^i;endc=Atranspose(fx);%p(x)的初始系数u=c(m+2);%算法中的utol=1;%精度while(tol>eps)t=a;while(t
8、大值的xt=t+0.05*(b-a)/m;px1=subs(px,'v',t);pt=px1*c(1:m+1);ft=subs(sym(func),findsym(sym(fun