Hello everyone. We are now onto the third part of our lecture on how to use the MATLAB programming environment to form scientific computations. So this is computing with MATLAB, part three. In some ways this is going to be a continuation of the previous lecture, where we discussed strategies for using MATLAB for data manipulation and analysis. But we're going to have a different focus compared to that last lecture. In this lecture, we're going to discuss several useful MATLAB built-in functions, such as those you can use to compute the mean, the maximum or the standard deviation of data that you may have. And we're going to particularly focus on the way that you can use MATLAB to determine the array locations that fit particular criteria. We'll get to that a little bit later, but what I mean by that is sometimes you don't care about the maximum value itself, sometimes you care where in your array does the maximum value occur. We're going to talk about how you can use that functionality of MATLAB. We're also going to discuss strategies for using if statements and then we're going to talk about exporting and importing results in MATLAB. So, several useful MATLAB built-in functions can be summarized as follows. Let's say that we have a one by five array, little a equals these five numbers. one, five, two, four, three. We can compute little b equals the mean of a. And for all the numbers one through five, the mean value in this case is going to be three. That computes, mean computes the average. We can compute little c equals std of a. That will give us this number, c equals 1.58. And what this is doing is computing the standard deviation of all the values in a. Or we could say, little d equals the max of a, that's going to give us the maximum value in this one by five array, which in this case is, of course, going to be equal to five. Or you could say, little e is equal to the sum of a. That's going to sum all the values one through five, which is going to give you a value of 15. Now if we said little f equals the square root of A, in this case we're not going to get a single number like we did with these other functions, we're going to get another one by five array, with these five numbers here. So what this function is doing, is computing the square root, but it's doing it on an element-by-element basis. Or you could say little g equals EXP of A. What that is doing is, that, that's going to give you this result here. These five numbers. What this is doing is computing. If you're saying EXP of X, what you're telling MATLAB to do is compute the number E raised to the X power. And you can of course combine functions like this. You could say little a is equal to max of the square root of exp of a, and we get a value like that, of 12.1825. So this is a way that you can easily combine functions by embedding one within the other. So again, if a is equal to these five numbers, one, five, two, four, three, in a one by five array. What if we did this? What if we said, square bracket, D comma index, end square bracket, equals the max of A, now, we'll get two numbers. We will get D equals five, and then, we will get index equals two. So what this is doing is your first, the first number, the first variable name you give it, D, is what's holding the maximum value itself. And the second variable you give it, index, is what's holding the location of the maximum in the array. So the reason this is two is because you can see where does the maximum occur. Well the maximum occurs here, which and, in this case, five is the second element of our array a. And a little bit later we'll see how this functionality of MATLAB can be used. A sort of related command here is if we say j equals find a greater than two. That's going to give us this result. j is two, four, five. Why do we get these three numbers? Well, what find does is, it returns the indices of all the elements of your, of your array, where a certain condition is true. So, in this case, the condition you're asking is, where is a greater than two? So it finds all the elements in a that have a value greater than two. And so you can look, and you can say okay, the value one is not greater than two, therefore we don't have the one here, this is the first element. The value five, which is our second element, which has an index of two. Five is greater than two, so therefore we get a two. And, four is greater than two, so the fourth element here, is you get four because that's the forth element. And then this, number three is also greater than two, and three in this case, it's the fifth element. So the second element, the fourth element, and the fifth element of A are all greater than two, so this is what the Find command does, is it returns all the indices where a certain condition holds. Now what if A, instead of using little A as a one by five, array or a vector, what if we had capital A and we had this as a three by three array. With something like this, these, this set of nine numbers arranged three by three. If you said K equals max of A, most people would look at this three by three array and say K is going to be equal to even. Because K is the largest value, in this three by three array. In this case, the way the MATLAB interprets the max command, this isn't exactly true. K in this case, is going to be a one by three array, three, five, seven. So what MAX does, is it computes the maximum on a column by column basis. You see, three is the maximum value in this column. Five is the maximum value in this column. Seven is the maximum value in this column. But what if you didn't want to do this on a column by column basis? What if what you really cared about was what is the overall maximum value in this array? What if you really wanted to get seven out of this. Well, you could just say little L is equal to the maximum of the maximum of A. In that case, it would it would give you seven. So first, it would compute this one by three array, and then it would say, okay, out of this one by three array, with numbers three, five, and seven, which of those is a maximum? And in that case, of, you're going to get of course, the number seven. So this one, this is the way of returning the overall maximum of your array. So one thing we mentioned in the last slide is, when you use the max command or also the min command, you can have MATLAB tell you not just the maximum value but also where in your array that maximum occurs. And here's a practical example of how you could use this. What if you wanted to find out where our curves cross? You had y one, which is the curve here which increases to the function of x. And then you had y two which is the red curve which decreases to the function of x. Now of course mathematically the curves cross where y one equals y two. So all you would have to do is say well, let me compute y one minus y two and find where that's equal to zero. But what if the values are never exactly equal, right? I mean, this is true in a formal mathematical sense but when you're looking at real data, like, real data are not defined continuously. Real data are defined in individual, data points. So, you could have your blue, which would be, you know, these dots here and your red would be these dots here. And you can see that they never exactly overlap. So, what you're really asking when, you know, when two curves cross, in the case of real data, is where do they come closest to one another? And here's an example of how you could compute that using MATLAB. You could say, dummymin comma index equals the minimum of the absolute value of Y one minus Y two. So, this is a way to compute, when does our, my, subtracted. When does my difference vector y one minus my, y two get closest to zero? So you want the minimum but you want the minimum to be absolute value. So that's when this number is, when this difference y one minus y two is going to be closest to zero. And then you can say the crossing point in this case is equal to x sub index. And then you can plot x sub index versus y one sub index in green, and this point here would appear. This is where, this is the point that it would it would identify, is where the two curves cross one another. So, let's review why we did it, this way. The reason we made this a dummymin is because this is a value we don't really care about. We don't care in fact how close these two curves come to one another, right? They could be off by 0.01 or they could be off by 0.01 or 10 to the minus seven. We don't really care exactly about the numbers. We just care, where is it the closest, right? What you care about is what's the value of x at this point, where the two curves come closest to one another. So, what we care about in this case is the index, not the actual minimum value itself. And then furthermore, remember, this index here is just telling you what number of, what point number in y one or y two is, are these two going to be the closest? This could be the tenth element or the fiftieth element or the hundredth element, it would depend on how closely these points are spaced. You don't really care about what element number it is. What you care about is what's the value of x at that number. So, that's why we said we computed an index in this first line and then we computed x sub index in the second line to tell us what the crossing point was. What's the value of x, which you know, in this case, it's something like 3.5 or 3.6, where the two curves approximately cross one another. Now a little bit more on, on built-in MATLAB functions. You can combine these functions with concatenation, like we talked about before. So if we typed in little a equals 1:10, put parenthesis around that and put an apostrophe. Remember from previous lectures that apostrophe means transpose. So that's going to take the numbers one through 10 in a row vector and apostrophe is going to transpose it, so it's going to convert that into a column. Then you can say something like big A is little a comma, square root of A. A squared, comma, sin A, and you would get this ten by four array here, where the first column is A, the second column is the square root of A, third column is A squared, fourth column is the sin of A. There's a little twist here. When we computed A squared, you noticed that we put a dot here, right? We computed a dot carat two, this is saying take a to the second power, but not exactly. It's saying take each element of a times the corresponding element of a, so times itself. So the first element times the first element, the second element times the second element. So that's why you get one, one squared is one, two squared is four, down here, seven squared is 49, etc. This is a way of taking all our values of A, and squaring it, but doing it on an element by element basis. And, note, there's, if you wanted to compute this particular ten by four array, there's different ways to do it. You could do it like this. You could say, little A equals 1:10 with () around it, and then an ; to indication transpose. And then you could say big A:,1 is A, big A :,2 equals square root of A, etc. Remember, this is a way that you can access the entire first column of A, the entire second column of A, the entire third column of A. So if you type these four lines in, you'll get the same definition of little a and the same definition of capital A that we got before. [SOUND] So now let's discuss how you can read in and write out results in MATLAB. Probably the most useful commands are what are called save and load in this case. Let's say that you're typing in a bunch of commands in MATLAB, you had defined a bunch of variables and then you had to go home for the day or for whatever reason you had to turn your computer off. Maybe you're, you're laptop was running out of batteries. But you want to come back to it later, you want, you wanted to sort of stop for a while and then come back exactly where you left off. Well, the simplest thing to do is just to type save matlabsession. That's going to create a file in whatever directory you're in that's called mat, it's going to have the name matlabsession, with the, extension .mat. And then later, if you go into that directory, you can go to the MATLAB command prompt and then type load matlabsession, and that's just going to read in all your variables that you had before. So this is a, this creates a type of file, a .mat file, it can only be read by MATLAB. It can't be read by other, by other programs, but it is very useful in the sense that it will just save all the variables that you had, everything that you had done up to that point. And then when you type, when you load that back in you can just start wherever you left off. Now what if you wanted to save it in a different format? What if you wanted to save it in a format that could be read by, by other programs? So, if we read, if we typed something called sample script two and that created an A and lets just say for the sake of argument sample script two creates an array capital A with size 100 by 10. Then we can type this, dlmwrite parenthesis, apostrophe A dot dat end apostrophe, comma A, comma, and then, apostrophe, backslash T, apostrophe, then end parenthesis. This is going to create a file called A dot dat. And what this is telling that letter to do, DLM rate, in this case, stands for the limited, right. So, this is going to create what's known as a tab-delimited text file. The reason we know that it's a tab-delimited text file is because of this little thing here, this backslash t. This says, take all your variables in A and separate them by, by tabs. The default would be to separate them by commas, but sometimes separating them by tabs is, makes them a little bit easier to read. So the three things that we're telling dlmwrite in this case are, first of all, the file name, A.dat. Secondly, what variable to put into that file, which in this case, the variable we want to put in that file is capital A, and then this third thing is telling them to separate the values by backslash t. What's useful about this is now your, your file A.dat, you can read that into Microsoft Word. And you can see what the numbers are. You could also read it into Excel if you wanted to perform more computations on this using Excel. And also standard software packages that are used for things like plotting can read these, these tab limited text files. Or later if you wanted to read this in mat lab, you could type dlm read at the MATLAB prompt. So just like you have a file called dlmwrite which is a way of exporting tab delimited text files dlmread is a way of importing a tab delimited text file or a comma delimited text file. So you could write a.dat then you could read it in later using this command called dlmread. And then, a third way that you can, ex, re, import and export data in MATLAB is through images there's a file called imread for image read, sorry there's a function called imread for image read. There's also a function called imwrite for image write. They can read in standard formats like JPEG and TIFF, PNG files et cetera. So now let's talk about something that's called the if statement. This is a standard programming logic tool. This is common to just about any programming language you will encounter. But, for some of you I know that MATLAB is the first programming language you're encountering. So this is just like four loops that we talked about previously. This is a very, powerful programming logic tool. So let's go through an example for how we could, how we might use an if statement. Let's assume that we had a one D array called little a. And all the values in this one-D array should, in principle, be positive, or at least they, they should be non-negative. They should be either positive, or they should be equal to zero. So what we want to do is we want to go through our one-D array a, and every time we see a negative value, we want to alert the, the user, so you know about it. You know, hey, that we have a negative number here where we're not supposed to be a negative number, and then what we also want to do is we want to set the value equal to zero. So, these commands would, would do what we need, in this case. We could say for i equals one, up to the length of a. Like we discussed before, this four loop is going to start with a value of one, and it's going to come up to the value of length of a. So it's going to go through all the elements of a. And then what we want to say is, if a sub i is less than zero, then we use this command called disp. Disp in this case is short for display. And so this is going to put more text down on the screen. And the text is going to say is, a negative value is found at a particular index and in order to print the index number on the screen, we use this command that we encountered previously, int to string sub i. So what this is going to do is print out on our screen. Negative value is found at index six, or index 14, or index 21, or whatever. And then what we want, the other thing we want to do is, every time that a sub i is less than zero is, we want to set a sub i equal to zero. And then we have an end command, here, this end is going to signify the end of the if statement. And then because this is in a for loop you need to have a second end command here which signifies the end of the for loop. So you can probably see what how the if statement is useful in this case. We don't want to set all of our values of a equal to zero, right? The only time we want to set the value of a equal to zero, is if that particular value of a was was negative to begin with. If it was positive, like it was supposed to be, we don't want to do anything. So, this if command will only execute these two, if, if, statement is set up so that it will only execute these two commands. It will only display this message and it will only set the value of A equal to zero if that corresponding value of A sub I is less than zero to start with. If that value of A sub I is equal to zero or it's greater than zero, it's just going to skip over this and nothing is going to happen in this case. So a couple of notes on this. One is that there are easier ways to test these sorts of things. Some of you may be familiar with programming logic or some of you may be familiar with MATLAB already. And if you're saying to yourself there's an easier way to do this then, yes, you're right, there's an easier way to do this. This is just meant to illustrate how the if statement works. And a second note here is, we've encountered a couple of conditional statements like this. If something is less than zero or greater than zero or greater than another value, et cetera, what if we wanted to say, you know, is a exactly equal to zero? Is, is one value of a exactly equal to zero? Well, in that case, you have to use a double equals sign because you need to specify to MATLAB. Am I just making an assignment, am I saying a sub i is equal to zero so take the value zero and assign it to this variable a sub i. Or, am I checking? Am I, am I making an assignment or am I like checking to see whether or not something is true. And in the case where you want to check to see whether or not something is true you use a double equals sign. So we could have typed in something like this, if a sub i equals equals zero then what you want to display is is zero value found in a particular index. So, if you want to check whether something is equal to zero, you have to use a double equal sign. So, now let's review what we've learned about MATLAB syntax. We've talked about how semicolons, colons, periods, and apostrophes can have special, special meaning in MATLAB. Semicolon can be used at the end of the line to suppress output. So we showed a lot of examples where you type in some variable name is equal to some output and MATLAB will spit that back out. But you don't want to to spit it back out every single time. In fact most of the time you don't. And if you don't want it to spit it out, you put a semicolon at the end of the line and that will tell MATLAB. Don't tell me the answer after we make this assignment. Semicolons we've also seen used for vertical concatenation, right? One, four, two, semicolon eight, zero, three, in this case, will create a two by three array. What have we used the colon for? We've used the colon to create batches of equally spaced numbers, right? If you said little a is zero, colon zero point zero one, colon five, it will start at zero, to zero point zero one, go to zero point zero two, etc. All the way up to five. We've seen examples of that. We've also used a colon in order to access all the elements along one dimension, right. If I said, if we said little a is big A, three, comma, colon, this will give us the third row of A and all the columns of big A. The period is used to perform array computations versus matrix computations. As we've discussed, saying big C equals A times B is different from saying big C is A dot times B. The dot times in this case says perform this multiplication on an element-by-element basis. And then we see an apostrophe used for a couple of times. For a couple of different things. Apostrophes can be used to transpose a matrix. If you say capital B equals capital A apostrophe, you can take this tall and skinny matrix and convert it into a short and fat matrix. And then we've also seen apostrophes used to delineate string variables. If you said little a equals apostrophe I love matlab, apostrophe, then little a in this case is not going to be equal to a number. It's going to be equal to this text, I love matlab. So, to summarize this lecture, we've learned that if statements, just like for loops, are very powerful computing tools. We encountered this, function called find. This is a very useful command for determining not whether something is true, but, not whether something is true but where something is true. So maybe what you care about is not whether you have a particular value in your array that is greater than a particular number but which indices in your array are greater than that. And that's where find can be very useful. And then we also discuss how MATLAB includes several convenient methods for writing out results and reading in results. [NOISE] We'll conclude this lecture with a self assessment question to see how you understood the concepts that we just discussed. Let's assume that a variable Y is a function of a variable X as shown where we have Y here plotted on the Y axis. Is a function of x on the x axis. And we wish to determine the values of y and x wherever y reaches its peak. So now let's assume you type the following lines in your command window. Max y index equal max of y and then display peak y equals, number to string, max underscore y and then display x at peak equals number to string index. The question now is, does this give you the correct result, and if not, why not? And these are five choices you have, either yes it gives the correct result which is choice a. Or there's four possibilities for why these particular lines of code do not give you the correct result. This might be a good time to pause as you think about it. And now we will move on to show you the answer. The answer in this case is D. No in this case the lines of code do not give you the correct result. And the reason is because max underscore y does correctly hold the maximum value but x(index) is the correct location with respect to x. Let's review why that's the case. These are the lines of MATLAB code that we typed to try to get the maximum value of x and the value of sorry try to get the maximum value of y and the value of x where that maximum value of y occurred. Max Y underscore index equals max of y. In this case, max underscore Y will hold the maximum. Index in this case tells you which element, for instance the sixteenth value of Y is the index where the maximum occurs. Now if you say display peak Y equals number to string max underscore Y, this line is fine. This is going to correctly display the maximum value of y. In this case you said, x at peak equals number to string index. If index tells you the 16th value of y is the largest, you need to type x parenthesis, index, end parenthesis to display the course finding value of x. In this case is roughly 0.37. So, index, in this case, just equals 16, because you can count up here, and you can see when you get to the 16th one, that's where you're at a maximum. But the number you really want is if you drop down a line here, to say that you're at 0.37, and you're at the maximum. And to display that, you have to take x parenthesis index end parenthesis.