Ministry of Higher Edu Scie Research AlMustansiriyah University

  • Slides: 7
Download presentation

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Experiment No. (6) - Plotting Commands Object: Plot several elements in digital signal processing, these variable can be generated using the command plot(y). Theory: Let’s say that your data set was the cubic of the numbers from negative five to four in step increments of one tenth. This data can be generated and plotted by typing: Y=(-5: 0. 1: 4). ^3; Plot(y); So we will obtain the figure shown in Figure (1) below. Figure (1): Plot of y=(-5: 0. 1: 4). ^3 Notice that the x-axis labels are not the numbers that were cubed, rather they are the subscript or index numbers of the vector y. MATLAB automatically plots data versus the index number when only one argument is passed to the plot function. In order to accomplish this for our previous example, we can use: X=-5: 0. 1: 4; Y=(x). ^3; Plot(x, y); Figure (3): Adding labels, title and grid The plot command argument are not restricted to being vectors, the inputs may also be matrices. When passing inputs to the plotting function, there are several simple rules to keep in mind so that the appearance of graph is what we expect. The rules can be summarized as follows: Plot(y) If y is a vector, you will generate a line of y versus the index numbers of y. 33 | P a g e

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ If y is a matrix, you will generate a set of lines where each line corresponds to the graph of one of the matrix columns versus the row number. plot(x, y) If x and y are vectors of the same length, a graph of y versus x will be displayed. If x is a vector and y is a matrix, the rows or columns of y will be plotted against x. If a column of the y matrix has the same length as vector x, then the columns will be plotted versus x. If the rows have the same length as vector x, then the rows will be plotted versus x. If the number of rows and columns of y are the same, the columns will be plotted versus x. If x is a matrix and y is a vector, y will be plotted against either the rows or columns of x. If a column of the x matrix has the same length as vector y, then y will be plotted versus columns of x. If the length of row of x is equivalent to. the length as vector y, then y will be plotted versus the rows of x. If the number of rows and- columns of x are the same, : y will be plotted versus the columns of x. If x and y are matrices which have the same number of rows and columns, the columns of y will be plotted against the columns of x. the plot command accepts a color-linestyle-marker string, which is a character string argument by which you can specify line types, plot symbols, and colors. For instance, if we wanted to plot a red dashed line with the vectors x and y, you could simply type the command: Plot(x, y, '--r'); The string that you create to define the characteristics of the line may use any combination of characters shown in Table (1) for the line type, symbol, or color. Table (1): Color line style marker 34 | P a g e

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Generating Plots with Multiple Data Set In matlab we can plot multiple sets of data with a single plot command. However, MATLAB does not restrict you to using a single call to the plot command in order to plot multiple lines in a graph. A command which you might find very useful is the hold command. The hold command allows you to additional lines to the current graph without erasing what already exists in it. When hold is not used, the plot command will replace the contents of the current graph. The hold command can be used in three different ways: Hold on tells MATLAB that you want all subsequent lines and surfaces to be added to the current graph. Hold off is used to turn off the hold command, setting MATLAB back to the default mode of replacing the current graph with a new one. Hold when used by itself will toggle the mode between the hold on and hold off state. Plotyy In some instances you will have data sets that you want to display on the same graph; however, the y-axis data values are not in the same range. MATLAB provides a useful graphics function for just such an occasion. The command plotyy, will help you plot these types of data sets on the same graph. 35 | P a g e

 ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Ministry

ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department Example: Let’s say you have created the following data sets X 1=0: 0. 1: 20 Y 1=x 1. *sin(x 1); X 2=10: 0. 2: 25; Y 2=50*x 2; If we plotted them with Plot (x 1, y 1, '-b', x 2, y 2, '--g'); Title('y 1 is the blue line, y 2 is the green dashed line'); Ylabel('y'); Xlabel('x'); As we can see in Figure (4) it is difficult to see the y 1 value since MATLAB's auto scaling is choosing the y-axis limits so as to display all the data points. Figure (4): plot of two data sets using 'plot' command Instead we could use plotyy to plot the which would generate the plot shown in (5). data Figure plotyy(x 1, y 1/x 2, y 2) Figure (5): using 'plotyy' command Stem Plots Stem plots provide another method of visualizing discrete data sequences, such as sampled time series data. In these types of graphs, vertical lines terminating with a symbol such as a circle are drawn from the zero value point on the y-axis to the values of the elements in the vector passed along with the command, stem(y). If you want spacing other than that provided by the element 36 | P a g e

 ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Ministry

ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department index number, you can use stem(x, y), where x specifies where the line is drawn along the x-axis. Figure (6) is an example that can be produced with the following code. X=0: 0. 25: (3*pi); Stem(x, sin(x)); Title('stem(x, sin(x))'); Xlabel('x'); Figure (6): Visuualizing discrete data with stem We can tell MATLAB to use any of the line styles and to terminate your stem plots with any of the marker types that are in Table (1). Additionally, these terminators can be either filled or unfilled. This line of code Will generate a stem plot in which the lines are dash-dotted and the terminating symbol is a filled five-pointed star as shown in Figure (7). Stem(x, sin(x), ' _' , 'p' , 'filled'); Figure (7): dotted lines Stem plot with filled terminator and dash- 37 | P a g e

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department

Ministry of Higher Edu. & Scie. Research Al-Mustansiriyah University Engineering Faculty Computer Engineering Department ﺛﺤﺒﻼﻭ ﻳﻤﻠﻌﻼ ﻠﻣﻴﻌﺘﻼ ةﺮﺍﺯﻭ ﻳﻼﻋﻼ ةﻌﻤﺎﺟﻼ ةﻴﺮﺻﻨﺘﺴﻤﻼ ةﺴﺪﻧﻬﻼ ﺑﻮﺳﺎﺣﻼ ةﺴﺪﻧﻪ ةﻴﻠﻚ ﻣﺴﻖ Procedure For an (M*M) matrix plot a vector of length M. first against its columns and the other against its rows. Discussion For a vector of length M, plot a matrix of size (M*M) against the vector. Stem(x, sin(x), ' _' , 'p' , 'filled'); 38 | P a g e