[MUSIC] Hi and welcome to Lesson 4, 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, but now we're going to 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, lets revisit one we've used
already, the square root function. Suppose I want to take
the square root of 9. Well, we know what that is, 3. No surprise there. Let me show you something
that perhaps is surprising. I'm giving square root a matrix. So, I'm going to ask it to
take the square root of matrix? What does it do here? Well, the matrix I gave
it was a 3 by 2 matrix. 1, 4; 9, 16; 25, 36. I specified it right here
in the input argument. Well what square root does is go to each one of
these elements, take the square root, and put it in the corresponding position in
a output matrix that's the same size as the input matrix. So, we've got square root of 1 is 1,
square root of 4 is 2, 9 goes to 3, square root of 16 is 4,
25, square root of 5. So, this is an example of something that is 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 functions'
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 the
square root function with a 3x4 matrix, it will return a 3 by 4 matrix. If we call it with a 1 by 2 matrix,
it'll return a 1 by 2 matrix and so forth. This shape shifting 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'll look at one right now that doesn't. The function sum doesn't behave this way. Let's make a vector, v. Let's 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. Two, three, four, yeah. It sums up the elements of the vector. Now let's give it a matrix. Let's make the matrix A. You could put 1, 2
on the first row, 3, 4 on the second. So there's A. Let's give 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 of the matrix. So, 1 plus 3 is 4. 2 plus 4 is 6. 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 its input. We'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, the element that's equal to 8. Now let's ask max to give us two
outputs and see what happens. I give it the same input. This time it gave us an 8 and a 4. The 8 is still the maximum number. The 4 is the index of the maximum number. So the fourth element here was where
the maximum was, so it returns 4. 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 3 by 2 matrix, so
it return that vector, 3, 2. And when I say 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 its 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] [APPLAUSE]
Btw. this is a lecture from free Coursera course on MATLAB from Vanderbilt university