版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、,第二讲:可视化和编程,MATLAB 基础,孙洪全,Homework 1 Recap, Use of semicolon(分号) 如果一行中只包括一个命令,那么通常不需要使用分号. 如果需要在一行中包括多个命令,那么需使用分号来分隔 多个命令: x=1:10; y=(x-5).2; plot(x,y);,Outline,(1) (2) (3) (4) (5),Functions Flow Control Line Plots Image/Surface Plots Vectorization,User-defined Functions 函数与脚本非常类似, but for ONE diffe
2、rence 函数必须有function declaration(函数声明)! Help file Function declaration,Outputs,Inputs Courtesy of The MathWorks, Inc. Used with permission.,If more than one output,User-defined Functions, 关于 function declaration的几点说明:, No need for return: MATLAB returns 声明中出现的输出变量, Variable scope: 任何函数内创建的变量仅限于函数内使用
3、它们不会被返回,函数运行结束后即消失。,function x, y, z = funName(in1, in2),Must have the reserved Function name should word: function match MATLAB file,name,must be in brackets,Inputs must be specified,Functions: overloading(重载), Were familiar with, zeros size, length sum, Look at the help file for size by typing, he
4、lp size, 帮助文件中描述了调用函数size的多种方法,D = SIZE(X),M,N = SIZE(X),M1,M2,M3,.,MN = SIZE(X) M = SIZE(X,DIM),Functions: overloading, MATLAB functions are generally overloaded,可以具有可变数量的输入参数 可以返回可变数量的输出数据, 下列命令分别返回什么结果:, a=zeros(2,4,8); %n-dimensional matrices are OK D=size(a), m,n=size(a), x,y,z=size(a) m2=size(
5、a,2), You can overload your own functions by having variable,input and output arguments (see varargin, nargin, varargout, nargout),0,1,2,3,4,5,6,7,-1,Functions: Exercise Write a function with the following declaration: function plotSin(f1) In the function, plot a sin wave with frequency f1, on the r
6、ange 0, 2: sin ( f1 x ) To get good sampling, use 16 points per period. 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8,Functions: Exercise, Write a function with the following declaration:,function plotSin(f1), In the function, plot a sin wave with frequency f1, on the range 0, 2: sin ( f1 x ), To get good
7、 sampling, use 16 points per period., In an MATLAB file saved as plotSin.m, write the following:, function plotSin(f1),x=linspace(0,2*pi,f1*16); figure,plot(x,sin(f1*x),Outline,(1) (2) (3) (4) (5),Functions Flow Control Line Plots Image/Surface Plots Vectorization,Relational Operators(关系运算符) MATLAB
8、uses mostly standard relational operators,equal not equal greater than less than greater or equal less or equal,= = = =,short-circuit (scalars) figure,if nargin = 1,plot(x,sin(f1*x); elseif nargin = 2,disp(Two inputs were given);,end, If the number of input arguments is 1, execute the plot command y
9、ou wrote before. Otherwise, display the line Two inputs were given, Hint: the number of input arguments are in the built-in variable,nargin,Outline,(1) (2) (3) (4) (5),Functions Flow Control Line Plots Image/Surface Plots Vectorization,Plot Options Can change the line color, marker style, and line s
10、tyle by adding a string argument plot(x,y,k.-);, Can plot without connecting the dots by omitting line style argument plot(x,y,.) Look at help plot for a full list of colors, markers, and linestyles,color,marker,line-style,Playing with the Plot,to select lines and delete or change properties,to zoom
11、 in/out,to slide the plot around,to see all plot tools at once,Courtesy of The MathWorks, Inc. Used with permission.,-3,-2,-1,0,1,2,3,4,-0.8 -4,0.2 0 See doc line_props for a full list of -0.2 properties that can be specified -0.4 -0.6,0.8 0.6 0.4,Line and Marker Options Everything on a line can be
12、customized plot(x,y,-s,LineWidth,2,. Color, 1 0 0, . MarkerEdgeColor,k,. MarkerFaceColor,g,. MarkerSize,10) You can set colors by using,a vector of R G B values or a predefined color character like g, k, etc.,10,10,10,10,10,Cartesian Plots We have already seen the plot function x=-pi:pi/100:pi; y=co
13、s(4*x).*sin(10*x).*exp(-abs(x); plot(x,y,k-); The same syntax applies for semilog and loglog plots, semilogx(x,y,k); semilogy(y,r.-);, For example: x=0:100; semilogy(x,exp(x),k.-);,0,10,20,30,40,50,60,70,80,90,100,0,20 10, loglog(x,y); 30,50 40,10,3D Line Plots We can plot in 3 dimensions just as ea
14、sily as in 2 time=0:0.001:4*pi; x=sin(time); y=cos(time); z=time; plot3(x,y,z,k,LineWidth,2); zlabel(Time);, Use tools on figure to rotate it Can set limits on all 3 axes xlim, ylim, zlim,Axis Modes, Built-in axis modes, axis square,makes the current axis look like a box, axis tight fits axes to dat
15、a axis equal,makes x and y scales the same, axis xy,puts the origin in the bottom left corner (default for plots), axis ij,puts the origin in the top left corner (default for matrices/images),Multiple Plots in one Figure, 在同一个figure中绘制多个坐标系, subplot(2,3,1),makes a figure with 2 rows and three column
16、s of axes, and activates the first axis for plotting,each axis can have labels, a legend, and a title, subplot(2,3,4:6),activating a range of axes fuses them into one, To close existing figures, close(1 3),closes figures 1 and 3, close all,closes all figures (useful in scripts/functions),Copy/Paste
17、Figures Figures can be pasted into other apps (word, ppt, etc), Edit,copy options,figure copy template,Change font sizes, line properties; presets for word and ppt, Edit,copy figure to copy figure, Paste into document of interest Courtesy of The MathWorks, Inc. Used with permission.,Saving Figures,
18、Figures can be saved in many formats. The common ones,are:,.fig preserves all information,.bmp uncompressed image,.eps high-quality scaleable format,.pdf compressed image,Courtesy of The MathWorks, Inc. Used with permission.,Advanced Plotting: Exercise Modify the plot command in your plotSin functio
19、n to use squares as markers and a dashed red line of thickness 2 as the line. Set the marker face color to be black (properties are LineWidth, MarkerFaceColor) If there are 2 inputs, open a new figure with 2 axes, one on top of the other (not side by side), and activate the top one (subplot),0,1,2,3
20、,4,5,6,7,-1,0 -0.2 -0.4 -0.6 -0.8,0.2,1 0.8,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0,0.6 0.6 0.4 0.4 0.2,1 0.8,plotSin(6),plotSin(1,2),Advanced Plotting: Exercise, Modify the plot command in your plotSin function to use squares as markers and a dashed red line of thickness 2 as the line. Set the ma
21、rker face color to be black (properties are LineWidth, MarkerFaceColor), If there are 2 inputs, open a new figure with 2 axes, one on top of the other (not side by side), and activate the top one (subplot), if nargin = 1,plot(x,sin(f1*x),rs-,.,LineWidth,2,MarkerFaceColor,k);,elseif nargin = 2,subplo
22、t(2,1,1);,end,Outline,(1) (2) (3) (4) (5),Functions Flow Control Line Plots Image/Surface Plots Vectorization,Visualizing matrices, Any matrix can be visualized as an image, mat=reshape(1:10000,100,100); imagesc(mat); colorbar, imagesc automatically scales the values to span the entire,colormap, Can
23、 set limits for the color axis (analogous to xlim, ylim), caxis(3000 7000),Colormaps, You can change the colormap:, imagesc(mat) default map is jet, colormap(gray) colormap(cool), colormap(hot(256), See help hot for a list, Can define custom colormap, map=zeros(256,3);, map(:,2)=(0:255)/255; colorma
24、p(map);,Surface Plots It is more common to visualize surfaces in 3D, Example:, How can we make these matrices,通过循环来生成 内建函数: meshgrid,2,4,6,8,10,12,14,16,18,20,2,4 6 8 10 12 14 16,18,20,-3,2 1 0 -1 -2,3,2,4,6,8,10,12,14,16,18,20,2 4 6 8,10 12 14 16 18 20,0 -1 -2 -3,2 1, surf puts vertices at specifie
25、d points in space x,y,z, and connects all the vertices to make a surface The vertices can be denoted by matrices X,Y,Z 3,surf, Make the x and y vectors, x=-pi:0.1:pi; y=-pi:0.1:pi;, Use meshgrid to make matrices (this is the same as loop), X,Y=meshgrid(x,y);, To get function values,evaluate the matr
26、ices Z =sin(X).*cos(Y);, Plot the surface, surf(X,Y,Z) surf(x,y,Z);,surf Options, See help surf for more options, There are three types of surface shading, shading faceted shading flat, shading interp, You can change colormaps, colormap(gray),contour, You can make surfaces two-dimensional by using c
27、ontour, contour(X,Y,Z,LineWidth,2),takes same arguments as surf color indicates height,can modify linestyle properties can set colormap, hold on, mesh(X,Y,Z),Exercise: 3-D Plots, Modify plotSin to do the following:, If two inputs are given, evaluate the following function:, y should be just like x,
28、but using f2. (use meshgrid to get,the X and Y matrices), In the top axis of your subplot, display an image of the Z matrix. Display the colorbar and use a hot colormap. Set the axis to xy (imagesc, colormap, colorbar, axis), In the bottom axis of the subplot, plot the 3-D surface of Z,(surf),Z = si
29、n ( f1 x ) + sin ( f2 y ),Exercise: 3-D Plots, function plotSin(f1,f2),x=linspace(0,2*pi,round(16*f1)+1); figure,if nargin = 1,plot(x,sin(f1*x),rs-,.,LineWidth,2,MarkerFaceColor,k);,elseif nargin = 2,y=linspace(0,2*pi,round(16*f2)+1); X,Y=meshgrid(x,y); Z=sin(f1*X)+sin(f2*Y);,subplot(2,1,1); imagesc
30、(x,y,Z); colorbar; axis xy; colormap hot,subplot(2,1,2); surf(X,Y,Z);,end,Exercise: 3-D Plots plotSin(3,4) generates this figure,0,1,2,3,4,5,6,0,6 5 4 3,-2,2 -1 1,2 1 0,0,1,2,3,4,5,6,7,0,2,4,6,2 0 -2 8,Specialized Plotting Functions, MATLAB has a lot of specialized plotting functions polar-to make p
31、olar plots, polar(0:0.01:2*pi,cos(0:0.01:2*pi)*2), bar-to make bar graphs bar(1:10,rand(1,10);, quiver-to add velocity vectors to a plot, X,Y=meshgrid(1:10,1:10);, quiver(X,Y,rand(10),rand(10);, stairs-plot piecewise constant functions, stairs(1:10,rand(1,10);, fill-draws and fills a polygon with specified vertices, fill(0 1 0.5,0 0 1,r);, see help on these functions for syntax doc specgraph for a complete list,Outline,(1) (2) (3) (4) (5),Functions Flow Control Line Plots Image/Surface Plots Vectorization,Revisit
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年心理咨询师考试模拟试题集
- 小学一年级道德与法治教学设计:开开心心上学去-基于核心素养的入学适应性教育实践
- 2026年环境工程污染控制技术与法规冲刺押题卷
- 高二化学选择性必修三《有机化合物的结构与性质》第二课时教学设计
- 初三物理《中考选填题型专项复习:方法建模与思维进阶》教学设计
- 高中三年级英语读后续写一轮复习专项教学设计
- 小学三年级道德与法治《少让父母操心》第二课时教学设计
- 2026小学教资面试英语高频考题题库解析
- 高中教资面试地理历年真题试卷及解析202
- 人事岗事业单位面试高频考题题库及解析
- 2026年人工智能(AI)训练师专业知识考试题库附答案(完整版)
- T/CPCA 022-2026铝基刚性印制电路板通用规范
- 2026年公务员考试申论作文专项训练押题卷
- 高中二年级生物神经调节的分层教学设计
- 车间反应釜超压爆炸应急预案演练脚本
- 2026四川省自然资源投资集团招聘30人笔试历年典型考点题库附带答案详解
- CSCO恶性血液病诊疗指南(2026版)
- 专题01 语言文字运用(真题汇编)(2021~2025)五年高考真题模拟语文专题(附答案)
- 渭南市大荔善达精神专科医院招聘笔试真题2025
- 2026年4月自考00258保险法试题
- 解酒饮创业计划书
评论
0/150
提交评论