Dr. Robert van de Geijn: Let's implement our first function.
It's going to be a very simple one that copies a vector to another vector.
We're going to implement it using M-script,
which is the language that MATLAB uses.
Now, realize that I'm making this video as the materials are still
under construction, so it may all look slightly
different from what I'm showing.
But you'll get the gist of it.
So let's get started on the first programming assignment, which
is to create a function in M-script that copies one vector to another.
We have rather detailed instructions for this.
So if you go to the notes and you find this homework,
and you click on LAFF programming supplement, you will get there.
And it'll look something like this.
Now, what I want you to do is to start up MATLAB.
And what you'll see is something like this.
And you may have to navigate to the particular sub-directory that
has the programming assignments in it.
We talked about that in Week 0.
And to do so, you may have to browse for the folder to get there.
Anyway, let's instead start right here.
Now the instructions say to start by creating two vectors, x and y.
So let's go to MATLAB.
And in the Command window, which is this window right here,
let's create a vector x, 1, 2, 3.
And a vector y, 0, minus 1, minus 2.
And you can always look at the contents of vectors x and y again
by just saying x, and it'll print it.
Now what we're going to do is we're going to back up these vectors.
So we're going to say x_old is equal to x.
And notice that, in the process, we're copying one vector to another.
So in some sense, intrinsic in MATLAB is already
the ability to assign one vector to another.
But we're going to write their own function,
because we're going to learn from doing so.
So we set x_old to x.
And we can set y_old to y.
And notice that it's a little annoying that every time the contents are
printed.
And you can suppress that by putting a semicolon at the end of the line.
If you now look at y_old you see this has the contents of y.
Now to copy one vector to another, what one can do
is simply assign an element of x to the corresponding element of y,
one at a time.
Now, to access an element of x, you say x of 1.
Now, recalling that x is 1, 2, 3, x of 1 is the first element of x.
The first element of y has content 0, because y
had the contents 0, minus 1, minus 2.
And what you notice is that M-script starts indexing at 1.
While we would have called y of 1, actually, Psi
sub 0 in our notation in our course.
And that's just because we choose to start indexing
at 0, as computer scientists tend to do, while MATLAB starts indexing
at 1, which is what mathematicians tend to do.
OK, so here we have it.
And to now assign the first element of x to the first element of y,
we could do this.
What you notice is that it's being overwritten.
And then we could do the same thing for the second elements,
and for the third elements.
So let's reset y to the original value.
Obviously, if these vectors were of length 100,
this would have been rather tiresome to do the copy one at a time.
And for this, M-script has something that's called a for loop.
And what you can say is, for indices i from 1 to 3, set y of i
equal to x of i.
And then you have to say, "end", which is the end of the span of the for loop.
And what happens is that, one by one, the elements of y
are overwritten with the corresponding elements of x.
Let's reset y to y_old.
We could have done the same thing, suppressing
the printing of the intermediate results by saying y of i
is equal to x of i semicolon.
And then if we print out y, you see that it has been overwritten.
Now again, obviously, every time you want to copy one vector into another,
it would be a little bothersome to have to write this kind of for loop,
and therefore what we're going to do is we're going to create a function.
Now to do that, we're going to start by going into sub-directory programming,
and into sub-directory Week 1, which is where we're
going to put the files that we're going to create in week 1.
And we're going to go up here and say, new function.
And what we get here is an outline for a function.
Notice that it says function.
Then some output arguments, some name of a function,
and then some input arguments.
The name of the function we're going to choose to be, laff_copy.
The input arguments are going to be both x and y,
and the output argument is going to be y sub out.
And then you really should have some comments here
that describe what the function does.
Let's save this.
And we're going to save this into a file, laff_copy.m
Notice that that's what the MATLAB files have as an ending.
And you say, Save.
And now over here, we could actually call this function.
So let's reset y to be y_old.
And we could now say, let's assign to y out the copying of x into y.
But notice that we're actually saying, put the result into vector y out.
And actually, if we do this, it protests.
Because it says, wait a minute.
As part of this function, we're saying that we're
going to create an output variable y out,
and we never assign anything to it.
And that just looks a little hokey.
So what we're going to do now is we're going to say,
let's set y out to be a vector of zeros that are 3 by 1.
So we're actually viewing column vectors as 3 by 1 arrays.
Let's then assign, for i equals 1 through 3,
to y out of i the contents of x of i.
And notice we're not even using the input variable y.
What's going on with that will become clear
as you go through the programming supplement in more detail.
Now I like to put a return at the end here
to indicate that we are returning from the function.
We now save this, and I'm doing that with Command S,
or I can just say, save.
Then, if I now say, y out is equal to laff copy x comma y,
notice that y out ends up being overwritten with the contents of x.
But I could just as easily set y equal to laff copy, x, y,
in which case y gets overwritten.
Let's reset y to y_old.
And if we now do this, but we put a semicolon at the end-- ah,
then notice along the way, the intermediate values of y
out are being written.
And if we then say y, we also see that y has been updated.
So what we really want to do here is we want
to put a semicolon at the end to suppress
the printing of intermediate results.
Let's save this.
And now what we have is a routine that can copy a vector of length 3
into another vector of length 3.
Obviously, we probably want a more general function
that can copy a vector of length n into a vector of length n.
So let's change this now.
OK, for that we're going to use the M-script function size.
If you look at the size of x, it tells you that it's a 3 by 1 array.
That's because we view a column vector as an n by 1 array.
And what we can do is we can extract the first and second dimension
of the vector, as such.
And what that then does is it returns the row and column dimension.
And then we can use that in our function by saying let's let m_x, n_x.
equal to the size of x, semicolon to suppress printing.
Let's let our output vector be of size m_x by 1,
and then for i equals 1 to m_x, let's set
the i entry equal to the corresponding entry in x.
And if we now save this, then we have a function that can copy, in general.
If we now set x equal to 1, 2, 3, 4, and we set y equal to 0, minus 1, minus 2,
minus 3, and we call the copy routine-- let's see,
I'm going up here with the up arrow.
Then notice that the vector of length 4 has been copied from x into y.
Now it turns that we really want a function that
can copy a row vector into a column vector,
or column vector into a row vector, et cetera, et cetera, et cetera.
And the rest of the programming supplement on this particular homework
problem walks you through all of those steps.
I'm just going to let you do the rest of that on your own.