实验二 数据和函数的可视化
实验目的:
1. 熟悉matlab软件中二维绘图和三维绘图的方法 2. 学习使用matlab图形窗功能
实验内容: 基本命令
>> x=linspace(0,2*pi,100); >> y=sin(x); >> plot(x,y);
问题1:
1.当运行x=[1 5 3 7;3 6 8 4;9 6 1 5];y=[2 5 7 4;6 8 4 1;8 0 4 2];plot(x,y)时的结果:
2.当运行x=[1 5 3 7];y=[2 5 7 4;6 8 4 1;8 0 4 2];plot(x,y)时显示:
3. 运行x=[1 5 3 7;3 6 8 4;9 6 1 5];y=[2 5 7 4;6 8 4 1;8 0 4 2];m=cat(2,x,y);plot(m);显示:
例2:t=0:pi/20:pi; plot(t,t.*cos(t),'-.r*'); hold on
plot(exp(t/100).*sin(t-pi/2),'--m0')
plot(sin(t*pi),':bs') %误把mo写成了m0 ^o^,导致如下结果:
??? Error using ==> plot
Error in color/linetype argument.
Error in ==> C:\\MATLAB6p5\\work\\Untitled2.m On line 4 ==> plot(exp(t/100).*sin(t-pi/2),'--m0')
正确输入: t=0:pi/20:pi;
plot(t,t.*cos(t),'-.r*'); hold on
plot(exp(t/100).*sin(t-pi/2),'--mo') plot(sin(t*pi),':bs')
如果没有hold on: t=0:pi/20:pi;
plot(t,t.*cos(t),'-.r*');
plot(exp(t/100).*sin(t-pi/2),'--mo') plot(sin(t*pi),':bs')
所以hold on 起到 继续在当前图形上画图的作用
图形修饰与控制
axis square %将图形设置为正方形 axis equal %x,y轴单位刻度相等 title (‘字符串’)%图形标题、
axis([xmin,xmax,ymin,ymax])%x轴范围在xmin-xmax之间,y轴范围在ymin-ymax之间 xlabel(‘字符串’)%x轴标注
ylabel(‘字符串’)%在(x,y)处标注说明文字 text(x,y,’字符串’)%在(x,y)处标注说明文字 grid on % 加网格线 grid off % 取消网格线 hold on %保持当前图形 hold off %解除hold on 命令
legend(‘first’,’second’,n) % 对一个坐标系上的两幅图形做出图例注解 subplot(m,n,p)将当前窗口分成m行n列区域,并指定在p区绘图
fill(x,y,’b’)%将x(1),y(1),(x(2),y(2)),…,(x(n),y(n)),(x(1),y(1))围成的封闭图形填充为蓝色
例三:subplot(2,2,1);x=0:pi/60:2*pi;plot(x,cos(x)); subplot(2,2,2);x=0:pi/60:2*pi;plot(x,sin(x)); subplot(2,1,2);plot(x,sin(x),':b',x,cos(x),'-r') legend('sin(x)','cos(x)',1)