Matlab 图形基础.docx
《Matlab 图形基础.docx》由会员分享,可在线阅读,更多相关《Matlab 图形基础.docx(15页珍藏版)》请在冰豆网上搜索。
Matlab图形基础
实验三Matlab图形基础
一、目的:
1、了解交互式绘图方法;
2、学会能用二维及三维指令绘图;
3、掌握图形的保存和输出;
一、实验原理:
1、准备需要绘制在Matlab图形窗口的数据;
2、创建图形窗口,并且选择绘制数据的区域。
一个Matlab图形窗口可以包含多个绘图区域;
3、绘制图形或者曲线;
4、设置曲线的属性,例如线型,线宽;
5、设置绘图区域的属性,并且添加数据网格线;
6、为绘制的图形添加标题、轴标签或者标注文本等;
7、打印或者导出图形。
二、实验仪器及软件:
电脑、Matlab编程软件
三、实验数据:
%交互式绘图
>>randn('state',27)>>startprice=40;>>f1=0.0015*randn(200,1)+0.0003;
>>x1=[startprice;1+f1];>>price1=cumprod(x1);>>t=(1:
length(price1))';
>>randn('state',7)>>f2=0.0015*randn(200,1)+0.0003;
>>x2=[startprice;1+f2];>>prices2=cumprod(x2);
Price1变量线图可视化结果:
Prices2变量线图可视化结果:
综合对比:
上图生成的M函数文件:
functioncreatefigure(y1,y2)
%CREATEFIGURE(Y1,Y2)
%Y1:
vectorofydata
%Y2:
vectorofydata
%Auto-generatedbyMATLABon06-May-201309:
10:
35
%%Createfigure
figure1=figure('PaperPosition',[0.63456.34520.315.23],'PaperSize',[20.9829.68]);
%%Createaxes
axes1=axes('Parent',figure1);
xlim(axes1,[0200]);
xlabel(axes1,'Time');
ylabel(axes1,'Prices');
hold(axes1,'all');
%%Createplot
plot1=plot(y1,...
'DisplayName','price1',...
'YDataSource','price1');
%%Createplot
plot2=plot(y2,...
'DisplayName','prices2',...
'YDataSource','prices2');
%%Createtextarrow
annotation1=annotation(...
figure1,'textarrow',...
[0.60710.6804],[0.26190.3571],...
'String',{'Price1'});
%%Createtextarrow
annotation2=annotation(...
figure1,'textarrow',...
[0.64110.7429],[0.63330.5714],...
'String',{'Prices2'});
>>x=0:
pi/100:
2*pi;>>y=exp(-0.5*x).*sin(2*pi*x+pi/4);>>plot(x,y);
>>title('绘制曲线');>>xlabel('independentvariableX');
>>ylabel('independentvariableY');
>>plot(x,y,x,y+1,x,y-1);
>>B=pascal(5)%利用plot函数绘制矩阵数据
B=
11111
12345
1361015
14102035
15153570
>>plot(B);>>title('绘制矩阵数据');
>>x=(0:
pi/100:
2*pi)';>>y1=2*exp(-0.5*x)*[1,-1];
>>y2=2*exp(-0.5*x).*sin(2*pi*x);>>x1=(0:
12)/2;
>>y3=2*exp(-0.5*x1).*sin(2*pi*x1);>>plot(x,y1,'g:
',x,y2,'b--',x1,y3,'rp');
>>x=(0:
pi/100:
2*pi)';>>y1=2*exp(-0.5*x)*[1,-1];
>>y2=2*exp(-0.5*x).*sin(2*pi*x);>>x1=(0:
12)/2;
>>y3=2*exp(-0.5*x1).*sin(2*pi*x1);>>plot(x,y1,'g:
',x,y2,'b--',x1,y3,'rp');
>>x1=0:
pi/100:
2*pi;>>x2=0:
pi/100:
3*pi;>>y1=exp(-0.5*x1).*sin(2*pi*x1);
>>y2=1.5*exp(-0.1*x2).*sin(x2);>>plotyy(x1,y1,x2,y2);
>>x=(0:
pi/100:
2*pi)';>>y1=2*exp(-0.5*x)*[1,-1];
>>y2=2*exp(-0.5*x).*sin(2*pi*x);>>x1=(0:
12)/2;
>>y3=2*exp(-0.5*x1).*sin(2*pi*x1);>>plot(x,y1,'g:
',x,y2,'b--',x1,y3,'rp');
>>title('CurveandEnvelopeline');>>xlabel('independentvariableX');
>>ylabel('independentvariableY');>>text(2.8,0.5,'Envelopeline');
>>text(0.5,0.5,'Curvey');>>text(1.4,0.1,'Discretedatapoint');
>>legend('Envelpoeline','Envelopeline','Curvey','discretedatapoint')%加图列
>>x=(0:
pi/100:
2*pi)';>>y1=2*exp(-0.5*x)*[1,-1];y2=2*exp(-0.5*x).*sin(2*pi*x);
>>plot(x,y1,'b:
');>>axis([0,2*pi,-2,2]);>>holdon;>>plot(x,y2,'k');
>>gridon;%加网格线>>boxoff;%不加坐标边框
>>holdoff;%关闭图形保持
tex_examp.m%M文件
functiontex_examp
%TEX_EXAMP在文本注释中使用特殊文本
alpha=-0.5;beta=3;A=50;t=0:
0.01:
10;y=A*exp(alpha*t).*sin(beta*t);plot(t,y);%添加特殊文本注释
title('\fontname{隶书}\fontsize{16}{隶书}\fontname{Impact}{Impact}')
xlabel('^{上标}and_{下标}')
ylabel('Some\bf粗体\rmandsome\it{斜体}')
txt={'y={\itAe}^{\alphax}sin(\beta\itt)',...
['\itA\rm','=',num2str(A)],...
['\alpha=',num2str(alpha)],...
['\beta=',num2str(beta)]};
text(2,22,txt);
>>tex_examp
Answer.m
x=linspace(0,2*pi,60);
y=sin(x);z=cos(x);
t=sin(x)./(cos(x)+eps);ct=cos(x)./(sin(x)+eps);
subplot(2,2,1);
plot(x,y);title('sin(x)');axis([0,2*pi,-1,1]);
subplot(2,2,2);
plot(x,z);title('cos(x)');axis([0,2*pi,-1,1]);
subplot(2,2,3);
plot(x,t);title('tangent(x)');axis([0,2*pi,-40,40]);
subplot(2,2,4);
plot(x,ct);title('cotangent(x)');axis([0,2*pi,-40,40]);
Question.m
x=linspace(0,2*pi,60);
y=sin(x);z=cos(x);
t=sin(x)./(cos(x)+eps);ct=cos(x)./(sin(x)+eps);
subplot(2,2,1);
stairs(x,y);title('sin(x)-1');axis([0,2*pi,-1,1]);
subplot(2,1,2);
stem(x,y);title('sin(x)-2');axis([0,2*pi,-1,1]);
subplot(4,4,3);
plot(x,y);title('sin(x)');axis([0,2*pi,-1,1]);
subplot(4,4,4);
plot(x,y);title('cos(x)');axis([0,2*pi,-1,1]);
subplot(4,4,7);
plot(x,t);title('tangent(x)');axis([0,2*pi,-40,40]);
subplot(4,4,8);
plot(x,ct);title('cotangent(x)');axis([0,2*pi,-40,40]);
circle.m
x=0:
0.35:
7;
y=2*exp(-0.4*x);
subplot(2,2,1);bar(x,y,'g');
title('bar(x,y,"g")');axis([0,7,0,2]);
subplot(2,2,2);fill(x,y,'r');
title('fill(x,y,"r")');axis([0,7,0,2]);
subplot(2,3,4);stairs(x,y,'b');
title('stairs(x,y,"b")');axis([0,7,0,2]);
subplot(2,3,5);stem(x,y,'k');
title('stem(x,y,"k")');axis([0,7,0,2]);
subplot(2,3,6);area(x,y);
title('area(x,y)');axis([0,7,0,2]);
>>theta=0:
0.01:
2*pi;>>rho=sin(3*theta).*cos(3*theta);%极坐标绘图
>>polar(theta,rho,'r');
>>x=0:
0.1:
10;>>y=12*x.*x;
>>subplot(2,2,1);plot(x,y);title('plot(x,y)');gridon;
>>subplot(2,2,2);semilogx(x,y);title('semilogx(x,y)');gridon;
>>subplot(2,2,3);semilogx(x,y);title('semilogx(x,y)');gridon;
>>subplot(2,2,4);loglog(x,y);title('loglog(x,y)');gridon;
Myf.m
functiony=myf(x)
y=cos(tan(pi*x));
fplot('myf',[-0.2,1.2],1e-4)
>>subplot(1,2,1);>>pie([7,17,23,19,5]);
>>title('饼图');legend('优秀','良好','中等','及格','不及格');
>>subplot(1,2,2);>>compass([3+2i,4.5-i,-1.5+5i]);title('相量图');
>>t=0:
pi/50:
2*pi;>>x=8*cos(t);y=4*sqrt
(2)*sin(t);z=-4*sqrt
(2)*sin(t);
>>plot3(x,y,z,'p');>>title('Linein3-DSpace');text(0,0,0,'origin');
>>xlabel('X'),ylabel('Y');zlabel('Z');grid;
>>x=5:
29;y=14:
35;>>[x,y]=meshgrid(x,y);%在[5:
29]*[14,35]区域生成网格坐标
>>z=2*x+5*y;>>k=find(z==126);>>x(k)',y(k)'
ans=
813182328
ans=
2220181614
>>x=0:
0.1:
2*pi;[x,y]=meshgrid(x);z=sin(y).*cos(x);
>>mesh(x,y,z);xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');title('mesh');
>>x=0:
0.1:
2*pi;[x,y]=meshgrid(x);z=sin(y).*cos(x);
>>surf(x,y,z);xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');title('surf');
>>x=0:
0.1:
2*pi;[x,y]=meshgrid(x);z=sin(y).*cos(x);
>>plot3(x,y,z);xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');title('plot3');grid;
>>[x,y]=meshgrid(-10:
0.5:
10);>>z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps);
>>subplot(2,2,1);>>meshc(x,y,z);>>title('meshc(x,y,z)');
>>subplot(2,2,2);>>meshz(x,y,z);>>title('meshz(x,y,z)');
>>subplot(2,2,3);>>surfc(x,y,z);>>title('surfc(x,y,z)');
>>subplot(2,2,4);>>surfl(x,y,z);>>title('surfl(x,y,z)');
>>t=0:
pi/20:
2*pi;>>[x,y,z]=cylinder(2+sin(t),30);>>subplot(1,3,1);
>>surf(x,y,z);>>subplot(1,3,2);>>[x,y,z]=sphere;>>surf(x,y,z);
>>subplot(1,3,3);>>[x,y,z]=peaks(30);>>meshz(x,y,z);
>>subplot(2,2,1);>>bar3(magic(5));>>subplot(2,2,2);
>>y=3*sin(0:
pi/10:
2*pi);>>stem3(y);>>subplot(2,2,3);
>>pie3([2347,1827,2043,3025]);>>subplot(2,2,4);
>>fill3(rand(3,6),rand(3,6),rand(3,6),'r');
>>subplot(1,2,1);>>[x,y,z]=peaks;>>waterfall(x,y,z);
>>xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
>>title('waterfallofpeaks');>>subplot(1,2,2);
>>contour3(x,y,z,12,'k');%其中12代表高度的等级数
>>xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');>>title('contour3ofpeaks');
&:
三维图形精细处理
>>subplot(2,2,1);mesh(peaks);>>view(-37.5,30);
>>title('azimuth=-37.5,elevation=30');>>subplot(2,2,2);mesh(peaks);
>>view(0,90);>>title('azimuth=0,elevation=90');
>>subplot(2,2,3);mesh(peaks);>>view(90,0);
>>title('azimuth=90,elevation=0');>>subplot(2,2,4);mesh(peaks);
>>view(-7,-10);>>title('azimuth=-7,elevation=-10');
>>z=peaks(20);colormap(copper);>>subplot(1,3,1);surf(z);
>>subplot(1,3,2);surf(z);shadingflat;>>subplot(1,3,3);surf(z);shadinginterp;
>>x=0:
0.1:
2*pi;[x,y]=meshgrid(x);z=sin(y).*cos(x);
>>surf(x,y,z);xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');title('surf');
>>x=0:
0.1:
2*pi;[x,y]=meshgrid(x);z=sin(y).*cos(x);
>>[I,J]=find(z>0.2);>>forii=1:
length(I)z(I(ii),J(ii))=NaN;
end
>>surf(x,y,z);
&:
图形的保存和输出
functionh=peakfile
%Thisfunctionwascreatedbysaveas(...,'mfig'),orprint-dmfile.
%ItloadsanHGobjectsavedinbinaryformatintheFIG-fileofthe
%samename.NOTE:
ifyouwanttoseetheold-styleMcode
%representationofasavedobject,previouslycreatedbyprint-dmfile,
%youcanobtainthisbyusingsaveas(...,'mmat').Butbeadvisedthatthe
%M-file/MAT-fileformatdoesnotpreservesomeimportantinformationdue
%tolimitationsinthatformat,includingApplicationDatastoredusing
%setappdata.Also,referencestohandlesstoredinUserDataorApplication-
%DatawillnolongerbevalidifsavedintheM-file/MAT-fileformat.
%ApplicationDataandstoredhandles(excludingintegerfigurehandles)
%arebothcorrectlysavedinFIG-files.
%
%loadthesavedobject
[path,name]=fileparts(which(mfilename));
figname=fullfile(path,[name'.fig']);
if(exist(figname,'file')),open(figname),elseopen([name'.fig']),end
ifnargout>0,h=gcf;end
>>surf(peaks(30))>>saveas(gcf,'peakfile','m')
>>peakfile>>open('peakfile.fig')
习题:
>>x=0:
1:
100;
>>y=x.*x.*x+x+1;
>>plot(x,y)