0:26

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.

0:49

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.

1:04

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.

2:40

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.

3:38

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.

3:53

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.

4:46

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.

5:10

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.

5:46

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.

6:20

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]