1:14

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.

Â