0:00

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.

Â 1:15

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

Â 2:48

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.

Â 4:13

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.

Â 8:32

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.

Â