1、机械振动加州大学University of CaliforniaChapter5TwoD要点Chapter 5 Two-Dimensional Plots5.1 THE PLOT COMMAND To create two-dimensional plotsplot(x,y)- The arguments x and y are each a vector (one-dimensional array).- Both vectors must have the same number of elements.- When the plot command is executed a figur
2、e is created in the Figure Window.- The curve is constructed of straight-line segments that connect the points whose coordinates are defined by the element of the vectors x and y.- The figure that is created has axes with linear scale and default range.- For example, x = 1 2 3 5 7 7.5 8 10; y = 2 6.
3、5 7 7 5.5 4 6 8; plot(x,y)- The plot appears on the screen in blue which is the default line color. The plot command has additional optional arguments that can be used to specify the color and style of the line and the color and type of markers, if any are desired. With these options the command has
4、 the form:plot(x,y,line specifiers, PropertyName, PropertyValue)- (Optional) line specifiers that defines the type and color of the line and markers.- (Optional) With PropertyName, PropertyValue, properties with values that can be used to specify the line width, and markers size and edge, and fill c
5、olors.Line Specifiers: The line style specifiers are:LineStyleSpecifierLineStyleSpecifierSolid(default)-Dotted:Dashed-Dash-dot-. The line color specifiers are:Line ColorSpecifierLine ColorSpecifierRedrMagentamGreengYellowyBluebBlackkCyancWhitew The marker type specifiers are:MarkerTypeSpecifierMarke
6、rTypeSpecifierplus sign+squarescircleodiamonddasterisk*five-pointed starppoint.six-pointed starhNotes about using the specifiers The specifiers are typed inside the plot command as strings. Within the string the specifiers can be typed in any order. The specifiers are optional. This means that none,
7、 one, two, or all the three can be included in a command. Some examples:plot(x,y,r) A red solid line connects the points.plot(x,y,-y) A yellow dashed line connects the points.plot(x,y,*) The points are marked with * (no line between the points).plot(x,y,g:d) A green dotted line connects the points t
8、hat are marked with diamond markers.Property Name and Property Value: Properties are optional and can be used to specify the thickness of the line, the size of the marker, and the colors of the markers edge line and fill. The Property Name is typed as a string, followed by a command a value for the
9、property, all inside the plot command. Four properties and possible values are:Property NameDescriptionPossible Property ValuesLineWidth(or linewidth)Specifies the width of the lineA number in units of points (default 0.5)MarkerSize(or markersize)Specifies the size of the markerA number in units of
10、pointsMarkerEdgeColor(or markeredgecolor)Specifies the color of the marker, or the color of the edge line for filled marker.Color specifiers, typed as a stringMarkerFaceColor(or markerfacecolor)Specifies the color of the filling for filled markerColor specifiers, typed as a string- For example, the
11、commands create a plot that connects the points with a magenta solid line and circles as markers at the points. The line width is two points and the size of the circle markers is 12 points. The markers have a green edge line and yellow filling. x = 1 2 3 5 7 7.5 8 10; y=2 6.5 7 7 5.5 4 6 8; plot(x,y
12、, -mo, linewidth, 2, markersize, 12, markeredgecolor, g, markerfacecolor, y)5.1.1 Plot of Given Data For example, with the sales data of a company from 1988 to 1994, yr = 1988:1:1994; sle = 8 12 20 22 18 24 27; plot(yr, sle, -r*, linewidth, 2, markersize, 12)5.1.2 Plot of a Function To plot a given
13、function, use the plot or the fplot commands. With the plot command, the user needs to first create a vector of values of x for the domain that the function will be plotted. Then, a vector y is created by using element-by-element calculations. To plot for -2x4,%Chapter5Plot1.m%the function: 3.5.(-0.
14、5*x).*cos(6x)x = -2:0.01:4;y = 3.5.(-0.5*x).*cos(6*x);plot(x,y) help Chapter5Plot1Chapter5Plot1.m the function: 3.5.(-0.5*x).*cos(6x) Chapter5Plot1- The plot can be copied from the Figure Window (in the Edit menu select Copy Figure) and then paste it in other applications.5.2 THE fplot COMMAND The f
15、plot command plots a function with the form y = f(x) between specified limits. The command has the form:fplot(function, limits, line specifiers)function- The function can be typed directly a string inside the command. For example, if the function is, it is typed as 8*x2+5*cos(x).- The function canno
16、t include previously defined variables.limits- The limits is a vector with two elements that specify the domain of x xmin, xmax, or a vector with four elements that specifies the domain of x and the limits of the y-axis xmin,xmax,ymin,ymax. For example, a plot of for can be created with the fplot co
17、mmand: fplot(x2+4*sin(2*x)-1,-3 3)5.3 PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT5.3.1 Using the plot Command Two or more graphs can be created in the same plot by typing pairs of vectors inside the plot command:plot(x,y,u,v,t,h)- It creates three graphs all in the same plot.- The vectors of each pair
18、 must be of the same length.- MATLAB automatically plots the graphs in different colors.- It is possible to add line specifiers following each pair. For example,plot(x,y,-b,u,v,r,t,h,g:)Sample Problem 5-1 Plot the function, and its first and second derivatives, for -2x4, all in the same plot.%SP5_1.
19、mx = -2:0.01:4;y = 3*x.3-26*x+10;dy = 9*x.2 - 26;ddy = 18*x;plot(x,y,-b,x,dy,-r,x,ddy,:k)5.3.2 Using the hold on, hold off Commands%SP5_1_a.m for Sample Problem 5-1x = -2:0.01:4;y = 3*x.3-26*x+10;dy = 9*x.2 - 26;ddy = 18*x;plot(x,y,-b)hold onplot(x,dy,-r)plot(x,ddy,:k)5.3.3 Using the line Command Wi
20、th the line command additional graphs (lines) can be added to a plot that already exists. The form of the line command is:line(x,y,PropertyName, propertyValue)- (Optional) PropertyName, propertyValue- The format of line command is almost the same as the plot command.- The line command does not have
21、the line specifiers. The plot command starts a new plot every time it is executed, while the line command adds lines to a plot that already exists. Thus, for the Sample Problem 5-1, the following M-file can be used:%SP5_1_b.m for Sample Problem 5-1x = -2:0.01:4;y = 3*x.3-26*x+10;dy = 9*x.2 - 26;ddy
22、= 18*x;plot(x,y,-b)line(x,dy, linestyle, -, color, r)line(x,ddy, linestyle, :, color, k)5.4 FORMATTING A PLOT A figure that contains a plot needs to be formatted to display information in addition to the graph itself. It can include specifying axis labels, plot title, legend, grid, range of custom a
23、xis, and text labels.5.4.1 Formatting a plot Using CommandsThe xlabel and ylabel commands: Labels can be placed next to the axes with the following commands:xlabel(text as string)ylabel(text as string)The title command: The text is placed at the top of the figure as a title with the following comman
24、d:title(text as string)The text command: A text can be placed in the plot with the text or gtext commands:text(x,y,text as string)gtext(text as string)- The text command places the text in the figure such that the first character is positioned at the point with the coordinates x, y (according to the
25、 axes of the figure).- The gtext command places the text at a position specified by the user. When the command is executed, the Figure Window opens and the user specifies the position with the mouse.The legend command: To place a legend on the plot,legend(string1, string2, , pos)- The strings are th
26、e labels that are placed next to the line sample.- Their order corresponds to the order that the graphs were created.- The pos is an optional number that specifies where in the figure the legend is placed. The options are:pos = -1 Places the legend outside the axes boundaries on the right side.pos =
27、 0 Places the legend inside the axes boundaries in a location that interferes the least with the graphs.pos = 1 Places the legend at the upper-right corner of the plot (default).pos = 2 Places the legend at the upper-left corner of the plot.pos = 3 Places the legend at the lower-left corner of the p
28、lot.pos = 4 Places the legend at the lower-right corner of the plot.Formatting the text: A complete explanation of all the formatting features can be found in the Help Window under Text and Text Properties. The formatting can be done either by adding modifiers inside the string, or by adding to the
29、command optional PropertyName and PropertyValue arguments following the string. The modifiers are characters that are inserted within the string.ModifierEffectbfbold fontititalic stylermnormal fontfontnamefontnamespecifies font is usedfontsizefontsizespecifies font size is used- These modifiers affe
30、ct the text from the point that they are inserted until the end of the string.- It is possible to have the modifiers applied to only a section of the string by typing the modifier and the text to be affected inside braces . The subscript and superscript:- A single character can be displayed as a subscript or a superscript by typing _ or in front of the character, respectively.- Several consecutive characters can be displayed as subscript or a superscript by typing the characters inside braces following the _ or the . Greek charac
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2