MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB论坛 (https://www.labfans.com/bbs/forumdisplay.php?f=6)
-   -   [原创]新作的程序 (https://www.labfans.com/bbs/showthread.php?t=5407)

nanhaizi9898 2008-11-19 22:59

[原创]新作的程序
 
现在学习MATLAB要多看人家的程序,在程序中学习各种函数的使用。在论坛很少看到牛人分享程序给我们学习,本人现在也在学习MATLAB,今天做了个关于插值和拟合问题的

[IMG]http://fmn020.xnimg.cn/fmn020/pic001/20081119/22/57/large_Vp4w_3971j198177.jpg[/IMG]
[IMG]http://fmn017.xnimg.cn/fmn017/pic001/20081119/22/57/large_ZfRe_3857p198177.jpg[/IMG]

程序如下: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);

sunspots 2008-12-18 20:18

回复: [原创]新作的程序
 
谢谢你,楼主辛苦了


所有时间均为北京时间。现在的时间是 11:05

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.