[BLANK_AUDIO] Hello everyone. Welcome back. This is our first lecture that is going to contain a significant amount of content in it. And this is the first lecture of four that's going to teach you how to, perform computations using the MATLAB programming environment. MATLAB is the program we're going to use to implement and to analyze our mathematical models. So we're going to start off by showing you some of the basics of MATLAB, showing you how to use this, this software package and like I said this is the first lecture out of four. They're going to focus on computing with MATLAB. So the topics that we're going to cover in this first section out of four are the very basics of MATLAB. We're going to talk about how to define vectors and arrays to hold data. I'm going to show you how to do basic calculations such as addition and subtraction and then we're going to talk a bit about the difference between array computations vs matrix computations in the MATLAB programming environment. So the format we're going to use for these first, these slides in this first lecture are, I'm going to show some text that was generated using my own MATLAB command window. And we're going to start by talking, telling you how to define variables in different forms and simple arithmetic. And the, syntax that we're going to use is that MATLAB commands are going to be shown in black Courier font, so these were MATLAB commands that were typed into my MATLAB command window. And then have been pasted here into the power point. And then, where I'm going to illustrate some comments on this, where I'm going to explain some things about these MATLAB commands, that's going to be shown in the red Arial font. So if you follow along the, the different fonts, you can tell whether something came directly from MATLAB or whether it's just some, some comments and some annotation on those MATLAB commands. So we're going to start by just showing some very basic, commands and we're going to go relatively quickly through these. One of the first things you might want to type is this command format compact. Normally MATLAB spits out a lot of information about the variables that you define and it includes a lot of spaces and this is a way to help, so that you can have more, information on your screen and not as many spaces if you type format compact. So you can start with some, some real basic commands in MATLAB, a equals 4. That just defines the variable a with the value 4. Similarly, b equals 3. Now that you've defined a couple of variables, you can start manipulating them. For instance c equals a plus b. C will be 7. Or d equals a times b, then d will of course be equal to 12. Or instead of using, defining d by multiplying, you can define d equals a divided by b, and in this case d will be equal to 1.333. One of the things that you'll notice if you're familiar with programming languages is that, a and b do not have, you know, because we've defined them as integer values, 4 and 3, we can still perform a division here, and MATLAB will automatically, keep track of this and say okay, well these are no longer integers. Now, d is equal to something that has a, has a fraction associated with it. And these comp commands to add, multiply, et cetera are all variant to what if. Now, what if we wanted to define a variable in MATLAB that wasn't equal to just a number? In other words, it wasn't just a scaler but it was equal to a whole series of numbers? Well in that case we would want to define this variable as a vector or an array. And that's one of the great strengths of the MATLAB programming environment is it handles vector operations and array operations, very easily and very well. So now I'm going to show you some examples of how we can define one dimensional vectors in MATLAB. You could type in a equals, and you'd have to have this square bracket in the beginning and a square bracket at the end and then in between you'd type 1 comma 2 comma 3 etc. All the numbers up to 10. And now a, instead of just being equal to four like it was in the last slide, now a is equal to all the numbers from 1 to 10. [BLANK_AUDIO] You could get the same answer if you typed in a equals one colon, comma. One colon ten. So what that does is it has evenly spaced integers from whereever you start up to wherever you end. See all the numbers from one to ten, just like you had up here, but you didn't have to type in every single one of the values. Now you say, what if you wanted to, if you didn't want them to be spaced, with a spacing of one, what if you just wanted odd numbers, or what if you just wanted even numbers? You could type in a equals 2 colon 2 colon 20 and that would give you just the even numbers between 2 and 20. 2, 4, 6, 8, 10, et cetera. So this middle number here, in between 2 and 20 defines the spacing. Start at 2, end at 20 and the spacing in between them should be 2. Conversely you can have descending numbers if you put a negative number in the middle. So instead of saying 2 colon 2 colon 20, if you add 20 colon minus 2 colon 2, then you would get the numbers descending from 20 down to 2 with a spacing of, of minus 2 in between them. Couple other useful MATLAB commands, if you wanted to a series of, of 10 numbers but to have them not, be all the same, rather than, being in, in an ascending or descending order, the ones command, gives you a bunch of numbers. All are equal to one, if within the same vector. You could have ten zeroes instead of ten ones or you could have ten random numbers. In this case, they're uniformly distributed random numbers, all between zero and one. And these, these numbers here are 1 colon 10 tells us the dimensions of the, of the vector or the array that we're going to end up with. And what this means is one row, ten columns. So everything we've showed on this slide has one row and ten columns. Now we're going to talk about, next, we're going to talk about variables that might have different numbers of rows and, and columns. [BLANK_AUDIO] Now we're going to show how you can define and manipulate multidimensional, arrays and matrices. So everything on the previous side was, had dimensions one row and ten columns. So it was a vector. Now we're going to talk about multidimensional arrays. So you could type at the MATLAB command window, a equals square bracket, 1 comma 2 comma 3 semicolon 4 comma 5 comma 6. This example illustrates two important points. One is that MATLAB is case sensitive. So in the previous slide we defined variables with little a, now we're defining it with a big A, MATLAB will recognize these as two separate variables. So, you need to keep that in mind and you need to keep track of what you mean when you use lowercase, letters and what you use, what you mean when you use uppercase letters. And the second important point this illustrates has to do with the semicolon here in the middle. A will be defined like this: 1, 2, 3 on top of 4, 5 and 6. So now we don't have all these, these six numbers all in a row. We have first three numbers in the first row and then next three numbers in the second row. So what the semicolon here, indicates is that we should have vertical concatenation. And that means move to a new row. So the comma like we showed before is for horizontal concatenation. Put a bunch of numbers next to one another. The semicolon is used for vertical, vertical concatenation, put a set of numbers on top of another set of numbers, move to a new row. So rather than having to type in all the numbers individually, we can combine this vertical concatenation with, the colon that we used before for evenly spaced numbers, 1 colon 3 semi-colon 4, colon 6, another semi-colon, 7, colon, 9. That will form an array that looks like this. All the numbers 1 through 9 can be arranged in a three by three array, and that would be the, variable that we defined as b. Now that you've defined A and B as arrays you can, you can manipulate them and you can combine them with each other. For instance, you could say, C equals A semicolon B. That's going to take the array A, and vertically concatenate it with the array B, so we'll end up with something that looks like this. These six numbers here, this two by three array is what we had A, and that's on top of this three by three array, B, and they're all together in a five by three array that we call C, that concatenates two matrices together. Now what if we take instead, C equals A comma B? In that case, we get an error and it would say this, error using horzcat. CAT arguments dimensions are not consistent. What that means is that we've tried to horizontally concatenate a two by three array, A, with a three by three array, B, and that's not possible because A has two rows, and B has three rows, and therefore they cannot be combined horizontally and so that's what this error here indicates. It says you can't horizontally concatenate two things, two objects, two variables that have different numbers of rows. [BLANK_AUDIO] Now we'll show some more examples of, of concatenation using MATLAB. What if we typed a equals 1 colon 3, we have the numbers 1 through 3. Then we could type a equals close bracket, square bracket, a comma, a. We would get this answer here: 1, 2, 3 next to 1, 2, 3. Now, for you know, this is not a formally mathematically correct, statement, right? A cannot be equal to a and a, together. This is the difference between formal mathematics and, and programming languages. What this is saying here in MATLAB is you already have something called a, and you can redefine a, you can define it to be something new, based on the previous value of a. So it is okay to have a on both the left-hand side of the equation and the right-hand side of the equation. These don't have to be exactly equal to one another. The whole idea is that what you get on the left-hand side is what you get new and the right hand side is something that you already have. So variable a can appear on both the left and right hand side. This is a way of defining the new value of a based on the old value of a. So we can also say a equals a comma 9. That's going to take our previous version of a 1, 2, 3, 1, 2, 3 and then stick a 9 on the end of it. So now we have an, a variable that has one row and seven columns. We can also do vertical concatenation of this. That's going to take our one by seven, array, one by seven vector, one by seven array, called a and put it on top of itself. So now a has two rows and seven columns. And the reason I keep, showing these, and I keep redefining a is to illustrate an important point here, is that after each one of these commands, the previous version of a is lost. So, if we type in these commands sequentially, a is going to be this object here, which is an array with two rows and seven columns. MATLAB has no way of knowing at this point that it used to be equal to 1, 2, 3. Or that it used to be equal to this one by six array here. The analogy to use here is if you're, were editing a document using Microsoft Word and you just called it document or you just called it, mytermpaper.doc, or something like that. Every time you, you went in and edited it, whatever you had before would be lost. So if you typed up a paragraph that you thought was very eloquent, you went back the next day and you edited it, and you didn't like the new version, you said, I want to go back to the old version, well you wouldn't be able to do that, right? Because whatever you had before is going to be lost if you keep saving it to the same file name. Here, we keep saving this to the same value, the same variable name, which we're calling little a. If you wanted to, keep your previous version of your term paper you would call it my term paper version one, my term paper version two, my term paper version three, et cetera. So analogously in MATLAB you could keep, redefining it into a new variable name. If we had a equals 1, 2, 3 and then we said b equals a comma a then b would be equal to this one by six vector here and we'd still have our old value of, of little a. So now the original definition of a is maintained, so as you're, as you're programming in MATLAB, you need to think about this, you know, do I need to keep the old version of this, or is it okay to overwrite it and just throw away the old version of, of my variable? In some cases you might want to do it one way, in other cases you might need to do it the other way. Now let's talk about some of the differences between matrix arithmetic verses array arithmetic in MATLAB. If you have a two by three or A, A with numbers 1 through 6 arranged like this sd we've seen and then you have a second two, two by three array called B with six different numbers, it's easy to perform manipulations,uh, arithmetic manipulations on arrays A and B. For instance you could say C equals two times A, and that will just multiply two times each element of A. You could say D equals A times three, sorry A plus three. That will add three to each element of A. And then you can combine these kinds of commands, you can say E equals two times A plus B and what you'll see here is you have 2 times each element of A, plus the corresponding element of B. For instance you have 2 times 1 is 2, plus 4 equals 6. 2 times 2 is 4, plus 1 equals 5. Alright so that multiplies each element in here by 2 and adds, to the, array of B element by element. Now here things get a little more complicated. What if you wanted to say, F equals A times B? In this case you would get an error and the reason you would get an error is because you're trying to do a matrix multiplication. So that's what this error message means. Error using mtimes or mtimes being the shorthand for matrix multiplication. And the reason you get this error is because the inner matrix dimensions must agree. So A and B are each two by three arrays, and, two by three matrices cannot be, multiplied using formal matrix multiplication. And in, pretty soon we'll talk about the difference between array multiplication and matrix and multiplication and what matrix multiplication, MATLAB actually means. Now you might not want to do a formal matrix multiplication, in fact a lot of the times you don't. What you might want to do instead is take 1 times 4, 2 times 1, 3 times 7, you might want to multiply two arrays on an element by element basis. And you can do this in MATLAB, but you have to add something to your multiply command. You have to add a dot. If you say G equals A dot times B then you're going to get this element by element multiplication, 1 times 4 is four, 2 times 1 is 2, 3 times 7 is 21, et cetera. So this is a very important point in MATLAB. If you just do a straight multiplication, MATLAB is going to interpret that as a matrix multiplication. If you do a dot times matrix will, sorry MATLAB will interpret that as a, an array, or an element by element multiplication. Now something else you can type is H equals A greater than B. This is a, another type of command that is, not formally mathematically correct. In formal mathematics it doesn't make any sense to say is one array bigger or, or smaller than another array. But the way MATLAB interprets this is on an element by element basis. So what you get when you do this A is greater than B is a bunch of ones and zeros. Every time an element of A is not greater than the element of B, for instance 1 is not greater than 4, you get a zero. A zero meaning false, in this case. But when an element of a is greater than the corresponding element of e, for instance, 5 is greater than 2, then you get a 1 here. So, greater than, less than, et cetera. We'll compare, the, the 2 arrays on an element by element basis. So these are some of the differences between matrix, arithmetic and array arithmetic in MATLAB. If you perform several computations in a row in MATLAB it can be difficult to keep track of all the variables you've defined and what the dimensions of each variable are and what each variable represents. In that case a command that's very helpful is what's known as the, the whos command. This will list all of the currently defined variables. For instance, if you've typed, a series of commands similar to the ones we, we just showed you and you type whos you may see these are the, the names of, these are the, the names and these are the dimensions of all your variables. And when you get an incompatible, incompatibility error, it can be useful to type whos to figure out why you got an incompatibility error. For instance you could say, well, A, capital A in this case is two by three and little a is two by seven. So of course I am not going to be able to multiply those two. And this is a way to, for you to determine when, where you may have done something wrong. For instance look, you know, you can look at little a and you could say well I didn't mean to have it, for it to have more columns than rows, I meant for it to have a lot of rows and only a few columns. So obviously I did something incorrect when I, when I defined the variable little a in this case. So the whos command is good to check up every so often. To look up, look up all your variables and what their dimensions are. Now let's talk about how you can access sections of arrays. As usual MATLAB commands are going to be in black Courier, comments are going to be in, in red Arial font. So if we have a, a five by three array called capital C that has this sort of structure here, we could type little e is C parenthesis, 1 comma 1, end parenthesis. And we would get this result. E is one. Why do we get this result? Well, this tells us to look at the erase C. Look at the first row of C and the first column of C. And if we look at the first row and the first column we see lesson number one, right there. We could also say little f is c five comma three. That would be fifth row, 1, 2, 3, 4, 5. Third column, 1, 2, 3. And so therefore we would get f equals 9. Either one of these will access a single element of the array. If we wanted to access more than a single element. If we wanted to access a row or a column, we can type this. D equals C colon comma 1, so it's like what we did here with 1 comma 1, except we replaced 1 with a colon. What do we get in this case? We get a 5 by 1 vector, 1, 4, 1, 4, 7, which we can see is the very first column of C. So what, we, what this colon does is it says, access all the rows and then this 1 says access the first column. So that's a way to access an entire, entire column of an array. And we can make this more complicated. We can say E is C colon 1 comma 3, which will give us a five by two array, which has the first column and then the third column of C. Another useful, keyword that MATLAB uses is what's called end. Let's say you didn't know exactly how, how many elements you had in your, in your array C but you knew that the very last row or the very last column was important. So in this case if you said capital F is equal to capital C end comma colon this is saying take the very last row and take all the column. So what we would get with capital F is 7, 8, 9. Which we look back at capital C, we see 7, 8, 9 is the, the very last row of F. Or we could say something like C 2 comma 5 and comma, 1 colon 2. So this is take the second row and the fifth row, and then take the first column up to the second column and this is what we get. 4, 7 is the first column, 5, 8 is the second column. Finally, if we said capital H equals C, 1 comma 5, 1 comma 5 in this case we would get an error. The reason we would get an error is because 1 through 5 is fine for, specifying the rows of C, but how many columns does C have? C only has three columns. So, it doesn't make sense to say the first column all the way up to the fifth column. There is no fourth column and there is no fifth column. And that's when you're going to, why you're going to get an error and the error will say index exceeds matrix dimensions. [BLANK_AUDIO] Now let's go back to the, that error that we saw to do before, when we tried to do a matrix multiplication. Remember when we had A as a two by three array and B as a two by three array, and then we said capital F is equal to A times B. We got this error here. Error using mtimes. Inner matrix dimensions must agree. Let's, now let's understand the basis for this error. [NOISE] [BLANK_AUDIO] Now we can understand how matrix multiplication works mathematically and how it works in MATLAB. Again if we have A with, as a two by three array, and B as a two by three array, then we can not multiply a two by three array, by a two by three array. So since A and B are both two by three in this case, A times B is undefined. We'll get an error is we try to do that. But what if we took b and converted it to a three by two array. One way we could do that is by using a command known as transpose. When we type B apostrophe here, B apostrophe means take the transpose of B, which in practical terms means turn a row into a column, turn each row into a column and turn each column into a row. So B apostrophe or B transpose would look like this. You see 4, 1, 7, which is the first row of B becomes the first column of B transpose. And 9, 2, 3 becomes the second column of B transpose. So, in this, now in this case, A is two by three, and B transposes three by two. And remember that for, matrix multiplication to occur, the inner dimensions, matrix dimensions, must agree. So you can take a two by three and multiply it by something that's three by two. So you can compute A times B transpose. And if we do do that computation in MATLAB, F equals A times B transpose, we don't get an arrow we get an answer. And we get this answer, F equals this two by two array, 27, 22, 63, 64. We can understand each one of these elements as follows. 27 in this case is 1 times 4 plus 2 times 1 plus 3 times 7. So you take this first row of A, 1, 2, 3 and you multiply it by this first column of B transpose 4, 1, 7. And you sum up all the products. So it's 1 times 4. 2 times 1. 3 times 7, right? 1 time 4 is 4. 2 times 1 is 2. 4 plus 2 is 6. 3 times 7 is 21. 6 plus 21 equals 27. That's why we got a 27 for the first row and first column, of F in this case. And all the other elements of, of F are computed similarly. [BLANK_AUDIO] So we can understand matrix multiplication a little bit more generally by, specifying a couple of rules that govern matrix multiplication. One, as we said, is that multiplication can only occur if the inner dimensions agree. And furthermore, what's the, what are the dimensions of our product going to be? If A is an n by m matrix, and B is an m by p matrix, then the product A times B is going to have dimensions n times p. So, the n and the m have to match. And then what you have, what you start with is the number of rows you start with in the first matrix. And the number of columns you end with in the second matrix, are what you're going to get in your product, n times p. So we can visualize this as follows. You can take something that's tall, and skinny, multiplying it, by something that's short and pretty fat, and you can end up with a product that's short, sorry that's tall and fat. For instance a fifty by ten matrix times a ten by forty matrix is going to yield a fifty by forty matrix. Or with another example, you can take something that's kind of short and fat, multiply it by something that's tall and skinny and end up with something that's relatively short and skinny. So if you started with something that was twenty by fifty, multiplied it by something that was fifty by eight, you would end up with a product that was twenty by eight. So this is, these are the rules for a matrix multiplication. And these are some examples of the things that you can get when you do real matrix multiplication. This is true using MATLAB and then, this is just true in general, in terms of the rules of mathematics. [BLANK_AUDIO] So in summary, what we've seen in this first lecture are that variables in MATLAB can be either scalars, vectors, or they can be arrays. And array dimensions influence the types of operations that can be allowed. For instance, arrays can only be added, or multiplied, or concatenated if the dimensions match. And we saw some examples of where these dimensions didn't match and where MATLAB gives you errors. And then the other key point that we're going to come back to we, we saw that for the first time in this lectures, in this lecture, is that certain symbols, such as the colon, the semi colon and the period have special meaning in MATLAB. And we'll come back to those, those meanings later. [BLANK_AUDIO] [NOISE] So now that we've finished this lecture, let's move onto a couple of self-assessment questions. This is a good way for you to, to, you know, take a look at the questions, see if you can answer it and check to see how well you're understanding, what we've covered. So, you can either do these questions now, or, if you feel like this was, some of this was kind of confusing, maybe you want to watch the lecture a second time and take the self-assessment question after watching the lecture for a second time. So, the first question we've devised for you is as follows. In analyzing data, you have to, to find an array called capital A. This array has 32 rows and 15 columns. Now you want to type a command to access a subset of A and this is what you type. Capital B equals A 20 colon end comma and then square bracket 1, 3, 6, 11 end square bracket, end parenthesis, semicolon. And so the question now is, what are the dimensions of your result, your subset, capital B? I'll give you a couple of seconds to think about that and then I'll come back and show you the answer. [BLANK_AUDIO] Okay. Capitol B, in this case, is going to have dimensions thirteen by four. So the answer is C. B has these dimensions. How do, how do we determine that these are the correct dimensions of B? Well, we looked at the number of rows. And the rows are specified by 20 to end. Sorry, 20 to 32. And we're told that, 20 to end, and we've been told that A has 32 rows, so what 20 to end means in this case, 20 to 32. How many elements are there in the, vector that defines all the numbers between 20 and 32 inclusive? Well that's going to have 13 elements in it. So we know that B is going to have 13 rows. What about the number of columns? Well, the number of columns is specified by this array here, 1, 3, 6, 11. And this vector has four elements in it. Therefore, B, capital B is going to have four columns. So that's how we know that C is the right answer in this case. [BLANK_AUDIO] Okay. Now let's move on to self-assessment question two. What if you defined two arrays? A equals 1 comma 5 comma 2 comma A. And B is 1, 2, 3, semi colon, 4, 5, 6. Then you attempt to type C equals closed square bracket, A semi colon B. And you're going to get an error, and the question is, what kind of error do you receive and why? I'll let you, give you a chance to read the, read the choices. You might want to pause the video in this case and then we'll come back and discuss the answer to this one. [BLANK_AUDIO] So the error you receive in this case is E. Error using vertcat because A and B have different numbers of columns. So let's look at our, how we defined A and B and figure out what dimensions they have. Well, A has dimensions one by four, right? One row and four columns. B has dimensions two by three, two rows and three columns. Remember that when we define B, the semicolon in this case, indicates vertical concatenation. So, it says take this first, these first three numbers and vertically concatenate it with the second set of three numbers. So then when we try to define C, we're trying to vertically concatenate A and B. So that's how we know that we're going to get an array using vertical concatenation rather than either choice A, choice B or choice C for horizontal concatenation or M times. And we can only put two arrays on top of one another if the number of columns is the same, and in this case A has four columns and B has three columns and that's why we get an error using vertcat in this case. [BLANK_AUDIO]