1、Matlab 图形基础 实验三 Matlab图形基础一、 目的:1、 了解交互式绘图方法;2、 学会能用二维及三维指令绘图;3、 掌握图形的保存和输出;一、 实验原理:1、 准备需要绘制在Matlab图形窗口的数据;2、 创建图形窗口,并且选择绘制数据的区域。一个Matlab图形窗口可以包含多个绘图区域;3、 绘制图形或者曲线;4、 设置曲线的属性,例如线型,线宽;5、 设置绘图区域的属性,并且添加数据网格线;6、 为绘制的图形添加标题、轴标签或者标注文本等;7、 打印或者导出图形。二、 实验仪器及软件: 电脑、Matlab编程软件三、 实验数据:%交互式绘图 randn(state,27)
2、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函数文件:function createfigure(y1, y2)%CREATEFIGURE(Y1,Y2)% Y1: ve
3、ctor of y data% Y2: vector of y data% Auto-generated by MATLAB on 06-May-2013 09:10:35% Create figurefigure1 = figure(PaperPosition,0.6345 6.345 20.3 15.23,PaperSize,20.98 29.68); % Create axesaxes1 = axes(Parent,figure1);xlim(axes1,0 200);xlabel(axes1,Time);ylabel(axes1,Prices);hold(axes1,all); % C
4、reate plotplot1 = plot(y1,. DisplayName,price1,. YDataSource,price1); % Create plotplot2 = plot(y2,. DisplayName,prices2,. YDataSource,prices2); % Create textarrowannotation1 = annotation(. figure1,textarrow,. 0.6071 0.6804,0.2619 0.3571,. String,Price1); % Create textarrowannotation2 = annotation(.
5、 figure1,textarrow,. 0.6411 0.7429,0.6333 0.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(independent variable X); ylabel(independent variable Y);plot(x,y,x,y+1,x,y-1); B=pascal(5) %利用plot函数绘制矩阵数据B = 1 1 1 1 1 1 2 3 4 5 1 3 6 10 15 1 4 10 20
6、 35 1 5 15 35 70 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
7、(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-
8、,x1,y3,rp); title(Curve and Envelope line); xlabel(independent variable X); ylabel(independent variable Y); text(2.8,0.5,Envelope line); text(0.5,0.5,Curve y); text(1.4,0.1,Discrete data point); legend(Envelpoe line,Envelope line,Curve y,discrete data point) %加图列 x=(0:pi/100:2*pi); y1=2*exp(-0.5*x)*
9、1,-1;y2=2*exp(-0.5*x).*sin(2*pi*x); plot(x,y1,b:); axis(0,2*pi,-2,2); hold on; plot(x,y2,k); grid on; %加网格线 box off; %不加坐标边框 hold off; %关闭图形保持tex_examp.m %M文件function tex_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(font
10、name隶书fontsize16隶书fontnameImpactImpact)xlabel(上标and_下标)ylabel(Somebf粗体rm and some it斜体)txt=y=itAealphaxsin(betaitt),. itArm,=,num2str(A),. alpha=,num2str(alpha),. beta=,num2str(beta);text(2,22,txt);tex_exampAnswer.mx=linspace(0,2*pi,60);y=sin(x);z=cos(x);t=sin(x)./(cos(x)+eps);ct=cos(x)./(sin(x)+eps
11、);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.mx=linspace(0,2*pi,60);y=sin(x);z=cos(x);t=sin(x).
12、/(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
13、(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.mx=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);axi
14、s(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);grid on; subplot(2,2,2);semilogx
15、(x,y);title(semilogx(x,y);grid on; subplot(2,2,3);semilogx(x,y);title(semilogx(x,y);grid on; subplot(2,2,4);loglog(x,y);title(loglog(x,y);grid on;Myf.mfunction y=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); compa
16、ss(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(Line in 3-D Space);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 = 8 13
17、 18 23 28ans =22 20 18 16 14 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)
18、.*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
19、); 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); sub
20、plot(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(waterfall of peaks); subplot(1,2,2); contour3(x,y,z,12,k); %其中12代表高度的等级数 xlabel(x-axis),ylabel(y-axis),zl
21、abel(z-axis); title(contour3 of peaks);&:三维图形精细处理 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); v
22、iew(-7,-10); title(azimuth=-7,elevation=-10); z=peaks(20);colormap(copper); subplot(1,3,1);surf(z); subplot(1,3,2);surf(z);shading flat; subplot(1,3,3);surf(z);shading interp; 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
23、:2*pi;x,y=meshgrid(x);z=sin(y).*cos(x); I,J=find(z0.2); for ii=1:length(I) z(I(ii),J(ii)=NaN;end surf(x,y,z);&:图形的保存和输出function h = peakfile% This function was created by saveas(.,mfig), or print -dmfile.% It loads an HG object saved in binary format in the FIG-file of the% same name. NOTE: if you w
24、ant to see the old-style M code% representation of a saved object, previously created by print -dmfile,% you can obtain this by using saveas(.,mmat). But be advised that the% M-file/MAT-file format does not preserve some important information due% to limitations in that format, including Application
25、Data stored using% setappdata. Also, references to handles stored in UserData or Application-% Data will no longer be valid if saved in the M-file/MAT-file format.% ApplicationData and stored handles (excluding integer figure handles)% are both correctly saved in FIG-files.%load the saved objectpath, name = fileparts(which(mfilename);figname = fullfile(path, name .fig);if (exist(figname,file), open(figname), else open(name .fig), endif nargout 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)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1