1 / 150

AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 6 2D GRAPHICS

AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 6 2D GRAPHICS. Chapter 6 – Objective Present the implementation of a wide selection of two-dimensional plotting capabilities. Topics. Introduction: Graphics Management Basic 2D Plotting Commands Changing a Graph’s Overall Appearance

Télécharger la présentation

AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 6 2D GRAPHICS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 6 2D GRAPHICS

  2. Chapter 6 – Objective • Present the implementation of a wide selection of two-dimensional plotting capabilities.

  3. Topics • Introduction: Graphics Management • Basic 2D Plotting Commands • Changing a Graph’s Overall Appearance • Special Purpose Graphs • Reading, Displaying, and Manipulating Digital Images

  4. Topics • Graph Annotations and Enhancement • Introduction • Axes and Curve Labels, Figure Titles, Legends, and Text • Filling in Regions • Greek Letters, Mathematical Symbols, Subscripts, and Superscripts • Altering the Attributes of Axes, Curves, and Text • Positioning One Figure Inside Another Figure • Interactive Plotting • Animation

  5. Introduction – • MATLAB provides a wide selection of very flexible and easy-to-implement two- and three-dimensional plotting capabilities. • The plotting functions can be grouped into three categories: • graphics management • curve and surface generation • annotation and graph characteristics • Although there are quite a few plotting functions, for the most part their syntax is similar and they can be annotated with the same set of functions.

  6. Purpose of a Graphic* • A graph should communicate complex ideas with clarity, precision, and efficiency. • Graphical excellence is attained when the viewer obtains the greatest number of ideas in the shortest time with the least amount of ink. * E. Tufte, Visual Display of Quantitative information, Graphics Press, Cheshire, CT, 1997

  7. The Functions Whose Usage We Shall Illustrate Are -

  8. When generating graphed entities one should expend the effort so that each figure – • Meets the solution’s objective by illustrating what is important and significant. • Exhibits clarity and specificity by being fully annotated with the – • Axes labeled • Figure titled • Curves identified (if more than one) • Important numerical values displayed • However, any devices that are used to enhance the figure, such as color, line type, symbols, and text should do so without being distracting.

  9. A typical set of graph-creating expressions consists of management functions, followed by one or more graph-generation functions, and followed in turn by annotation functions, which may be followed by additional management functions. • Except for the management functions, the order of these functions is, in most applications, arbitrary. • The employment of the annotation and graph characteristic functions is optional. • MATLAB scales the axes and labels the axes’ magnitudes, even if more than one set of data is plotted. • Thus, one can always obtain a partially annotated graph, provided that the function’s syntax has been used correctly.

  10. A graph is created in a figure window, which is a window created by MATLAB at execution time, when any one of its graph management, generation, or annotation and characteristics functions is invoked. • When a script or function uses more than one graph-generation function MATLAB creates a new figure window. • Any previously created window is removed prior to creating the new figure window. • To retain each new graph in its own figure window, one must use • figure(n) • where n is an integer. • If the argument of figure is omitted, then MATLAB gives it the next integer value.

  11. One can also place several independently created graphs in one figure window with • subplot(i, j, k) • where • i, j divide the window into sectors (rows and columns) • k indicates in which sector a graph is to be placed • A value of 1 for this argument indicates the upper left corner, and the product of the number of rows and columns indicates the lower right corner. • As the numbers increase, they indicate the sectors from left to right, starting at the top row.

  12. Any annotation and management functions that appear in the program after figure and/or subplot apply only to that sector indicated by the third argument of subplot. • Within each sector, any compatible set of the 2D or 3D graph generation functions can be used.

  13. Examples – • Script or Function • figure(1) • plotting expressions • Script or Function • figure(2) • subplot(1, 2, 1) • plotting expressions • subplot(1, 2, 2) • plotting expressions

  14. Script or Function • figure(3) • subplot(2, 1, 1) • plotting expressions • subplot(2, 1, 2) • plotting expressions • Script or Function • figure(4) • subplot(2, 3, 3) • plotting expressions • subplot(2, 3, 2) • plotting expressions • subplot(2, 3, 1) • plotting expressions • subplot(2, 3, 4) • plotting expressions • subplot(2, 3, 5) • plotting expressions • subplot(2, 3, 6) • plotting expressions

  15. Since each graph-generation function creates a new figure window, in order to draw more than one curve, surface, or line (or combination of these) on a given graph, one must use • hold on • which holds the current window (or subplot sector) active. • All figures that have been created can be copied to the Windows clipboard by selecting Copy Figure from the Edit pull-down menu within each figure’s window. • This figure can then be transferred (pasted) to a page in a word processor program and will be in the Windows metafile format.

  16. Figure Format Conversion – • MATLAB provides the means to convert a figure to a format compatible with many common print devices. • For example, if one wants to save the graphics appearing in the active figure window as – • A level-2 encapsulated postscript file for black-and-white printers, • With the name FileName.eps, • Such that a “tiff” preview image of the figure can be displayed in a Word document, • then one uses

  17. print('-deps2', '-tiff', 'c:\path\FileName.eps') • where • -tiff is a keyword indicating that a tiff preview is available. • -deps2 is a keyword to indicate that a level-2 encapsulated postscript file is to be created. • path describes the directory and subdirectory names where the file will reside.

  18. Basic 2D Plotting Commands • The basic 2D plotting command is • plot(u, v, c) • where • u and v are the x and y coordinates, respectively, of a point or a series of points. • These points are either a pair of numbers, vectors of the same length, matrices of the same order, or expressions that, when evaluated, result in one of these three quantities.

  19. The quantity c is a string of characters. • One character specifies the line/point color. • One character specifies the point type, if points are to be plotted. • One or two characters are used to specify the line characteristics. • The order of the three sets of characters within the single quotes is not important. • If c is omitted, then the system’s default values are used. • If more than one curve is drawn, then the line colors change according to the default sequence.

  20. When both lines and points are to be plotted, but the points defining the line (u1, v1) are different from the points that are to be plotted (u2, v2), we use either • plot(u1, v1, c1, u2, v2, c2) • or • plot(u1, v1, c1) • holdon • plot(u2, v2, c2) • where • c1 contains the symbols for the line type and color. • c2 contains the symbols for the point type and color.

  21. Line and Point Characteristics (Table 6.2) • Example – • To plot blue dashed lines connecting blue diamonds, • c = 'b--d'

  22. Changing Line and Point Attributes – • One has the capability to change the attributes of the lines and points that are being plotted. • These attributes can be changed one of two ways. • The first way is with • plot(u1, v1, c1,'KeyWord', KeyWordValue, …) • where • 'KeyWord' is a string expression of the keyword for one of the line and point attributes. • KeyWordValue is either a numerical value or a string expression, depending on 'KeyWord'. • One may use as many pairs of keywords and their values as required.

  23. The second way to change the line and point attributes is with the combination of getting the handle to the plotted curve and to then change the attribute using set as follows. • hdl = plot(u1, v1, c1); • set(hdl, 'KeyWord', KeyWordValue, …) • We will give examples of this usage subsequently.

  24. Clicking on Axes brings up the window shown next. Use of the Help file to determine the appropriate keywords and their values.

  25. Clicking on Core Objects brings up the window shown next.

  26. Points • To place a red asterisk at the location (2,4), we use • plot(2, 4, 'r*') • Lines • To draw a straight line that goes from (0,0) to (1,2) using the default line type (solid) and the default color (blue), we use • plot([0, 1], [0, 2]) • Notice that the first two-element vector [0, 1] represents the values of the x coordinates, and the second two-element vector [0, 2] represents the values of the y coordinates.

  27. To draw a set of n unconnected lines whose end points are (x1k,y1k) and (x2k,y2k), k = 1,2,…,n, we create four vectors • Then, the plot command is • x1 = […]; x2 = […]; • y1 = […]; y2 = […]; • plot([x1; x2], [y1; y2]) • where [x1; x2] and [y1; y2] are each (2n) arrays. • This plot expression is a generalization of the plotting function stem, which assumes that x1 = x2 and that y1 = 0.

  28. Example – • Let us draw four black vertical lines from y = 0 to • y = cos(x/20) when x = 2, 4, 6, and 8. • The script is • x = 2:2:8; • y = [zeros(1, length(x)); cos(pi*x/20)]; • plot([x; x], y, 'k') • where we have used the fact that x1k = x2k, k = 1, …, n.

  29. ymax ymin xmin xmax • Unfortunately, because of automatic scaling of the axes, the first and last lines are coincident with the box around the figure. • Therefore, to adjust the axes so that these lines are visible, we use • axis([xmin, xmax, ymin, ymax]) • where xmin, xmax, ymin, and ymax are the minimum and maximum values of the x and y axes, respectively.

  30. The revised script is • x = 2:2:8; • y = [zeros(1, length(x)); cos(pi*x/20)]; • plot([x; x], y, 'k') • axis([1, 9, 0, 1]) Example – continued vs.

  31. Example – continued • To place a red square at the end of each vertical line that is off the x-axis, we have to add another triplet of instructions in the plot command • Thus, the script becomes • x = 2:2:8; • y = [zeros(1, length(x)); cos(pi*x/20)]; • plot([x; x], y, 'k', x, cos(pi*x/20), 'rs') • axis([1, 9, 0, 1])

  32. Example – continued • To change the square with red edges to a solid red square with a blue edges that is larger than the default size, we use the Help file to find that the governing keywords are • MarkerEdgeColor • MarkerFaceColor • MarkerSize • Hence, the script becomes • x = 2:2:8; • y = [zeros(1, length(x)); cos(pi*x/20)]; • plot([x; x], y, 'k') • holdon • plot(x, cos(pi*x/20), 's', 'MarkerEdgeColor', 'b', … • 'MarkerFaceColor', 'r', 'MarkerSize', 14) • axis([1, 9, 0, 1])

  33. Circles • To draw a circle of radius r whose center is located at (a, b) in a Cartesian coordinate system, one has to first transform the radial coordinates to Cartesian coordinates using where 0 1 2. When 1 < 2, we draw an arc of a circle. If we assume that 1 = 2, a = 1, b = 2, and r = 0.5, then the script to draw the circle is

  34. Example – continued • theta = linspace(0, 2*pi); • plot(1+0.5*cos(theta), 2+0.5*sin(theta)) • axis equal • The axis equal function proportions the graph so that the circles appear as circles, rather than as ellipses.

  35. Example – continued • The script to draw a family of six concentric circles, whose initial radius of 0.5 increases in increments of 0.25 and whose centers are indicated by a plus sign, is • theta = linspace(0, 2*pi, 50); % (150) • rad = 0.5:0.25:1.75; % (16) • x = 1+cos(theta)'*rad; % (506) • y = 2+sin(theta)'*rad; % (506) • plot(x, y, 'k', 1, 2, 'k+') • axis equal • Recall that MATLAB operates • on matrices on column by • column basis.

  36. Family of Curves • In general, MATLAB allows one to have the x-axis represented by a vector and the y-axis by a matrix. • It will draw the curves by drawing the vector versus either the columns or the rows of the matrix, depending on which one matches the length of the vector.

  37. Example – • Draw a family of parabolas for 5 x 5 in increments 0.2 and a = 1, 2, …, 5 • The script is • x = 5:0.2:5; % (151) • a = 1:5; % (15) • [xx, aa] = meshgrid(x.^2, a.^2); % (551) • plot(x, aaxx, 'k') Note: MATLAB plots this expression by using the length of x and matching this length with the same length of either the row or column of the array aa-xx.

  38. Example – • Visualize of the convergence of the series • for N = 1, 2, …, 10 and a = 1, 2, and 3. • The script is • aa = 1:3; % (13) • N = 1:10; % (110) • [a, k] = meshgrid(aa, N); % (103) • S = cumsum(1./(a+k).^2); % (103) • plot(N, S, 'ks-')

  39. Multiple Functions Plotted on One Figure • Consider the three functions where 0 x , y , z 3.5. We can draw these three functions on one figure in either of three ways -

  40. Example – continued • x = linspace(0, 3.5); • plot(x, [0.1*x.^2; cos(x).^2; exp(-0.3*x)], 'k') • or • x = linspace(0, 3.5); • plot(x, 0.1*x.^2, 'k', x, cos(x).^2, 'k', x, exp(-0.3*x), 'k') • or • x = linspace(0, 3.5); • plot(x, 0.1*x.^2, 'k') • hold on • plot(x, cos(x).^2, 'k') • plot(x, exp(-0.3*x), 'k') Note: Color specification is required.

  41. Example – continued • If the range of the independent variable for each of these functions is different, then only the second and third scripts can be used. • For example, if 0 x 3, 1 y 4, and 2 z 5, then • x = linspace(0, 3, 45); • y = linspace(1, 4, 55); • z = linspace(2, 5, 65); • plot(x, 0.1*x.^2, 'k-', y, cos(y).^2, 'b--', z, exp(-0.3*z), 'r-.')

  42. Changing Graph Appearance • box, grid, and axis • Several functions can be used to change the appearance of a graph - • axis on or axis off [default – on] • box on or box off [default – on] • grid on or grid off [default – off] • The function boxon only works when axis on has been selected.

  43. th = linspace(0, 2*pi, 101); x = sin(th); y = sin(2*th+pi/4); plot(x, y, 'k-') box on grid on th = linspace(0, 2*pi, 101); x = sin(th); y = sin(2*th+pi/4); plot(x, y, 'k-') box off grid off axis off

  44. th = linspace(0, 2*pi, 101); x = sin(th); y = sin(2*th+pi/4); plot(x, y, 'k-') box off grid off

  45. Special Purpose Graphs • MATLAB has a library of special purpose graphs that are applicable to a wide variety of applications. • We shall briefly illustrate them by having each of them plot some part of the following expression. where  < 1 and

More Related