In this video I'd like to tell you how to write control statements for your Octave programs, so things like for, hile, 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. Let me start by setting V to be a ten by one vector of zeroes. Now here's how I write a for loop. For I equals 1 to 10. That's it for I equals one 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 doesn't matter. So I'm, I'm putting the spaces just to make it look nicely indented. But, you know, the spacing doesn't matter but if I do this, then the result is that v gets set to, you know? To the power one, to the power two, and so on. So, this is syntax. For I = 1,10. that makes I loop through the, the, the values one through ten. And by the way, you can also do this by setting your indices equals 1 to 10 and so for indices, if there's an array for one to ten, you can also write for I equals indices. and this is actually the same as the for I equals 1 from 10 and 2, display I N and this will do the same thing. So that was a fall loop if you are familiar with break and continue as break and continue statements you can also use those inside loops in octave but first let me show you how a wire loop works. So here's my vector V and that's where the value I equals one. While I is less than equal to five. Let's set v, I equals 100 and increment I by 1. And so this says I starts off equal to one and then I'm going to set vi equals 100 and increment I by one until I is you know, greater than five. And as a result of that, was 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 100. So that's the syntax for a while loop. Let's do another example. I equals one, while true. And here I want to show you how to use a break statement. So, B equals 999 and I equals I plus one. If I equals six, break End. End. And this is also our first use of an if statement so, I hope the logic of this makes sense, its says for I equals [COUGH] I equals one, and you know, inter loop. While I've repeatedly said VI equals one and inframent I by one and then once I gets up to six, do a break which breaks you out of the wild loop. And so the effect of this should be to take the first five elements of this vector V and set them to 999 and yes indeed we've taken V and overwritten the first five elements with 999. So, this is the syntax for if statements and for wow statement and notice the end. The we have two ends here, this here ends here, ends the if statement, and the second end here whiles the end statement. Now let me show you the more general syntax for how to use the if, else statement. So, let's see, V1 is equal to 999 let's say V1 equals to two for this example. So, let me type if V1 = 1. Display the value is 1. Here's how you write an else statements, all knowledge isn't else if, v1 = 2, this is going to the case that's true in are example, display the value, as two else display the value is not 1 or 2, okay? So, that's a if else, if, else, statement that ends, and of course, here we've just said U1 equals two, so hopefully, yep, displays that the value is two. And, finally I, I don't think I talked about the serity yet, but if you ever need to exert octive you take enter command and you hit enter, that of course will ask you 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 presaved a, on my desktop, a file called squarethisnumber. This is how you define functions in octave. you create a file called you know, with your function name. And then ending in dot m. And when Octave finds this file, it knows that, this is where it should look for the definition of the function square. This number, dot m. Let's open up this file. Notice that, I'm using the Microsoft program Wordpad to open up this file. I would like to encourage you, if you're using Microsoft to 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 another text editor, for editing your functions. So here's how you define the function in Octave and just zoom in a little bit. And this file has just three lines in it. The first line says function y equals square this number x. This tells octave that I'm going to return the value y. I, I'm going to return one value. And that, that value's going to be, be saved in variable y. And moreover it tells octave that this function has one argument which is the argument x and the way that the function body is defined eh, y equals x squared. So let's try to call this function square this number five. And this actually isn't going to work and octave says square this number is undefined. That's because octave doesn't know where to find, find this file. So as usual let's use pwd though I'm not in the right directory. So let's CD users ang slash desktop. That's where my desktop is. Oops, little typo there, uses AG desktop, and if I now type square this number five, it returns the answer 25. As kind of an advanced teacher this is only for those of you that know what the term search path means. But so if you want to modify the active search path. And you could, just think of this next part as, advanced or optional material only for those of you who are familiar with the concept of search paths and programming languages. But, you can use the term at path say C:\users\ang\desktop Slash to add that directory to the Octave search path so that even if I go to some other directory I can still Octave still knows to locate the user ANG desktop directory for functions so that even though I'm in a different directory now it still knows where to find the squarest number function. Okay, but if, if, if you're not familiar with the concept of search paths, don't worry about it just make sure to use the CD command to go to the directory of your function before you run it and that should work just fine. One concept that Octave has that many other programming languages don't is that it can also define lets you define functions that return multiple values, and multiple arguments. So here's the example of that define the function called square and cube this number x. And, what this says, is this, this function returns two values, y1 and y2. I'm going to set them as follows, y1 is squared, Y two is X cubed. And, what this does, is this really returns you know, two numbers to, so, some of you depending on what programming language you've used, if you are familiar with you know, C, C ++, [INAUDIBLE], often we think of a function as returning just one value. But this all the syntax, in Octave, that's in return multiple values. Now back to the Octave window, if I type, you know, A B equals square and cube this number five. Then A is now equal to 25 and B is equal to the cube of five equal a 125. So this is, often convenient if you need 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 it does not put, set my design matrix to be 1-1, 1-2, 1-3. So this is, my design matrix x. With x zero, the first column being the intercept term. And the second term being my, here are the x values of my three training examples. And let me set y to be one, two, three, as follows, which were the y axis values. So, [COUGH] let's say theta is equal to 0;1.1. Here on my desktop I have predefined this cost function J and if I bring up the definition of that function it looks as follows. So function J equals cost function J, inputs x, y, theta. Some comments specifying the inputs and then fire a few steps. Set M to be the number of training examples, that's the number of rows in x. Computer predictions, predictions equals x times theta and [COUGH] oh this is part of the comment that is wrapped around. So, this is part of the preceding comment line, computer script errors, taking the difference, making predictions and the y values and taking element y squaring and then finally computing the cost function J. And Octave knows that J is a value that I want to return because J up here in function definition. Feel free by the way to post this video if you want to look at this function definition for longer and kind of make sure that you understand is, understand the different steps. That's when I run it in Octave, I run J equals cost function JXY theta. It computes oops, made a typo there, should have been capital X. It computes j equals zero because if my data set was you know one 1, 2, 3. 1, 2, 3 then setting right, theta zero equals zero, theta one equals one. This gives me exactly the 45 degree line that fits my data set perfectly. Whereas in contrast if I set theta equals, say zero, zero. Then this hypothesis is predicting 0's on everything. It's saying theta zero equal zero, theta one equal zero, and a computed cost function then is 2.333. And that's actually equal to 1^2 which is my squared error on the first example, plus 2^2 + 3^2 and then divide it by 2 M. Which is two times the number of training examples oops, 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 of examples that we've tried out on our, our simple training example. And so that sanity checks that the cost function j as, as defined here, that it is indeed, you know, seeming to compute the correct cost function, and these on a simple training set. That we had here with X and Y being the, the simple training example that we saw. So now, you know how to write 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 want to tell you about vectorization which an idea for how to make your Octave programs run much faster.