MATLAB-插值算法实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1.高斯插值functionf=Gauss(x,y,x0)if(length(x)==length(y))n=length(x);elsedisp('x和y的维数不相等!');return;endxx=linspace(x(1),x(n),(x(2)-x(1)));if(xx~=x)disp('节点之间不是等距的!');return;endif(mod(n,2)==1)if(nargin==2)f=GStirling(x,y,n);elseif(nargin==3)f=GStirling(x,y,n,x0);endendelseif(nargin==2)f=GBessel(x,y,n);elseif(nargin==3)f=GBessel(x,y,n,x0);endendendfunctionf=GStirling(x,y,n,x0)symst;nn=(n+1)/2;f=y(nn);for(i=1:n-1)for(j=i+1:n)y1(j)=y(j)-y(j-1);endif(mod(i,2)==1)c(i)=(y1((i+n)/2)+y1((i+n+2)/2))/2;elsec(i)=y1((i+n+1)/2)/2;endif(mod(i,2)==1)l=t+(i-1)/2;for(k=1:i-1)l=l*(t+(i-1)/2-k);endelsel_1=t+i/2-1;l_2=t+i/2;for(k=1:i-1)l_1=l_1*(t+i/2-1-k);l_2=l_2*(t+i/2-k);endl=l_1+l_2;endl=l/factorial(i);f=f+c(i)*l;simplify(f);f=vpa(f,6);y=y1;if(i==n-1)if(nargin==4)f=subs(f,'t',(x0-x(nn))/(x(2)-x(1)));endendendfunctionf=GBessel(x,y,n,x0)symst;nn=n/2;f=(y(nn)+y(nn+1))/2;for(i=1:n-1)for(j=i+1:n)y1(j)=y(j)-y(j-1);endif(mod(i,2)==1)c(i)=y1((i+n+1)/2)/2;elsec(i)=(y1((i+n)/2)+y1((i+n+2)/2))/2;endif(mod(i,2)==0)l=t+i/2-1;for(k=1:i-1)l=l*(t+i/2-1-k);endelsel_1=t+(i-1)/2;l_2=t+(i-1)/2-1;for(k=1:i-1)l_1=l_1*(t+(i-1)/2-k);l_2=l_2*(t+(i-1)/2-1-k);endl=l_1+l_2;endl=l/factorial(i);f=f+c(i)*l;simplify(f);f=vpa(f,6);y=y1;if(i==n-1)if(nargin==4)f=subs(f,'t',(x0-x(nn))/(x(2)-x(1)));endendend2.Hermite(艾尔米特)插值functionf=Hermite(x,y,y_1,x0)symst;f=0.0;if(length(x)==length(y))if(length(y)==length(y_1))n=length(x);elsedisp('y和y的导数的维数不相等!');return;endelsedisp('x和y的维数不相等!');return;endfori=1:nh=1.0;a=0.0;forj=1:nif(j~=i)h=h*(t-x(j))^2/((x(i)-x(j))^2);a=a+1/(x(i)-x(j));endendf=f+h*((x(i)-t)*(2*a*y(i)-y_1(i))+y(i));if(i==n)if(nargin==4)f=subs(f,'t',x0);elsef=vpa(f,6);endendend3.拉格朗日插值functionf=Language(x,y,x0)symst;if(length(x)==length(y))n=length(x);elsedisp('x和y的维数不相等!');return;end%检错f=0.0;for(i=1:n)l=y(i);for(j=1:i-1)l=l*(t-x(j))/(x(i)-x(j));end;for(j=i+1:n)l=l*(t-x(j))/(x(i)-x(j));%计算拉格朗日基函数end;f=f+l;%计算拉格朗日插值函数simplify(f);%化简if(i==n)if(nargin==3)f=subs(f,'t',x0);%计算插值点的函数值elsef=collect(f);%将插值多项式展开f=vpa(f,6);%将插值多项式的系数化成6位精度的小数endendend4.利用均差牛顿插值functionf=Newton(x,y,x0)symst;if(length(x)==length(y))n=length(x);c(1:n)=0.0;elsedisp('x和y的维数不相等!');return;endf=y(1);y1=0;l=1;for(i=1:n-1)for(j=i+1:n)y1(j)=(y(j)-y(i))/(x(j)-x(i));endc(i)=y1(i+1);l=l*(t-x(i));f=f+c(i)*l;simplify(f);y=y1;if(i==n-1)if(nargin==3)f=subs(f,'t',x0);elsef=collect(f);%将插值多项式展开f=vpa(f,6);endendend5.向后牛顿插值functionf=Newtonback(x,y,x0)symst;if(length(x)==length(y))n=length(x);c(1:n)=0.0;elsedisp('x和y的维数不相等!');return;endf=y(n);y1=0;xx=linspace(x(1),x(n),(x(2)-x(1)));if(xx~=x)disp('节点之间不是等距的!');return;endfor(i=1:n-1)for(j=i+1:n)y1(j)=y(j)-y(j-1);endc(i)=y1(n);l=t;for(k=1:i-1)l=l*(t+k);end;f=f+c(i)*l/factorial(i);simplify(f);y=y1;if(i==n-1)if(nargin==3)f=subs(f,'t',(x0-x(n))/(x(2)-x(1)));elsef=collect(f);f=vpa(f,6);endendend5.向前牛顿插值functionf=Newtonforward(x,y,x0)symst;if(length(x)==length(y))n=length(x);c(1:n)=0.0;elsedisp('x和y的维数不相等!');return;endf=y(1);y1=0;xx=linspace(x(1),x(n),(x(2)-x(1)));if(xx~=x)disp('节点之间不是等距的!');return;endfor(i=1:n-1)for(j=1:n-i)y1(j)=y(j+1)-y(j);endc(i)=y1(1);l=t;for(k=1:i-1)l=l*(t-k);end;f=f+c(i)*l/factorial(i);simplify(f);y=y1;if(i==n-1)if(nargin==3)f=subs(f,'t',(x0-x(1))/(x(2)-x(1)));elsef=collect(f);f=vpa(f,6);endendend

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功