MATLAB与控制系统仿真及实验实验报告(二)2015- 2016 学年第 2 学期专业:班级:学号:姓名:20 年月日实验二 MATLAB的图形绘制一、实验目的1.学习MATLAB图形绘制的基本方法2.熟悉和了解MATLAB图形绘制程序编辑的基本指令3.熟悉掌握利用MATLAB图形编辑窗口编辑和修改图形界面,添加图形的标注4.掌握plot、subplot的指令格式和语法二、实验设备及条件计算机一台(包含MATLAB 软件环境)。
三、实验内容1.生成1×10 维的随机数向量a,分别用红、黄、蓝、绿色绘出其连线图、杆图、阶梯图和条形图,并分别标出标题“连线图”、“杆图”、“阶梯图”、“条形图”。
(1. Generate random vector of dimension 1×10, and use different functions plot, stem, stairs and bars to draw figures with different colors, such as red, yellow, blue and green. Then title the figures with "Plot", "Stem", "Stem", "Bars" respectively.)a=rand(1,10);subplot(2,2,1);plot(a,'r');title('连线图');subplot(2,2,2);stem(a,'y');title('杆图');subplot(2,2,3);stairs(a,'b');title('阶梯图');subplot(2,2,4);bar(a,'g');title('条形图');2. 绘制函数曲线,要求写出程序代码。
(2. Plot the curves and write down the code.)(1) 在区间[0:2π]均匀的取50个点,构成向量tt=linspace(0,2*pi,50)t =Columns 1 through 50 0.1282 0.2565 0.3847 0.5129Columns 6 through 100.6411 0.7694 0.8976 1.0258 1.1541(2) 在同一窗口绘制曲线y1=sin(2*t-0.3); y2=3cos(t+0.5);要求y1曲线为红色实线,标记点为圆圈;y2为蓝色虚线,标记点为加号。
t=linspace(0,2*pi,50);y1=sin(2*t-0.3);y2=3*cos(t+0.5);plot(t,y1,'ro',t,y2,'b--+')(2. Plot the curves and write down the code.(1) Obtain vector t by uniformly selecting 50points in the range [0:2π])(2) In one figure, plot y1=sin(2*t-0.3) andy2=3cos(t+0.5), where y1 is red solid line with circle marker and y2 is blue dashed line with plus sign.)3.在一个图形窗口中以子图形式同时绘制正弦、余弦、正切、余切曲线。
(3. Use function subplot to plot sine, cosine, tangent and arctangent functions in one figure. )t=linspace(0,2*pi,300); subplot(2,2,1);y1=sin(t);plot(t,y1);title('正弦图');axis([0,2*pi,-1,1]); subplot(2,2,2);y2=cos(t);plot(t,y2);title('余弦图');axis([0,2*pi,-1,1]); subplot(2,2,3);y3=y1./y2;y3=plot(t,y3);title('正切图');axis([0,2*pi,-50,50]); subplot(2,2,4);y4=y2./(y1+eps); plot(t,y4)title('余切图');axis([0,2*pi,-50,50]);4.用mesh 或surf 函数,绘制下面方程所表示的三维空间曲面,x 和y 的取值范围设为[-3,3]。
101022y x z +-= (4. Use function mesh and surf to plot three dimensional figures, x and y in range [-3,3].)x=-3:0.1:3; y=x;[X,Y]=meshgrid(x,y); Z=-X.^2/10+Y .^2/10; mesh(Z)5.用imread 命令读入一幅RGB 图像(如:lena.jpg ),分别运用rgb2gray 命令和im2bw 命令将其变换为灰度图像和二值图像,并用imadjust 命令进行图像调整(如imadjust(Img,[0.2 0.8],[]); Img 为读入的图像)在同一个窗口内分成四个子窗口来分别显示RGB 图像、灰度图像,二值图像和调整后的图像,注上文字标题。
a=imread('C:\Users\Administrator\Desktop/lena.jpg') a=imread('C:\Users\Administrator\Desktop/lena.jpg'); i = rgb2gray(a) I = im2bw(a,0.5)c=imadjust(lena,[0.2 0.8],[]);subplot(2,2,1);imshow(a);title('原图像')subplot(2,2,2);imshow(i);title('灰度图像')subplot(2,2,3);imshow(I);title('二值图像')subplot(2,2,4);imshow(c);title('调整后图像')(5. Use function imread to read in an image, and then use rgb2gray, im2bw and imadjust(i.e. imadjust(Img,[0.2 0.8],[]), where Img is the image in workspace) respectively to obtain the processed images. Show the processed images in one figure and title them.)6.综合实验: 阅读以下关于通过绘制二阶系统阶跃响应综合演示图形标识的示例,理解示例中所有图形标识指令的作用,掌握各个图形标识指令的运用方法,并在原指令上改动以实现以下功能:(1)把横坐标范围改为0至5pi,纵坐标范围改为0至2;把程序中axis([-inf,6*pi,0.6,inf])改为axis([0,5*pi,0,2])(2)把图中的α改为σ。
把程序title('\it y = 1 - e^{ -\alpha}改为text(13.5,1.2,'\fontsize{12}{\sigma}=0.3');【附】二阶系统阶跃响应综合演示图形标识的示例代码clf;t=6*pi*(0:100)/100;y=1-exp(-0.3*t).*cos(0.7*t);tt=t(find(abs(y-1)>0.05));ts=max(tt);plot(t,y,'r-','LineWidth',3);axis([-inf,6*pi,0.6,inf]);set(gca,'Xtick',[2*pi,4*pi,6*pi],'Ytick',[0.95,1,1.05,max(y)]);grid on;title('\it y = 1 - e^{ -\alpha}cos{\omegat}');text(13.5,1.2,'\fontsize{12}{\alpha}=0.3');text(13.5,1.1,'\fontsize{12}{\omega}=0.7');hold on;plot(ts,0.95,'bo','MarkerSize',10);hold off;xlabel('\fontsize{14} \bft \rightarrow');ylabel('\fontsize{14} \bfy \rightarrow');(6. Read the following codes and try to figure out what the meaning of the codes, especially the codes for plotting. Change the codes according to the following requirements:(1) Setting scaling for the x-axis in [0,5pi] and y-axis in [0,2] on the current plot;(2) Changing αtoσ)五、心得体会通过本次实验,学习了MATLAB图形绘制的基本方法,熟悉和了解了MATLAB 图形绘制程序编辑的基本指令,熟悉掌握利用MATLAB图形编辑窗口编辑和修改图形界面,添加图形的标注,掌握了plot、subplot的指令格式和语法的使用。
实验中对imread指令的使用存在一些问题,但在help环境下成功了解指令用法。