Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
初级会员
注册日期: 2008-11-02
年龄: 37
帖子: 2
声望力: 0 ![]() |
![]()
现在学习MATLAB要多看人家的程序,在程序中学习各种函数的使用。在论坛很少看到牛人分享程序给我们学习,本人现在也在学习MATLAB,今天做了个关于插值和拟合问题的
![]() ![]() 程序如下:clf,a=-1;b=1;n=100; g=inline('x^2-x^4');xx=linspace(a,b,n); for i=1:n gx(i)=g(xx(i)); end ymin=min(gx)*0.8; ymax=max(gx)*1.2; subplot(2,2,1), plot(xx,gx,'--'),grid,hold on,axis([a b ymin ymax]);title('近邻插值') subplot(2,2,2), plot(xx,gx,'--'),grid,hold on,axis([a b ymin ymax]);title('线性插值') subplot(2,2,3), plot(xx,gx,'--'),grid,hold on,axis([a b ymin ymax]);title('样条插值') subplot(2,2,4), plot(xx,gx,'--'),grid,hold on,axis([a b ymin ymax]);title('多项式拟合') button=1 x1=[a];y1=[gx(1)]; %or y1=[gx(1)] while button==1 [xi,yi,button]=ginput(1); subplot(2,2,1),h=plot(xi,yi,'ro'); subplot(2,2,2),h=plot(xi,yi,'ro'); subplot(2,2,3),h=plot(xi,yi,'ro'); subplot(2,2,4),h=plot(xi,yi,'ro'); x1=[xi x1];y1=[yi y1]; end x1=[b x1];y1=[gx(n),y1]; xx=linspace(a,b,n); ynearest=interp1(x1,y1,xx,'nearest'); ylinear=interp1(x1,y1,xx,'linear'); yspline=interp1(x1,y1,xx,'spline'); [p,c]=polyfit(x1,y1,4); ypolyfit=polyval(p,xx); subplot(2,2,1),h=plot(xx,ynearest,'r-');set(h,'linewidth',2); subplot(2,2,2),h=plot(xx,ylinear,'r-');set(h,'linewidth',2); subplot(2,2,3),h=plot(xx,yspline,'r-');set(h,'linewidth',2); subplot(2,2,4),h=plot(xx,ypolyfit,'r-');set(h,'linewidth',2); |
![]() |
![]() |
|
|
![]() |
||||
主题 | 主题作者 | 版面 | 回复 | 最后发表 |
[资料]BP网络总结及应用实例 | guofeng0108 | MATLAB论坛 | 37 | 2012-06-11 22:08 |
[求助]如何求多幅图像的平均图像 | snowtv7 | MATLAB论坛 | 2 | 2009-02-26 16:24 |
[求助]判断一元多项式函数的编程问题 | 剑八 | MATLAB论坛 | 0 | 2008-12-10 19:48 |
积分输出结果格式问题 | bangbg | MATLAB论坛 | 1 | 2008-06-18 23:45 |
Simplify Instrument Communication: Using Instrument Drivers with the Instrument Contr | TechnicalArticles | MATLAB技术文章 | 0 | 2008-01-06 16:32 |