[MUSIC] Well, while we are on the subject of 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 gonna go over it again with a bit more detail this time. The easiest way to use the flat function is simply to pass a vector to it. So let's get a vector This is just a list of the squares of the numbers from one to ten. And plotting it 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. And it's labeled figure, figure 1. We docked it last time, like this, it shows up right here. So now we've got the command window visible. But I'm gonna undock it. Now, that covers up the Command Window, but if I click in the 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. Then I'm going to resize this figure window and put it up here. 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. So this is half of a parabola. To plot both sides of the parabola, we can do this. So there is a little longer vector it's the squares of the numbers from minus ten to ten. Now I wanna plot this one on a separate figure. If I just plot now it will replace the figure that I've got and I don't wanna do that. I don't wanna compare these. So you may remember the figure command. 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. There. Let's make this one. About the same size as the other one. But 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 parable there. And you'll notice that mat lab picks a range. Pick 0 to 10 here to get all the points. Pick 0 to 25 here. Well you might wonder, shouldn't this go from -10 to 10? 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. T is going to be my X values, and B will be my Y values, so T goes from -10 to 10, and B is just going to be the square of those numbers. So let's get a new figure window. And let's move it over here. Size it same as the others. And this time we're gonna plot with two arguments. There. The arguments are 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 10 to 10, as we wanted it to do and the range here, on 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 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 x 1 versus y 1. And x 2 versus y 2 on the same plot, so let's get these vectors ready. There, I have these commands already typed out ahead of time to save us some time. Here we've got x 1 and I space 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 sine 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 in 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. X 1 and y 1 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. 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 three 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 wanna point out that your version of MatLab might choose different colors. MatLab just recently changed it's default set of colors. In an older version, the first one would be blue, the second one would be green. In this version, the first one's, I would say, blueish and the second one is reddish. So let's change to colors to be, 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. And I'm gonna repeat this command, and I just did, but I'm going to alter it a little bit. After the x1 y1 I'm gonna insert a little short string, quote r quote comma. That's gonna 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 plotted is 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 a little bit. Nice and neat. So you can see that this is in fact a red line, the default is that it' a sodded line, and this is black. Maybe look closely you can see these dots. You can get lots more line styles with the command help plot. Let's do that. Now 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. 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, hit Return here. I'll pick figure 1. When I do that, you'll notice that figure 1 got highlighted. And now I'm gonna plot t and b again. When you use an M, for the color magenta. A double dash for a dash line. And an o, that can be upper case or lower case, to put markers our 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 the circular markers on here. We've seen up here how to plot two things on the same figure. There's another 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 There. You can also do that by clicking figure 2 with a mouse. We noticed that it's highlighted up here relative to the other figures. Now let's place 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. Once that command is given, all subsequent plots to figure two which is a figure that was active when we gave the whole command. Will be made on the same axils on that figure the previous plot won't be replaced. So let's do that. We'll plot X 2 and Y 2 this time and let's as we did before use a black dotted line. There. And notice that the range or the axes were adjusted accordingly. And just like when we plotted everything in one fell swoop with one called plot. When we're ready to replace the current plot in figure two with something else instead of adding to it. You give the command hold off. When the command hold off is issued, all subsequent calls to plot while figure two 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 I don't know. Green asterisks. 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 plot in figure three. There that's the active figure now. You may remember the 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. Turns it off. And one more time because I want it on. There now it's on. And you may remember the title x label and y label commands. Let's Let's do those again. 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 do that one. There, up here you see blue line here corresponding to this. Blueish it's the same shade and the word sine then the reddish line that corresponds to this one with cosine. 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 a sin and the second was a co-sin and the legends will be straight. If you want this legend to appear somewhere else, in this case it 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 axises with a function named axis. Let's do that. 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 the fact that it effects both the X and Y axes up here. It's name is axis. I probably mistyped this command as axes a hundred times. There is an axis 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 three up here change when I hit Return. Okay one, two, three, there. As always, you can get more information about this function or any function with either the help command or the dot 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 Figures four and one 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 a Close All command, like this. Now that we've got all this room, I think I'll stretch the desks top back to full size. And I think I'll clear everything. [MUSIC] There, all done with plotting. [MUSIC]