How To Draw Vertical Reference Line Subplots In Matlab
Plotting in MATLAB
1 of the nearly important functions in Matlab is the plot function, which besides happens to be one of the easiest functions to acquire how to use. The basic format of the function is to enter the following control in the MATLAB command window or into a MATLAB script.
plot(x,y)This command will plot the elements of vector x on the horizontal axis of a figure, and the elements of the vector y on the vertical axis of the figure. The default is that each time the plot command is issued, the current effigy will be erased; nosotros will discuss how to override this below. If nosotros wanted to plot the simple, linear formula:
y=3xWe could create a m-file with the following lines of code:
10 = 0:0.1:100; y = 3*x; plot(ten,y)which will generate the following plot:
One matter to keep in mind when using the plot control is that the vectors x and y must be the aforementioned length. The other dimension can vary. MATLAB can plot a 1xN vector versus a Nx1 vector, or a 1xN vector versus a 2xN matrix (you will get ii lines), as long as N is the aforementioned for both vectors.
The plot command can also be used with only ane input vector. In that instance the vector columns are plotted versus their indices (the vector 1:1:N will be used for the horizontal axis). If the input vector contains complex numbers, MATLAB plots the real part of each chemical element (on the ten-centrality) versus the imaginary part (on the y-axis).
Plot aesthetics
The color and point marker tin can exist changed on a plot by adding a third parameter (in single quotes) to the plot command. For example, to plot the above function every bit a blood-red, dotted line, the m-file should be inverse to:
x = 0:0.one:100; y = 3*ten; plot(x,y,'r:')The plot now looks like:
The third input consists of one to three characters which specify a colour and/or a point marking type. The listing of colors and bespeak markers is equally follows:
y yellow . point m magenta o circle c cyan x x-marking r ruby-red + plus chiliad green - solid b blue * star westward white : dotted m black -. dashdot -- dashedYou can plot more one function on the aforementioned figure. Let's say y'all want to plot a sine wave and cosine moving ridge on the same ready of axes, using a unlike color and point marker for each. The following one thousand-file could be used to do this:
x = linspace(0,2*pi,50); y = sin(x); z = cos(10); plot(10,y,'r', ten,z,'gx')You volition get the following plot of a sine moving ridge and cosine wave, with the sine wave in a solid red line and the cosine wave in a green line made up of xs:
By adding more sets of parameters to plot, yous can plot as many different functions on the aforementioned figure as you want. When plotting many things on the aforementioned graph it is useful to differentiate the different functions based on color and bespeak marker. This aforementioned event tin can besides be achieved using the concur on and hold off commands. The same plot shown in a higher place could be generated using the following m-file:
x = linspace(0,2*pi,50); y = sin(x); plot(x,y,'r') z = cos(x); concur on plot(x,z,'gx') hold offAlways remember that if you use the hold on control, all plots from then on will be generated on one set of axes, without erasing the previous plot, until the hold off command is issued.
Subplotting
More than one plot can exist put on the aforementioned figure using the subplot command. The subplot command allows you to separate the figure into as many plots as desired, and put them all in one figure. To use this command, the following line of code is entered into the MATLAB command window or an yard-file:
subplot(k,due north,p)This command splits the figure into a matrix of thou rows and n columns, thereby creating m*due north plots on one figure. The pth plot is selected equally the currently active plot. For instance, suppose you want to see a sine moving ridge, cosine moving ridge, and tangent moving ridge plotted on the same figure, but not on the same axis. The following chiliad-file will accomplish this:
x = linspace(0,2*pi,50); y = sin(10); z = cos(x); due west = tan(x); subplot(2,2,ane) plot(x,y) subplot(ii,2,ii) plot(10,z) subplot(ii,2,three) plot(x,w)
Equally you tin run into, there are but 3 plots, even though I created a 2 x 2 matrix of four subplots. I did this to show that you lot practise not take to make full all of the subplots you have created, but MATLAB will leave a spot for every position in the matrix. I could take easily made another plot using the line subplot(2,two,4) control. The subplots are arranged in the same manner as you would read a book. The first subplot is in the top left corner, the next is to its right. When all the columns in that row are filled, the left-most cavalcade on the adjacent row downwardly is filled (all of this assuming you make full your subplots in order i.eastward. 1, 2, iii,..).
One affair to note about the subplot command is that every plot control issued later volition place the plot in whichever subplot position was last used, erasing the plot that was previously in it. For example, in the m-file above, if a plot command was issued later in the m-file, it would be plotted in the third position in the subplot, erasing the tangent plot. To solve this problem, the figure should be cleared (using clf), or a new figure should be specified (using effigy).
Changing the axis
Now that you take plant unlike ways to plot functions, you can customize your plots to meet your needs. The most of import fashion to exercise this is with the axis command. The axis command changes the axis of the plot shown, so only the part of the axis that is desirable is displayed. The axis command is used past entering the following command right subsequently the plot control (or whatsoever command that has a plot as an output):
centrality([xmin, xmax, ymin, ymax])For example, suppose want to look at a plot of the function y=exp(5t)-1. If you enter the following into MATLAB
- t=0:0.01:5; y=exp(five*t)-one; plot(t,y)
Every bit yous tin can see, the plot goes to infinity. Looking at the y-axis (calibration: 8e10), it is apparent that not much can be seen from this plot. To become a meliorate idea of what is going on in this plot, let's look at the first 2nd of this function. Enter the following command into the MATLAB command window.
- centrality([0, 1, 0, 50])
Now this plot is much more useful. You tin can see more conspicuously what is going on as the function moves toward infinity. Y'all can customize the centrality to your needs. When using the subplot command, the axis tin exist changed for each subplot past issuing an axis command before the next subplot command. There are more uses of the axis control which you can see if y'all type help centrality in the MATLAB command window.
Adding text
Another thing that may be important for your plots is labeling. Y'all tin give your plot a title (with the title command), 10-axis label (with the xlabel control), y-axis label (with the ylabel command), and put text on the actual plot. All of the above commands are issued subsequently the actual plot command has been issued.
A title will be placed, centered, above the plot with the control: title('title string'). The x-axis label is issued with the post-obit command: xlabel('x-axis string'). The y-axis label is issued with the following command: ylabel('y-axis cord').
Furthermore, text tin exist put on the plot itself in one of two means: the text control and the gtext control. The first control involves knowing the coordinates of where yous desire the text string. The command is text(xcor,ycor,'textstring'). To use the other control, yous exercise not need to know the exact coordinates. The control is gtext('textstring'), then you just motility the cross-hair to the desired location with the mouse, and click on the position you want the text placed.
To further demonstrate labeling, take the step response plot from in a higher place. Assuming that you have already changed the centrality, copying the post-obit lines of text afterwards the axis command will put all the labels on the plot:
title('footstep response of something') xlabel('time (sec)') ylabel('position, velocity, or something like that') gtext('unnecessary labeling')The text "unnecessary labeling" was placed right above the position, I clicked on. The plot should look similar the following:
Other commands that can be used with the plot command are:
- clf (clears the current plot, so it is blank)
- figure (opens a new effigy to plot on, so the previous effigy is saved)
- close (closes the current figure window)
- legend adds a legend to the graph, useful when you have two curves, come across example below where fable('y=exp(5t)-one','y=exp(5(t+3))-one'); is used:
Acknowledgement
Thanks to the Carnegie Mellon University and the Command group at the College of Engineering, University of Michigan for use of their tutorial materials, partly funded by the National Science Foundation (USA).Source: http://info.ee.surrey.ac.uk/Teaching/Courses/matlab/plotting/
Posted by: aldrichfater1942.blogspot.com
0 Response to "How To Draw Vertical Reference Line Subplots In Matlab"
Post a Comment