收藏 分销(赏)

matlab插值法实例.doc

上传人:s4****5z 文档编号:9436005 上传时间:2025-03-26 格式:DOC 页数:4 大小:135KB
下载 相关 举报
matlab插值法实例.doc_第1页
第1页 / 共4页
matlab插值法实例.doc_第2页
第2页 / 共4页
点击查看更多>>
资源描述
Several Typical Interpolation in Matlab Lagrange Interpolation Supposing: x 144 169 225 y 12 13 15 If x=175, while y=? Solution: Lagrange Interpolation in Matlab: function y=lagrange(x0,y0,x); n=length(x0);m=length(x); for i=1:m z=x(i); s=0.0; for k=1:n p=1.0; for j=1:n if j~=k p=p*(z-x0(j))/(x0(k)-x0(j)); end end s=p*y0(k)+s; end y(i)=s; end input: x0=[144 169 225] y0=[12 13 15] y=lagrange(x0,y0,175) obtain the answer: x0 = 144 169 225 y0 = 12 13 15 y = 13.2302 Spline Interpolation Supposing: x 1 4 9 16 1 2 3 4 Resolve the cubic spline function s(x) Solution: Input x=[ 1 4 9 6];y=[ 1 4 9 6];x=[ 1 4 9 6];pp=spline(x,y) pp = form: 'pp' breaks: [1 4 6 9] coefs: [3x4 double] pieces: 3 order: 4 dim: 1 output : pp.coefs ans = -0.0500 0.5333 -0.8167 1.0000 -0.0500 0.0833 1.0333 2.0000 -0.0500 -0.2167 0.7667 4.0000 It shows the coefficients of cubic spline polynomial , so: S(x)= Newton’s Interpolation Supposing: x 1 4 9 1 2 3 Resolve Solution: Newton’s Interpolation in matlab: function yi=newint(x,y,xi); n=length(x); ny=length(y); if n~=ny error end Y=zeros(n);Y(:,1)=y'; for k=1:n-1 for i=1:n-k if abs(x(i+k)-x(i))<eps error end Y(i,k+1)=(Y(i+1,k)-Y(i,k))/(x(i+k)-x(i)); end end m=length(xi);yi=zeros(1,m); for i=1:n; z=ones(1,m); for k=1:i-1; z=z.*(xi-x(k)); end yi=yi+Y(1,i)*z; end input: x=[1 4 9]; y=[1 2 3]; xi=[5 6]; yi=newint(x,y,xi); operate the program and obtain the answer: yi= 2.2667 2.5000 The difference between several One-dimensional Interpolations like `nearest`、`linear`、`spline`、`cubic` Supposing ,y = sin(x).*exp(-x/5),plot the figure . Solution: program of MATLAB: x = 0:1:4*pi; y = sin(x).*exp(-x/5); xi = 0:0.1:4*pi; y1 = interp1(x, y, xi, 'nearest'); y2 = interp1(x, y, xi, 'linear'); y3 = interp1(x, y, xi, 'spline'); y4 = interp1(x, y, xi, 'cubic'); plot(x, y , 'o', xi ,y1 ,'g-' ,xi ,y2 ,'r:' ,xi ,y3 ,'k-.', xi, y4, 'b--'); legend('Original', 'Nearest', 'Linear', 'Spline', 'Cubic'); operate the program and obtain the figure: (It shows that the smoothness of 'nearest' is the worst and `cubic` is the best.)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服