fancy output, let's look at plotting. MATLAB has an extremely powerful plotting facility, and it's workhorse is the built-in function, plot. You may remember we saw it in Lesson One. Well we're going to go over it again with a bit more detail this time. The easiest way to use the plot function is simply to pass a vector to it. So let's get a vector. [CLICKING] This is just a list of the squares of the numbers from one to ten. And plotting it [CLICKING] is done like that. And there we go. There's our plot. As we saw in lesson one, it shows up as a separate window in it's labeled figure, Figure 1. We docked it last time like this. Shows up right here, so now we've got the Command Window visible. But I'm going to undock it. Now that covers up the command window. But if I click in a command window, it uncovers it. But where did it go? Well, it's still there. It's just behind. I'm going to move this down a little bit, or actually squeeze it down. Leave a little room up here. And then I'm going to resize this figure window. Hm, put it up here, so I can see this figure, and I can have other figures laid out here. I think I'll start this one over to the left. Okay, so where was I? Uh...Oh yeah. This, so this is a half of a parabola. To plot both side of the parabola, we can do this. [CLICKING] So there's a little longer vector. It's the squares of the numbers from minus ten to ten. Now I want to plot this one in a separate figure. If I just plot now, it'll replace the figure that I've got, and I don't want to do that. I want to compare these. So, you may remember the figure command.
[CLICKING] I can type figure and
I'll get a figure 2, or I can actually tell it I want figure 2. Either way, there's a new figure. And I can plot into it. [CLICKING] There. Let's make this one about the same size as the other one. Note that you can just change the aspect ratio too. And let's tuck them over here right side by side. So now we can see the whole parabola there. And you'll notice that MATLAB picks a range. It picked zero to ten here to get all the points. It picked 0 to 25 here. Well you might wonder, shouldn't this go from minus ten to ten? But no, all we did was give it the "a", which is these numbers here, and these indexes here, these indices, go from 1 to 21. And they just represent the indices of the elements in "a". To properly plot a mathematical function like this, we need to pass two vectors of the same length, one with the x values and one with the y values. Let's do that. [CLICKING] t is going to be my x values, and b will be my y values. So t goes from minus ten to ten. And b is just the squares of those numbers. So, let's get a new figure window. [CLICKING] And let's move it over here, size it the same as the others, and this time we're going to plot with two arguments. [CLICKING] There. The arguments of vectors, the t and the b, and they have to be the same length. And they give the horizontal and the vertical coordinates respectively. So now we see that the range, or actually the domain down here, goes from minus ten to ten, as we wanted it to do. And the range here, the y axis, goes from 0 to 100. Again, MATLAB picked these for us. It's also easy to extend this idea to put more than one plot in a figure. So sometimes you want to compare
two plots right in the same figure.
You can do this with plot because it's polymorphic. It accepts a varying number of arguments, as we've already seen. So let's do an example of that. We're going to plot x1 versus y1, and x2 versus y2 on the same plot. So let's get these vectors ready. There, I had these commands already typed out ahead of time, to save us some time. Here we've got x1, and I spaced this out so you can see it starts from 0. It goes in increments of 0.1, and it goes up to 2 pi. And then, the other vector, y1, is equal to the sine of these numbers. The sin is polymorphic, if you give it something of the shape of a vector, it returns something exactly the same shape. And that's great, because we want x1 and y1 to be inputs to plot, and they have to be the same length. Here's another plot, x2 and y2. X2 starts at pi over 2. It increments by 0.1 and it goes to 3 pi. And y2 is the cosine of those numbers. x2 and y2 have the same length, so they can be plotted versus each other. x1 and y1 have the same length, so they can be plotted versus each other. And both these pairs, even though they have different lengths from each other, can be plotted on the same plot. Let's do that. [CLICKING] Note that the order is x, y, that is, horizontal, vertical and x, y horizontal, vertical. And here goes our plot. Note that the plot shows up in Figure 3 in place of what was there because we didn't give the command, figure, again. Notice that they're two different colors. MATLAB picks the colors for you. There's a default set of colors. You can specify the colors, and we'll show you how to do that in just a second, but I want to point out that your version of MATLAB might use different colors. MATLAB just recently changed its default set of colors. In an older version, the first one would be blue, the second would be green. In this version, the first one's ...I would say... blueish and the second one is reddish. So let's change the colors to be...uh, let's make the first one bright red and
the second one black.
And let's make the second one a dotted line. We can do all this with one command. Let's get a new figure. [CLICKING] And I'm going to repeat this command that I just did, but I'm going to alter it a little bit. After the x1, y1, I'm going to insert a little short string. Quote, r, quote, comma. That's going to tell plot to plot this as a red line. And then here, for the second one, I'm going to put quote, k, colon, quote. This k is for black. Why not b? Well, b is already taken up for blue. And this colon means plot as a dotted line. So here we go. And let's make this a little smaller. And tuck it in over here. I guess I'm going to have to move all of these just a little bit. Nice and neat. So you can see that this is in fact, a red line. The default is that it's a solid line. And this is black. And if you look closely, you can see these dots. You can get lots more line styles with the command, help plot. Let's do that. [CLICKING] Now I'll scroll up a little bit. And there they are. Over here on the left, you see the colors. In the middle, you see the marks that you can, the markers, that you can plot, and, over here, lines. We haven't plotted any markers yet. Um, Let's do that. In fact, we can mix all three of these together. I want to plot an example with one from each column in Figure 1. Let's see, to do that let me hit return here. [CLICKING] I pick Figure 1. [CLICK] When I do that, you'll notice that Figure 1 got highlighted, and now I'm going to plot. [CLICKING] I'm going to plot t and b again. [CLICKING] I'm going to use an m for the color magenta,
[SOUND] a double dash for dashed line, and
and an o that can be upper case or lower case to put markers that are little circles on there. And here we go. Isn't that pretty? It's magenta, it's got the dashed line, and it's got this circular markers on here. Well, we've seen up here how to plot two things on the same figure. There's a other way to do that. You can call plot multiple times and use the function called "hold". Let's see how to do that. Let's do it in Figure 2. We first make Figure 2 the active figure with the command figure 2, so [CLICKING] There. You can also do that by clicking Figure 2 with the mouse. We notice that it's highlighted up here, relative to the other figures. Now let's replace the current contents of Figure 2 with a new plot. So we type a plot, using the same x1 and y1 that we defined earlier. And there it is in red, which is what we requested. Now we give the command, hold on. [CLICKING] Once that command is given, all subsequent plots to Figure 2, which is a figure that was active when we gave the whole command, will be made on the same axes in that figure. The previous plot won't be replaced. So let's do that. We'll plot x2 and y2 this time. And let's, as we did before, use a black dotted line. There. And notice that the range of the axes were adjusted accordingly. And just like when we plotted everything in one fell swoop with one call to plot. When we're ready to replace the current plot in Figure 2 with something else instead of adding to it, we give the command, hold off. When the command, hold off, is issued, all subsequent calls to plot while Figure 2 is active will replace the previous plot in that figure instead of adding to it. So watch Figure 2 while I replot x1 and x2 with, um, ...I don't know... green asterisks. [CLICKING]
Hm, that's a little garish.
Anyway, as we showed you in Lesson One, there are additional plotting options not reachable through the plot function itself that you can get via separate functions. These options will enhance the visual quality of a plot that's already plotted. Let's apply some of them to the plot in Figure 3. [CLICK] There, that's the active figure now. You may remember that grid turns on a graph paper like grid. Repeating the command turns it off and on. And like hold, it can be used with the terms, "off" and "on", as well, but we'll use it without this time. Let's see, grid, and you see this grid shows up here. And now grid again, [CLICKING] turns it off. And one more time, [CLICKING] because I want it on [CLICK]. There. Now it's on. And you may remember the "title", "xlabel", and "ylabel" commands. Let's let's do those again. [CLICKING] And there things are nicely decorated, as you can see here. Another handy function is the legend command. It'll insert a legend right there in the figure, with a note about each separate plot in the figure. Let's to do that one. [CLICKING] There up here you see a blue line here corresponding with this, or bluish, ...It's the same shade... and the word, "sine", and then the reddish line that corresponds to this one with "cosine". And the order of these is the same. The order here is the same as the order in which we plotted them. So what you just need to do is remember that the first thing we plotted was the sine, and the second was the cosine and the legends will be straight. If you want this legend to appear somewhere else, ...In this case, there was a nice blank spot up here. That's where it showed up... But if you want it here or here or somewhere else, even just outside here, next to the plot, you can do that.
You need to just check
the options in help legend. And finally, we'll remind you that you can control the range of the x and y axis, with the function named "axis". Let's do that. [CLICKING] Axis takes one argument, which is a vector of four numbers, with the left and right limits, right here, of the plot, followed by the lower and upper limits, right here. Please note that despite that fact that it affects both the x and y axis up here, its name is "axis"! I probably mistyped this command as a-x-e-s a hundred times. There is an axes command, but it's far more sophisticated than the work that one normally needs to do. You probably won't want to use it. Anyway, watch Figure 3 up here change when I hit return. Okay? One, two, three. [CLICK] There. As always, you can get more information about this function or any function with either the help command or the doc command. Okay, that's it for our second installment on plotting, except for closing the figures. You can do that with the function, close. Either one at a time, like this. [CLICKING] Figures 4 and 1 are gone, you can't get them back, except by repeating the commands that did the plotting and the decorating. And you can close all the open figures at once with the "close all" command, like this. Now we've got all this room I think I'll stretch the desktop back to full size. And I think I'll clear everything. [CLICKING] There, all done with plotting. [MUSIC] [APPLAUSE]