[MUSIC] Hi, and welcome to Lesson Four: Toolbox. This week, we'll be talking about the tools that a programming environment like MATLAB gives us to help create useful programs. These tools include the many built-in functions that MATLAB supplies. We've learned about a few of these already, and now we're gonna cover many more of them. We'll see how we can print on the screen in a nicely formatted manner, and how we can create graphical output, and perhaps most importantly, we'll see how MATLAB can help us track down and eliminate the errors that inevitably creep into our programs. Before we look at new tools, let's revisit one we've used already, the square root function. Suppose I wanna take the square root of nine. Well, we know what that is, three, no surprise there. Let me show you something that perhaps is surprising. I'm doing square root matrix. So, I'm gonna ask you to take the square root of matrix. What does it do here? Well, the matrix I gave it was a 3x2 matrix, 1, 4, 9, 16, 25, 36, I specified it right here in the input argument. And what square root does is go to each one of these elements, take the square root, and put it in the corresponding position in an output matrix that's the same size as the input matrix. So, we got square root of one is one, square root of four is two, nine goes to three, square root of 16 is four, 25 square root is five. So, this is an example of something that's not supported by all programming languages, it's polymorphism. If the type of an input argument to a function can vary from one call of the function to another, it's called a polymorphic function. Polymorphic means having multiple forms and polymorphism is a powerful feature, because it allows one function to handle a huge variety of inputs, which can save us a huge amount of work, because we avoid having to write a huge variety of functions. Furthermore, as we'll see, not only can the type of the input arguments vary, but the number of arguments can vary, as well, and we can make the function's behavior change when the type or number of arguments changes. Many built-in functions in MATLAB employ the first form of polymorphism by returning an output that's shaped like it's input. For example, as we've seen, if we call a square root function with a 3x4 matrix, it'll return a 3x4 matrix. We call it with a 1x2 matrix, it'll return a 1x2 matrix, and so forth. This shapeshifting of the output to match the input is, in fact, the predominant form of polymorphism in MATLAB, and it's very convenient. But not every built-in function behaves that way. We're gonna look at one right now that doesn't. The function sum doesn't behave this way. Let's make a vector, v, and put some elements in it. Doesn't make a difference what it is. And let's give this vector to sum. There, I guess that's right. Yeah, it sums up the elements of a vector. Now let's give it a matrix, let's make the matrix A equal to one, two, on the first row, three, four, on the second, so there's A. Let's give it, sum A. When you give a matrix to sum, it returns a row vector, and each element of it is the sum of one column in the matrix. So one plus three is four, two plus four is six. However, when you give it a vector, like we did up here, it's vector, v, it returns a scalar that's equal to the sum of the elements of the vector. So sum is polymorphic, but the output that it returns doesn't have the same shape as it's input. You've seen how to make a function return more than one argument. Well, some built-in MATLAB functions do that, the maximum function, for example. Here we gave max a four element row vector, and it returned the maximum number in that vector element that's equal to eight. Now, let's ask max to give us two outputs, let's see what happens. I gave it the same input. This time it gave us an eight and a four. The eight is still the maximum number. The four is the index of the maximum number, so the fourth element here was where the maximum was, so it returns a four. Note the returning two output arguments is not the same as returning one output argument that is a vector with two elements. Size does that. S is a vector. It has two elements, 3 and 2. This is a 3x2 matrix, so it returned that vector, 3, 2. And when I say it's a vector, just to establish that, here's the first element. There's the second element. The function max wouldn't allow us to catch both outputs as a single vector, but size does. However, the function size makes it possible also to save the elements of it's output in another way. Like this. This time, it put the number of rows in the first output argument and it put the number of columns in the second output argument. So, size behaves differently depending on the number of output arguments we ask for. This is another example of polymorphism, polymorphism based on the number of outputs requested by the caller of the function. Note that if you want to write a function that has these polymorphic properties, you know, the ones that depend on the number of output arguments, you have to write it that way. It's not something MATLAB does for you. We'll see later how you do this. [MUSIC] [SOUND] [APPLAUSE]