Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()

Similar presentations


Presentation on theme: "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()"— Presentation transcript:

1 ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot() command Plotting multiple curves in the same plot Formatting plots Basic Two Dimensional Plots / Chapter 5

2 ENG 1181 2 MATLAB PLOTS MATLAB has many functions and commands that can be used to create various types of plots. 107 In 1181 we will only create two dimensional x – y plots. x-y plot polar plot3D plot

3 ENG 1181 3 EXAMPLE OF A 2-D PLOT 108

4 ENG 1181 4 TWO-DIMENSIONAL plot() COMMAND where x and y are any vector names. Both vectors must have the same number of elements.  The plot command creates a single curve with the x values on the abscissa (horizontal axis) and the y values on the ordinate (vertical axis).  The curve is made from segments of lines that connect the points that are defined by the x and y coordinates of the elements in the two vectors. The basic 2-D plot command is: plot(x, y) 108- 112

5 ENG 1181 5 PLOT OF GIVEN DATA Given data in two vectors, x and y: >> x = [1 2 3 5 7 7.5 8 10]; >> y = [2 6.5 7 7 5.5 4 6 8]; >> plot(x,y) Once the plot command is executed, the Figure Window opens with the following plot. 112- 113 x123577.5810 y26.5775.5468

6 ENG 1181 6 PLOT OF GIVEN DATA 112- 113

7 ENG 1181 7 LINE SPECIFIERS IN THE plot() COMMAND plot(x, y, ’line specifiers’) Line specifiers are used in the plot() command to specify:  The style of the line  The color of the line  The type of the markers (if any)  The specifiers are entered as a string  Within the string the specifiers can be in any order  The specifiers are optional (that is, 0, 1, 2 or all 3 can be included in a command 109- 112

8 ENG 1181 8 109- 112 LINE SPECIFIERS IN THE plot() COMMAND plot(x, y, ’line specifiers’) Line StlyeSpecifierLine Color SpecifierMarker Type Specifier Solid Dotted Dashed Dash-dot - : -- -. Red Green Blue Black rgbkrgbk X-mark Circle Asterisk Point xo*.xo*. Information about these options (and others) can be accessed in the command window by typing: >> help plot

9 ENG 1181 9 Example Plot Line Specifiers: dashed red line and asterisk markers. 112- 113 >> year = [1988: 1: 1994]; >> sales = [127, 130, 136, 145, 158, 178, 211]; >> plot(year, sales, '--r*') Year1988198919901991199219931994 Sales ($M) 127130136145158178211

10 ENG 1181 10 PLOT OF GIVEN DATA USING LINE SPECIFIERS IN THE plot() COMMAND 112- 113 Dahed red line and asterisk markers

11 ENG 1181 11 CREATING A PLOT OF A FUNCTION % A script file that creates a plot of % the function: 3.5^(-0.5x)*cos(6x) x = [-2: 0.01: 4]; y = 3.5.^ (-0.5*x).* cos(6*x); plot(x,y) Consider: A script file for plotting the function is: Creating a vector with spacing of 0.01. Calculating a value of y for each x. Once the plot command is executed, the Figure Window opens with the following plot. 113- 114

12 ENG 1181 12 A PLOT OF A FUNCTION 113- 114

13 ENG 1181 13 A PLOT OF A FUNCTION If the vector x is created with large spacing, the graph is not accurate. Below is the previous plot with spacing of 0.3. 113- 114 x = [-2: 0.3: 4]; y = 3.5.^ (-0.5*x).* cos(6*x); plot(x,y)

14 ENG 1181 14 PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT Two Methods 1.Using the plot() command. 2. Using the hold on and hold off commands. 116- 118

15 ENG 1181 15 USING THE plot() COMMAND TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT Plots three graphs in the same plot: y versus x, v versus u, and h versus t.  By default, MATLAB makes the curves in different colors.  The curves can have a specific style by adding specifiers after each pair, for example: 116- 117 plot(x, y, u, v, t, h) plot(x, y, ’-b’, u, v, ’--r’, t, h, ’g:’)

16 ENG 1181 16 116- 117 Plotting Multiple Functions x = [-2: 0.01: 4]; y = 3 * x.^ 3 - 26 * x + 6; yd = 9 * x.^ 2 - 26; ydd = 18 * x; plot(x, y, '-b‘, x, yd, '--r', x, ydd, ':k') Vector x (the domain of the function) Vector y (the function value for each x) Vector yd (value of first derivitave Vector ydd (value of second derivitave) Create three graphs, y vs x (solid blue line), yd vs x (dashed red line), ydd vs x (dotted black line) in the same figure Plot of the function,, and its first and second derivatives, for -2 ≤ x ≤ 4 all in the same plot.

17 ENG 1181 17 116- 117 Multiple Functions

18 ENG 1181 18 USING THE hold on, hold off, COMMANDS TO PLOT MULTIPLE GRAPHS IN THE SAME PLOT This method is useful when all of the information (all vectors) used for the plotting is not available at the same time. 117 Holds the current plot and all axis properties so that subsequent plot commands add to the existing plot Returns to the default mode where plot commands erase the previous plots and reset all axis properties before drawing new plots hold on hold off

19 ENG 1181 19 HOLD ON, HOLD OFF Example 117 For light intensity vs. distance, plot the function, and the corresponding collected data in the same plot. x = [10: 0.1: 22]; y = 95000./ x.^ 2; x_data = [10: 2: 22]; y_data = [950 640 460 340 250 180 140]; plot(x, y, '-‘) hold on plot(x_data, y_data, 'ro--') hold off First graph is created A second graph is added to the first

20 ENG 1181 20 HOLD ON, HOLD OFF Example 108

21 ENG 1181 21 Title FORMATTED 2-D PLOT 108 Legend y-axis label Tick-mark Data symbol Tick-mark label x-axis label axis limits

22 ENG 1181 22 FORMATTING PLOTS A plot can be formatted to have a required appearance. With formatting you can:  Add a title to the plot  Add labels to the axes  Change the range of the axes (as appropriate)  Add a legend (when multiple curves)  Add a grid (if needed for clarity) 118- 123 More formatting options are discussed on pages 120-122 in the text book

23 ENG 1181 23 FORMATTING COMMANDS Adds the string as a title at the top of the plot Adds the string as a label to the x-axis Sets the minimum and maximum limits of the x-axis and y-axis Creates a legend for the various plots – must be in same order as plots were generated. title(‘string’) xlabel(‘string’) ylabel(‘string’) axis([xmin xmax ymin ymax]) legend(‘string1’, ‘string2’, ‘string3’) 118- 123

24 ENG 1181 24 FORMATTING Example 123 x = [10: 0.1: 22]; y = 95000./ x.^ 2; x_data = [10: 2: 22]; y_data = [950 640 460 340 250 180 140]; plot(x, y, '-', x_data, y_data, 'ro--') xlabel('DISTANCE (cm)') ylabel('INTENSITY (lux)') title('Light Intensity as a Function of Distance') axis( [8 24 0 1200] ) legend('Theory', 'Experiment') The plot that is obtained is shown again in the next slide. Labels for the axesTitle of the plotSet limits of the axes Create legend

25 ENG 1181 25 EXAMPLE OF A FORMATTED PLOT 123


Download ppt "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()"

Similar presentations


Ads by Google