Introduction to Matlab Matlab Graphics 2 D Graphics

Introduction to Matlab: Matlab Graphics 2 D Graphics S. Awad, Ph. D. M. Corless, M. S. E. E. E. C. E. Department University of Michigan-Dearborn

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department 2 D Graphics Topics n n n n n Creating Two Dimensional Plots Creating Multiple Plots Using a Legend Box Customizing Axes Zoom Command Adding Text Getting Data from a Graph Sub Plotting Other 2 D Features and Plot Types 2 D EZPlot 2

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Two-Dimensional Plots n Plot Command for 2 D plots n Sinewave Example: » theta=[0: pi/128: 2*pi]'; » y=sin(theta); Marker: Point » plot(theta, y, 'b. '); Color: Blue » xlabel('Angle in Radians'); » ylabel('Amplitude'); » title('One Period of a Sine Wave'); Writes Title of Graph 3

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department 2 -D Grids To Enable Grid » grid on To Disable Grid » grid off Grid Toggles 4

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Stem Command » theta=[0: pi/32: 2*pi]'; » y=cos(theta); » stem(theta, y, 'rx'); Color: Red Marker: x » xlabel('Angle in Radians'); » ylabel('Amplitude'); » title('Stem Plot of Cosine Wave'); 5

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Clearing a Figure n n In case a figure already exists, the plot command will clear the current figure window and draws a new plot. To clear a figure use the clf command » clf 6

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Multiple Plots n Consider the following vector pairs: » x 1=[1 2 3 4]'; y 1=[4 4. 1 4. 2 4. 3]'; (x 1 and x 2 have same length) » x 2=[1 2 3 4 5]'; y 2=[5 5. 1 5. 2 5. 3 5. 4]'; (x 2 & y 2 have same length but may be different from x 1 & y 1) » x 3=[1 2 3 4 5 6]'; y 3=[6 6. 1 6. 2 6. 3 6. 4 6. 5]'; (x 3 & y 3 have same length but may be different from x 1 & y 1. . . ) n To plot the 3 plots on the same graph plot(x 1, y 1, 'r', x 2, y 2, 'xb', x 3, y 3, 'k'); n xlabel, ylabel, & title can also be put on the graph 7

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Figure with Multiple Plots plot(x 1, y 1, 'r', x 2, y 2, 'xb', x 3, y 3, 'k'); r = color: red xb = marker: x k color: = color: blue black 8

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Plotting Using Hold n » n» n» n hold on command to draw multiple plots on same figure plot(x 1, y 1, 'r'); hold on plot(x 2, y 2, 'xb') hold on plot(x 3, y 3, 'k'); 9

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Vectors of Same Length n If x 1, x 2, & x 3 are the same vector x: » x =[1 n 2 3 4 ]’; And there are y 1, y 2, & y 3 of same size: » y 1=[1 2 3 4 ]’; » y 2=[1. 2 2. 2 3. 2 4. 2]’; » y 3=[1. 4 2. 4 3. 4 4. 4]’; n Then to plot the 3 plots at the same time » y = [ y 1, y 2, y 3] % 3 columns » plot( x, y ); 10

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Same Length Plots n n 3 Vectors How to Distinguish between plots? 11

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Legend Box n To create a legend box in the upper right corner of the plot for the previous example: » legend('First', 'Second', 'Third' ); n n To move the legend, click and hold the left mouse button near the edge of the legend and drag it to the required place. To delete the legend, use » legend off 12

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Placing Legend Box n Click and Drag to place Legend 13

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Customizing Axes n To set each axis minimums and maximums » axis([ xmin xmax ymin ymax]); n n To return to default settings » axis('auto') or » axis auto Set scaling factors for both axes to be equal » axis equal n To keep MATLAB from altering the proportions of the axes if the view is changed » axis vis 3 d 14

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Zoom Command n To expand the sections of a 2 -D plot use » zoom on n Click the left mouse button to expand the area by a factor of 2 around the point under the mouse pointer Each time you click, the plot expands To return plot to it’s initial state » zoom out n To exit zoom mode » zoom off Note: Turn Legend off before using zoom 15

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Adding Text to a Graph n n gtext is used to place text on a graph It puts the cross-hair that follows the mouse and waits for a click at the desired location » gtext('These are straight lines'); 16

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Getting Data from a Graph n n Suppose there is a current plot on the screen and we want to select some points from the graph and return the x & y coordinates To do this use the ginput command » [xp, yp] = ginput(3); Number of Points Selected Y-Coordinate Vector X-Coordinate Vector 17

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Subplots on Same Figure n n n subplot(m, n, p) breaks the Figure window into an m-by-n matrix of small axes & selects the p-th axes The p-th axis is numbered left to right along the top row, second row, etc. Each subplot is a separate graph When a particular subplot is active, it is the only subplot (or axis) that is responsive to axis, xlabel, etc. The other subplots are not affected. To return to default mode use: » subplot(1, 1, 1) 18

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Subplot Example » » » subplot(2, 3, 1); title('plot 1'); subplot(2, 3, 2); title('plot 2'); subplot(2, 3, 3); title('plot 3'); subplot(2, 3, 4); title('plot 4'); subplot(2, 3, 5); title('plot 5'); subplot(2, 3, 6); title('plot 6'); 19

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Other 2 -D Features » loglog(x, y); Plots log scale for x & y » semilogx(x, y); » semilogy(x, y); X-Axis uses log scale Y-Axis uses log scale » pie(a, b); Pie Chart (a&b are vectors) » bar(x, y); Bar Graph 20

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Pie Chart Example » a=[0. 5, 1, 1. 6, 1. 2, 0. 8, 2. 1]; » pie(a, a==max(a)); Optional logical vector describing a slice or slices to be pulled out of pie chart 21

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department Bar Graph Example » x=[-2. 9: 0. 2: 2. 9]; » y=exp(-x. *x); » bar(x, y); 22

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department 2 -D EZPlot n n ezplot(f) is used to plot symbolic variables f is a string or symbolic expression representing a mathematical expression involving a single symbolic variable say ‘x’; » f='sin(x)'; » xmin=-2*pi; xmax=2*pi; » ezplot(f, [xmin, xmax]); 23

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department EZPlot Sine Example » f='sin(2*x)*exp(-x)'; xmin=0; xmax=2*pi; » ezplot(f, [xmin, xmax]); 24

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department EZPlot X 2 Example » ezplot('x^2', [0, 5]), grid; 25

MATLAB Graphics: 2 D Graphics Introduction to MATLAB and its Toolboxes U of M-Dearborn ECE Department EZPlot Exponential Example » » f='sin(x)*exp(-x)'; ezplot(f, [0, 10]); grid on; ylabel('Amplitude'); 26
- Slides: 26