In this video, I'd like to tell you how to write control statements for your Octave programs, so things like "for", "while" and "if" statements and also how to define and use functions. Here's my Octave window. Let me first show you how to use a "for" loop. I'm going to start by setting v to be a 10 by 1 vector 0. Now, here's I write a "for" loop for I equals 1 to 10. That's for I equals Y colon 10. And let's see, I'm going to set V of I equals two to the power of I, and finally end. The white space does not matter, so I am putting the spaces just to make it look nicely indented, but you know spacing doesn't matter. But if I do this, then the result is that V gets set to, you know, two to the power one, two to the power two, and so on. So this is syntax for I equals one colon 10 that makes I loop through the values one through 10. And by the way, you can also do this by setting your indices equals one to 10, and so the indices in the array from one to 10. You can also write for I equals indices. And this is actually the same as if I equals one to 10. You can do, you know, display I and this would do the same thing. So, that is a "for" loop, if you are familiar with "break" and "continue", there's "break" and "continue" statements, you can also use those inside loops in octave, but first let me show you how a while loop works. So, here's my vector V. Let's write the while loop. I equals 1, while I is less than or equal to 5, let's set V I equals one hundred and increment I by one, end. So this says what? I starts off equal to one and then I'm going to set V I equals one hundred and increment I by one until I is, you know, greater than five. And as a result of that, whereas previously V was this powers of two vector. I've now taken the first five elements of my vector and overwritten them with this value one hundred. So that's a syntax for a while loop. Let's do another example. Y equals one while true and here I wanted to show you how to use a break statement. Let's say V I equals 999 and I equals i+1 if i equals 6 break and end. And this is also our first use of an if statement, so I hope the logic of this makes sense. Since I equals one and, you know, increment loop. While repeatedly set V I equals 1 and increment i by 1, and then when 1 i gets up to 6, do a break which breaks here although the while do and so, the effective is should be to take the first five elements of this vector V and set them to 999. And yes, indeed, we're taking V and overwritten the first five elements with 999. So, this is the syntax for "if" statements, and for "while" statement, and notice the end. We have two ends here. This ends here ends the if statement and the second end here ends the while statement. Now let me show you the more general syntax for how to use an if-else statement. So, let's see, V 1 is equal to 999, let's type V1 equals to 2 for this example. So, let me type if V 1 equals 1 display the value as one. Here's how you write an else statement, or rather here's an else if: V 1 equals 2. This is, if in case that's true in our example, display the value as 2, else display, the value is not one or two. Okay, so that's a if-else if-else statement it ends. And of course, here we've just set v 1 equals 2, so hopefully, yup, displays that the value is 2. And finally, I don't think I talked about this earlier, but if you ever need to exit Octave, you can type the exit command and you hit enter that will cause Octave to quit or the 'q'--quits command also works. Finally, let's talk about functions and how to define them and how to use them. Here's my desktop, and I have predefined a file or pre-saved on my desktop a file called "squarethisnumber.m". This is how you define functions in Octave. You create a file called, you know, with your function name and then ending in .m, and when Octave finds this file, it knows that this where it should look for the definition of the function "squarethisnumber.m". Let's open up this file. Notice that I'm using the Microsoft program Wordpad to open up this file. I just want to encourage you, if your using Microsoft Windows, to use Wordpad rather than Notepad to open up these files, if you have a different text editor that's fine too, but notepad sometimes messes up the spacing. If you only have Notepad, that should work too, that could work too, but if you have Wordpad as well, I would rather use that or some other text editor, if you have a different text editor for editing your functions. So, here's how you define the function in Octave. Let me just zoom in a little bit. And this file has just three lines in it. The first line says function Y equals square root number of X, this tells Octave that I'm gonna return the value Y, I'm gonna return one value and that the value is going to be saved in the variable Y and moreover, it tells Octave that this function has one argument, which is the argument X, and the way the function body is defined, if Y equals X squared. So, let's try to call this function "square", this number 5, and this actually isn't going to work, and Octave says square this number it's undefined. That's because Octave doesn't know where to find this file. So as usual, let's use PWD, or not in my directory, so let's see this c:\users\ang\desktop. That's where my desktop is. Oops, a little typo there. Users ANG desktop and if I now type square root number 5, it returns the answer 25. As kind of an advanced feature, this is only for those of you that know what the term search path means. But so if you want to modify the Octave search path and you could, you just think of this next part as advanced or optional material. Only for those who are either familiar with the concepts of search paths and permit languages, but you can use the term addpath, safety colon, slash users/ANG/desktop to add that directory to the Octave search path so that even if you know, go to some other directory I can still, Octave still knows to look in the users ANG desktop directory for functions so that even though I'm in a different directory now, it still knows where to find the square this number function. Okay? But if you're not familiar with the concept of search path, don't worry about it. Just make sure as you use the CD command to go to the directory of your function before you run it and that actually works just fine. One concept that Octave has that many other programming languages don't is that it can also let you define functions that return multiple values or multiple arguments. So here's an example of that. Define the function called square and cube this number X and what this says is this function returns 2 values, y1 and y2. When I set down, this follows, y1 is squared, y2 is execute. And what this does is this really returns 2 numbers. So, some of you depending on what programming language you use, if you're familiar with, you know, CC++ your offer. Often, we think of the function as return in just one value. But just so the syntax in Octave that should return multiple values. Now back in the Octave window. If I type, you know, a, b equals square and cube this number 5 then a is now equal to 25 and b is equal to the cube of 5 equal to 125. So, this is often convenient if you needed to define a function that returns multiple values. Finally, I'm going to show you just one more sophisticated example of a function. Let's say I have a data set that looks like this, with data points at 1, 1, 2, 2, 3, 3. And what I'd like to do is to define an octave function to compute the cost function J of theta for different values of theta. First let's put the data into octave. So I set my design matrix to be 1,1,1,2,1,3. So, this is my design matrix x with x0, the first column being the said term and the second term being you know, my the x-values of my three training examples. And let me set y to be 1-2-3 as follows, which were the y axis values. So let's say theta is equal to 0 semicolon 1. Here at my desktop, I've predefined does cost function j and if I bring up the definition of that function it looks as follows. So function j equals cost function j equals x y theta, some commons, specifying the inputs and then vary few steps set m to be the number trading examples thus the number of rows in x. Compute the predictions, predictions equals x times theta and so this is a common that's wrapped around, so this is probably the preceding comment line. Computer script errors by, you know, taking the difference between your predictions and the y values and taking the element of y squaring and then finally computing the cost function J. And Octave knows that J is a value I want to return because J appeared here in the function definition. Feel free by the way to pause this video if you want to look at this function definition for longer and kind of make sure that you understand the different steps. But when I run it in Octave, I run j equals cost function j x y theta. It computes. Oops, made a typo there. It should have been capital X. It computes J equals 0 because if my data set was, you know, 123, 123 then setting, theta 0 equals 0, theta 1 equals 1, this gives me exactly the 45-degree line that fits my data set perfectly. Whereas in contrast if I set theta equals say 0, 0, then this hypothesis is predicting zeroes on everything the same, theta 0 equals 0, theta 1 equals 0 and I compute the cost function then it's 2.333 and that's actually equal to 1 squared, which is my squared error on the first example, plus 2 squared, plus 3 squared and then divided by 2m, which is 2 times number of training examples, which is indeed 2.33 and so, that sanity checks that this function here is, you know, computing the correct cost function and these are the couple examples we tried out on our simple training example. And so that sanity tracks that the cost function J, as defined here, that it is indeed, you know, seeming to compute the correct cost function, at least on our simple training set that we had here with X and Y being this simple training example that we solved. So, now you know how to right control statements like for loops, while loops and if statements in octave as well as how to define and use functions. In the next video, I'm going to just very quickly step you through the logistics of working on and submitting problem sets for this class and how to use our submission system. And finally, after that, in the final octave tutorial video, I wanna tell you about vectorization, which is an idea for how to make your octave programs run much fast.