Download presentation
Presentation is loading. Please wait.
Published byPhillip Miles Modified over 9 years ago
1
„ 1999 BG Mobasseri1 9/18/2015 June 2 GRAPHICS IN MATLAB- PART I BASIC PLOTTING
2
„ 1999 BG Mobasseri2 9/18/2015 PLOTTING A SINGLE ARRAY The most basic command that puts a graphic on screen is plot If y is an array, plot(y) simply plots values of y vs. their array index position Do this: –t=[0:0.01:1]; –y=cos(2*pi*t); –plot(y) Notice numbers on x-axis. They are not time
3
„ 1999 BG Mobasseri3 9/18/2015 PLOTTING ONE ARRAY AGAINST ANOTHER If t is one array and y another, plot(t,y) plots y vs. t. Do this: –t=[0:0.01:1]; –y=cos(t); –plot(t,y)
4
„ 1999 BG Mobasseri4 9/18/2015 Try it! Plotting one array against another generates creative figures For example, define your x-array as cos(2(pi)t) and y-array as sin(2(pi)t). Then plot y vs.x. Let t run from 0 to 1 as before Do the same for 2cos(2(pi)t) vs. 4sin(2(pi)t)
5
„ 1999 BG Mobasseri5 9/18/2015 Plots via a matrix The argument of plot can be a matrix. In this case, each column is plotted separately on the same axis. This is a compact way of plotting several graphs on the same axis Try –t=0:pi/16/:2*pi; –x=[cos(t)’,sin(t)’] –plot(x) each column is plotted separately vs. position
6
„ 1999 BG Mobasseri6 9/18/2015 Plotting a matrix against another array Let Y be a matrix and x be the vector of variables, then –plot(x,Y) plots columns of Y vs. x on the same graph Try this –t=0:pi/16:2*pi; –Y=[cos(t)’,sin(t)’] –plot(t’,Y) each column plotted separately vs. another array
7
„ 1999 BG Mobasseri7 9/18/2015 Putting multiple plots on the same graph There are 3 ways to do this. Want to plot 3 data vectors y1,y2 and y3 vs. t Method 1: –Y=[y1,y2,y3] –plot(t,Y) Method 2 –plot(t,y1,t,y2,t,y3) Method 3 –Use hold command
8
„ 1999 BG Mobasseri8 9/18/2015 RC circuit response Capacitors charge and discharge following an exponential curve. Plot the following 3 voltages on the same graph for 0<t<5.
9
„ 1999 BG Mobasseri9 9/18/2015 Try it!
10
„ 1999 BG Mobasseri10 9/18/2015 hold COMMAND Once hold is turned on, all subsequent plot commands are superimposed on the same axis hold cycles between on and off. For example, –plot(t,y1) –hold on –plot(t,y2) –plot(t,y3) –hold off
11
„ 1999 BG Mobasseri11 9/18/2015 Homework #1 Plot y vs. x over the range x=[-2,2]. In addition to your m-file, save your figure and send it to me
12
„ 1999 BG Mobasseri12 9/18/2015 Plotting parametric functions- fplot. You may want to plot a function, rather than an array of numebrs, against a variable. Example is plotting cos(x). The command is fplot(‘cos’,[-pi pi]) Note that the function must be known to MATLAB such as sine and cosine function x range
13
„ 1999 BG Mobasseri13 9/18/2015 MULTIPLE PLOTS USING fplot fplot(‘[sinc(x),sinc(2*x)]’,[-2 2])
14
„ 1999 BG Mobasseri14 9/18/2015 CONTROLLING LINE TYPES We can select line styles and colors in a graph Simply pass a character string to plot –plot(t,x,’s’) For example, s=r+, puts red plus(+) marks for every data point. MATLAB accepts many other choices. Redo the exponentials using different styles
15
„ 1999 BG Mobasseri15 9/18/2015 Homework #2 Plot the 3 exponentials on slide 8 simultaneously. –1. Use red, green and blue solid lines respectively –2. Use red plus sign(+), green star(*) and blue dots (.) for each graph
16
„ 1999 BG Mobasseri16 9/18/2015 SPECIALIZED 2D PLOTTING FUNCTIONS For each item write an m-code to see the plots. Read the example slides bar - creates a, well, bar graph polar - creates a ( plot hist - creates a histogram of data fill - fills the curve with solid colors stairs - similar to bar but without internal lines
17
„ 1999 BG Mobasseri17 9/18/2015 bar Usage: –bar(x,y) –plot the shape of a normal distribution given by y=exp(-x 2 ) in the range (-2,2).
18
„ 1999 BG Mobasseri18 9/18/2015 fill Usage: –fill(x,y,’b’) where b indicates a blue fill. –plot the function exp(-t^2), in the range (-2,2)
19
„ 1999 BG Mobasseri19 9/18/2015 polar Usage: –polar(theta,rho) Plot
20
„ 1999 BG Mobasseri20 9/18/2015 stairs Usage –stairs(x,y) –plot the function exp(-x^2), in the range (-4,4)
21
„ 1999 BG Mobasseri21 9/18/2015 ADDING LABELS AND TITLES There are numerous ways the appearance of a plot can be controlled. The most obvious ones are through –axis labels –figure title –legend –grid
22
„ 1999 BG Mobasseri22 9/18/2015 LABEL USAGE axis labels –xlabel(‘time’) –ylabel(‘voltage’) Figure title –title(‘Capacitor voltage’) Legend –legend(‘first’,’second’,’third’) Grid –grid - places a grid over the plot
23
„ 1999 BG Mobasseri23 9/18/2015 Try it!
24
„ 1999 BG Mobasseri24 9/18/2015 Homework #3 Over a time range [-3, 3], plot the following 3 functions simultaneously using the matrix method of slide 7 –y=e -4x cos(2*pi*x);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.