![]() |
Matlab数值分析里插值方法的问题
Matlab数值分析里插值方法的问题
比较插值法的运算耗时为什么用TIC,TOC算出来都是0.00的? 这是拉格朗日,牛顿和hermit插值算法编在一起的函数 function jisuan(n) x=-1:0.25:1; y=1./(1+9*x.^2); x0=linspace(min(x),max(x),n); tic p=zeros(size(x0)); for i=1:length(x0) p(i)=lagrint(x,y,x0(i)); end toc tic p2=newtint(x,y,x0); toc tic if nargin<1,n=4; end yp=(1-x).*exp(-x); p3=hermint(x,y,yp,x0); toc 为什么运行出来的时间都是0.000呢? 为什么运行出来的时间都是0.000呢? >> jisuan(100) Elapsed time is 0.000000 seconds. Elapsed time is 0.015000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(200) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(300) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(10) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(20) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.015000 seconds. >> jisuan(30) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.016000 seconds. >> jisuan(40) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(50) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(60) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(70) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(80) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(90) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. >> jisuan(100) Elapsed time is 0.000000 seconds. Elapsed time is 0.000000 seconds. Elapsed time is 0.015000 seconds. 同样是100运行出来还是不一样的 时间?!这是为什么啊 关于lagrange,Newton,Hermit的代码应该没问题的 [COLOR="blue"]这是lagrange的[/COLOR]function yi=lagrint(x,y,xi) %输入:X和Y是插值节点的横坐标和纵坐标构成的向量 %输出:y是拉格朗日插值多项式的系数构成的向量 dxi=xi-x;%xi-x(k)的向量值 n=length(x);%求出X的元素个数 L=zeros(size(y));%产生一个和向量y同阶的零矩阵 L(1)=prod(dxi(2:n))/prod(x(1)-x(2:n));%j=1 ,prod计算向量元素的乘积 L(n)=prod(dxi(1:n-1))/prod(x(n)-x(1:n-1)); %j=n for j=2:n-1 num=prod(dxi(1:j-1))*prod(dxi(j+1:n)); den=prod(x(j)-x(1:j-1))*prod(x(j)-x(j+1:n)); L(j)=num/den; end yi=sum(y.*L); %估计插值:y(j)*L(j)的和 ,j=1,...,n [COLOR="Blue"]NEWTON[/COLOR] function[yhat,dy,cout]=newtint(x,y,xhat)%x,y定义要插值的表格数据向量,参数xhat是在该处要计算差值的标量或向量值 n=length(y); if length(x)~=n,error('x and y are not compatible'); end c=y(:); for j=2:n for i=n:-1:j c(i)=(c(i)-c(i-1))/(x(i)-x(i-j+1));%计算系数c(i)即均差f[x1,x2,...xi] end end yhat=c(n); for i=n-1:-1:1 yhat=yhat.*(xhat-x(i))+c(i);%计算牛顿插值多项式的值,插值结果返回给yhat end if nargout>1 yn2=c(n-1); for i=n-2:-1:1; yn2=yn2.*(xhat-x(i))+c(i); end dy=yhat-yn2;%dy是yhat中n-1阶和n-2阶多项式插值的差 if nargout>2,cout =c;%c是牛顿多项式的系数 end end 谁能帮帮我哦... |
回复: Matlab数值分析里插值方法的问题
分段三阶hermit
function yhat=hermint(x,f,fp,xhat)%分段三阶hermite插值 n=length(x); if length(f)~=n,error('x and f are not compatible'); elseif length(fp)~=n,error('x and fp are not compatible');end x=x(: );xhat=xhat(: );f=f(: );fp=fp(: ); dx=diff(x); divdif=diff(f)./dx; a=f(1:n-1); b=fp(1:n-1); c=(3*divdif-2*fp(1:n-1)-fp(2:n))./dx; d=(fp(1:n-1)-2*divdif+fp(2:n))./dx.^2; i=zeros(size(xhat)); for m=1:length(xhat) i(m)=binSearch(x,xhat(m)); end xx=xhat-x(i); yhat=a(i)+xx.*(b(i)+xx.*(c(i)+xx.*d(i))); |
回复: Matlab数值分析里插值方法的问题
已经解决了,不用回答了...
|
所有时间均为北京时间。现在的时间是 12:45。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.