0:00

[MUSIC]

Hi, welcome to lesson three, Functions.

So far, what we've learned is how to perform individual operations with MATLAB,

and we've used MATLAB as a glorified calculator.

But now, it's time to start programming.

You'll remember that we've called a few of MATLAB's built-in functions.

For example, to plot lines in a figure, to calculate the square root,

to calculate the sine, and to do other things.

And there's hundreds more functions that we could call.

But what if we need a function that's not provided by MATLAB?

Guess what?

We can write our own functions, and

we can use them just as if they were built into MATLAB.

Let's see how.

The built-in function named rand

creates random numbers between zero and one.

The numbers are uniformly distributed, and

that means that any number in that range is equally likely to be chosen.

Here's an example of how we might call it.

[CLICKING] It creates a three by four matrix of random numbers between zero and one.

You can see the first argument gave us the three, and the second one,

the four: the height and the width of the matrix that's returned.

And you can see these numbers are all between zero and one, but

what if we needed random numbers between one and ten instead?

That's easy enough. [CLICKING]

And here's some numbers between one and ten.

Multiplng by 9, stretched the range to go from 0 to 9, and

adding 1 shifts the range to go from 1 to 10.

Let's say we need this functionality repeatedly.

We could type in this expression every time we need it, but

that would be annoying.

And more importantly, it would be error prone.

We might mistype something one or more of these times, and

result would be wrong and potentially we might not even notice it.

So let's create our own function that does what we want it to do.

Then we can just call that function repeatedly.

And let's call it myRand.

We create a function by using the Editor.

Let's see what that is.

We can get it with the command, edit.

e-d-i-t,

return, and there's the Editor window right there.

The Editor is the place that you type in code that you can save in a file and

the code we're going to type into the Editor will be a function.

We type this: function myRand,

return,

a equals 1 plus

rand(3,4) times 9,

return.

And I'm going to put an optional “end” in here.

Notice that function and end both turn blue.

These are the key words.

We'll say, something about that a little later.

The word “myRand” is of course the name of our function.

So now what do we do?

Well, we want to be able to run this function, and

in order to run the function, we have to save it into a file.

MATLAB reminds us that we haven't yet saved it in a file with this

little word untitled and this little asterisk right here.

Okay. To save our function into its file,

we click this little diskette right over here.

4:01

Up pops our operating system’s file-save window, and

it's suggesting that we name this myRand.m.

That's a good idea.

This is no time to be imaginative.

We're going to name it just that.

We could pick another name, but it's very bad practice to do that

because it's very confusing.

First off, we should always use the same name for the file, not counting the

dot-m here that we use for the function name, which is myRand in the case.

And second, the dot-m is required.

All MATLAB must be stored in files with the extension, dot-m.

And as a result, they are all called dot-m files.

Okay,

let's save this file.

We hit Save.

4:51

And notice that the file is saved right here in the current folder.

That's exactly where we want it to be

because that's where MATLAB will look for it when we tell it to run it.

Okay, we've opened the Editor;

we've typed in a function; we've saved it into a file.

We can now run it by typing its name in the command window,

just like we would for a built-in function.

Let's do it.

[MUSIC]

Voila.

There's our three by four array of random numbers selected from a range of one

to ten.

[MUSIC]

[APPLAUSE]