0:39

If you've been following the advice we gave at the beginning of the course

and have typed in the same commands that I've been typing in,

it shows that you're serious about learning this stuff.

And that's good.

That's a great first step.

If you keep doing that,

you'll get a pretty good idea of what computer programmers do.

1:20

To actually, you know, BE a computer programmer, you have to solve problems by writing

computer programs, and that's the second step.

And it's a big part of learning to be a computer programmer, and

it's a big part of this particular course.

2:03

In other words, there's little doubt that you can learn to be a computer programmer.

But the only way you're going to do it is to ...you know... DO it.

And we're going to give you a chance to do it in the form of

weekly "homework assignments", starting with week three.

2:26

We use the word "homework" because you do the work on your own, and

that usually means you'll be doing the work at home.

Of course, you may be doing the whole course on your own at home, but

that's what we call them.

2:39

Each one of these homework assignments consists of no more than ten problem

statements, and you'll solve each of these problems by writing MATLAB code.

For each individual problem,

you'll write one individual MATLAB function that solves that one problem

according to the specifications given in the problem statement.

3:19

So, for example, if there are eight problems in,

say, the third assignment, you'll create eight m-files for that assignment.

3:40

I've got a folder, and I'm calling it hwx.

Let's go up one level, so you can see that.

So there's hwx.

And I double-click it so that it's the current MATLAB folder.

Here X means example.

You could make a folder named hw3 for

homework three for instance, instead of hwx for example.

4:30

For each homework assignment, you have to download a separate grader to your

computer before you can use it, so you need to know how to do that.

Links to both the grader and

the PDF homework are given with every programming assignment, and

they show up when you click the green button that says, "Start homework Now".

4:50

The grader for this example homework assignment is named hwx.p.

There's one grader for each homework assignment, and

each one has a file name like this one.

But with the X changed to the homework number.

So for homework five here, for example,

the grader is hw5.p.

Now let's look at the problem set in the PDF.

5:29

The first one says, "Unless otherwise indicated,

you may assume that each function will be given the correct number of inputs and

that those inputs have the correct dimensions.

For example, if the input is stated to be three row vectors of four elements each,

your function's not required to determine whether the input consists of three

two-dimensional arrays, each with one row and four columns."

This says, "unless otherwise indicated". Later on, we will actually have you

check in your function to make sure the input has the right form,

but at first, unless otherwise indicated, you don't have to do that.

6:08

Next, "Unless otherwise indicated,

your functions should not print anything to the Command Window."

But, if you have your function print something to Command Window,

it's not going to be a problem because it says, "but

your function will not be counted incorrect if it does".

7:08

It says, "Write a function called corner_sum that takes a matrix,

M, as its input argument and

returns a scalar that is equal to the sum of the elements in the four corners of M.

7:26

For example, MATLAB would assign 20 to the variable, xyz,

if the function was called like this."

So we see an example command, xyz = corner_sum of

... in other words, with the input argument...: 1, 2,

3; 4, 5, 6; 7, 8, 9.

So this is a three by three matrix.

And the four corners are the number 1,

the number 3, which is the upper left element and

the upper right element here on the first row, and then here on the bottom row,

the bottom left, or lower left, position has a 7, and the

lower right position has a 9.

So the sum is: 1 plus 3 is 4, plus 7 is 11, plus 9 is 20.

So, xyz would be assigned 20, sure enough.

And we've got to write a function called corner_sum that'll do that, no matter

what we give it as an argument.

8:49

And we gotta know the name of the function, because if we don't use

the exact name given in the problem, we won't get any credit.

Okay, there it is, corner_sum.

9:21

So... What do we do now?

Well, we need to put a function header in here.

Well, that's easy to begin with, because we just type "function" and

we get this keyword highlighted in blue.

9:43

Because if it does, we gotta put an output argument here.

So, functions usually do. Let's read the problem again.

"Write a function called corner_sum that takes a matrix, M, as its input argument,

and returns" ... There we go ... "returns a scalar."

So, like most functions, it returns something,

"a scalar that's equal to the sum of the elements in the four corners".

Well it's a sum, so let's call it "s".

10:06

[CLICK]

After that, we have to put an equals sign, and now comes the name of the function,

which we can see right here, corner_sum.

[CLICKING]

And in parenthesis, if this function takes input arguments,

and most of them do, we need to put the input arguments.

So let's see if it does.

We read it again.

"Write a function called corner_sum that takes a matrix, M, as its input argument."

Yes it does,

and it says "a matrix, M".

So that means there's just one argument.

I mean it's not saying like two matrices, three vectors, or something.

11:16

Okay, so that's the end of my function header.

I'm going to hit return

and I'm ready to type the body in here.

So let's think about the problem now.

What do we have to do again?

11:44

And the first corner is pretty easy.

The top left corner is element A(1,1).

What about the rest of the elements?

If we knew this was a three-by-eight matrix, we could do it this way.

[CLICKING]

Because this would be on the first row, the left end, ... you know, ...

... It's the upper left corner... And then we have the last element on the row.

Because there's eight columns in a three-by-eight matrix, it will be (1,8).

In the bottom left is on row 3, element 1, and

then the bottom right corner is the last row, 3, the last column, 8.

That's great, but, you know, what if it isn't a three-by-eight matrix?

What do we do then?

Well one thing we could do

12:40

is call the size function, and get the number of rows and number of columns:

[m,n] = size(A)

will put the number of rows into m and the number of columns into n.

So, since this is supposed to be the last column, we make that an n.

So this's supposed, this is supposed to be the last row, we'll make that an m.

13:23

So far it's the same, still the same,

but, instead of n, I put "end", because "end" is a keyword

that gives you the last element index in the position that you put it.

So I put it in the column position, and

it will change to this n, the last element, uh, the column.

And it works for the row too,

[CLICKING]

and it works for both of them at the same time.

So, I've got two solutions: this one and this one.

Well which one to use?

Which one's better?

Well, neither.

It makes no difference.

14:46

So before we even think about saying our solution's correct,

we need to test it to see if it is.

By "test it", I mean making up inputs and calling it directly from the MATLAB

Command Window with those inputs, and looking at the outputs.

I don't mean running the grader.

15:04

The problem with running the grader is it doesn't tell you enough.

In the very rare cases when you'll get the function right without any testing, oh

it'll tell you it's correct.

But in the vast majority of cases, in which you don't get the function right,

all that it'll tell you is the arguments that it succeeded on and

the first argument that it failed on.

That helps a little, but there's no hint about why it failed.

You'll get no information about that if you don't run the function yourself.

But if you run tests in the Command Window,

MATLAB itself will always tell you, for example, if there's a syntax error, and

it'll tell you the exact line it's on.

And there are a lot of semantic errors it'll identify too,

like trying to use a nonexistent variable or using an index that's outside the range

for some vector or matrix you've got in there.

You don't want to give up all this free help.

And if MATLAB doesn't report any errors when you try your test,

you'll see the value that each function returns, and

you can check it to see whether it's correct or not.

So ... Let's test our function.

Well, to do that, we need to give it a matrix.

16:12

We know it needs to work on the example given here: 1, 2, 3;

4, 5, 6; 7, 8, 9, so let's do that one.

16:44

So you may remember that "ones" gives you a bunch of ones.

And when you give it one argument, it means, like with a 3,

it means 3 by 3 ones.

And so, ... let's see ... each corner's a 1, there are four corners,

so the answer should be 4. So let's try that.

And we get 4.

More good news!

17:06

Now let's just try another one.

That's a 2 by 5 matrix, and the corners are 1, 5, 6, and 10.

So 1 plus 5 is 6, plus 6 is 12. 22 should be the answer.

Let's see.

[CLICKING]

22.

17:31

Now let's try one more here.

X = ...

[CLICKING]

So we've got 1s, 2s, and 3s...

Uh, the corners are 1, 1, 3, 3, so that should be 8.

So, let's set y equal to the result.

[CLICKING]

18:00

The input to the function, that is, the various matrices

we use to test our function, were created outside of the function.

Not in here, over here in the Command Window.

18:17

When we call our function, right here,

MATLAB passes the value that's in this variable X,

which is a 3 by 4 matrix of ones, twos, and threes, into the function.

Inside the function, the input argument A is assigned a copy of this matrix,

18:40

the one that was in X.

But it's just a copy over here in A.

Then the function goes to work, does what it's supposed to do with it,

assigns the value that it gets to s, which we've made an output argument here.

18:55

And then the function returns,

and when it returns control to the Command Window,

this value that's in s, which happens to be 8, is copied over here,

and it's assigned by the command window to y, so y gets the 8.

And the value's exactly the sum of the corner elements of the original matrix X.

Perfect.

19:51

a different matrix is supposed to be assigned to A.

We don't want to copy over that value that was assigned to

A with this same value every time.

We'll get the same answer every time, right or wrong.

20:41

We're getting 20 every time because we're clobbering the A that was copied

from the Command Window into this argument here with our matrix

that we defined inside here, 1, 2, 3; 4, 5, 6; 7, 8, 9.

Friends, we can give this thing input after input until the cows come home, and

we're going to get that same monotonous 20 as an output every time, right or wrong.

And I can tell you, the grader won't like it.

Okay, let's get rid of that.

22:05

Well, it returns 8, or it looks like it does.

So it may seem to work.

It's kind of strange, though.

When it returns something, it doesn't usually say s is equal to 8.

It's saying s is equal to 8 because that's what it does when you leave the semicolon

off an assignment to s.

But you might not notice that when you are testing it.

22:26

Let's try assigning it to a variable like we did before.

[CLICKING]

Oh my.

We got an error this time.

And it says, "Error using corner_sum Too many output arguments."

The phrase, "Too many arguments", means that we've given one output argument here in

the y when we called it, and zero output arguments here when we defined it.

24:04

So, we have this basic menu here.

Let's see.

If type a 0, it'll EXIT.

If we type 1, it'll test corner_sum.

If we type 2, it'll test total_weight.

If we type 3, it'll test all the problems, and it gives a score and

a submission code.

We'll talk about that later.

24:48

Well, it says. "Your solution is correct",

which is what we were hoping for.

It also gives what it calls "feedback".

Let's look at that.

As I mentioned earlier, this is just the set of arguments it tried.

25:08

As you can imagine, these arguments have been chosen

carefully to test most of the errors that students are likely to make.

But, if it fails on any of these arguments,

as you'll see in a couple of minutes,

it won't tell you why, and it won't tell you what the next arguments are

that it's going to test on.

25:26

Both of these pieces of information would make it far easier for

you to debug your function, but

the emphasis in these homework assignments is for you to practice and

improve your own debugging skills by coming up with your own examples.

25:39

Wait!

I know what you're thinking.

You're thinking

you can look inside the grader function to see what the test cases are.

Well, you naughty, naughty thing.

Sorry, no can do.

This is a p-file, and p-files are encrypted.

And, this is bazooka-proof encryption,

so there's no way to hack into one of them.

26:10

You know? Like a real computer

programmer in the real world solving real problems.

And a big part of solving a problem is testing your solution.

Of course, this is not the real world, but it's as real as we can get it.

27:06

Well, this was so much fun.

Let's do this again.

But this time, let's see how the grader reacts to mistakes and

then show how we might find them and correct them.

Let's start with the first error we made before,

when we were...you know...putting our own matrix inside the functions.

Let's pull this thing over here a little bit.

And...uh...

28:12

Gives me the 20.

Remember? I'm supposed to get a 20.

Of course, I didn't give it an argument, but we're making mistakes here.

So, we're ready to run the grader.

Let's see how it reacts.

28:52

Title: HW X Problem 1, Grader Error.

Body: My function works correctly.

It computes the correct answer, 20, for the example, yet

the grader says it is incorrect.

What is wrong with your grader?

29:13

Now, try to imagine somebody reading your post.

That person has no information other than your claim

that your function works correctly.

But, of course, your function is incorrect.

In fact, it has three major errors.

29:34

So, what information should you post?

First off, there's something that you should not post.

You should not post the code from your function.

In fact, you must not post any of the code from your function.

We don't allow that.

In fact, we don't allow anyone to post any code in

any of the forums regardless whether it's correct or incorrect.

Instead of posting code from your function or saying that your function is correct,

you need to run the function in the MATLAB Command Window and

copy both the command and the output into the post,

so you can prove to the reader that your function's working correctly.

30:29

The phrase, "Too many input arguments",

means that we're trying to input an argument here, when we call our function,

but we didn't include an input argument in the heading here,

33:26

Okay, we made a common testing error here.

We ran the grader immediately after the last change.

We should have tested the function directly first.

So let's test it directly.

[CLICKING]

33:41

"Attempted to access A(1,3); index out of bounds because

size(A) = [2,2]. Error in corner_sum, line two."

Let me click that, takes me right there.

Ah yeah!

36:10

And four, if we still can't figure out what's wrong,

we can post in the discussion forum.

But we provide the result

from running our function directly from the command line when we do that.

And we should do that as a last resort.

And when we do it, we should also provide additional information by running these

commands, including the input the grader fails with.

37:29

The second output, called tv, is the total volume of the objects.

For example, if vol is equal to [1 2] and

wd is equal to [1 0.5] , then executing the command,

37:44

...and you can see it there... wv = total_weight of vol, wd,

would make w equal to 2 and v equal to 3.

Well let's see if this makes sense now.

Okay.

So, the first element here is the volume of something, and

the first element here is the density of that same something.

38:08

So, if you want to find out how much that first one weighs,

you'd multiply the volume, which is 1, times the density, which is 1.

1 times 1 is 1.

Okay.

38:18

And if you want to know the weight of the second one, that'd be 2 times 0.5,

that's also 1, so I guess both of them weigh 1.

Now,

tw is the total weight, so the total weight would be 1 plus 1,

38:36

And the volume,

total volume, well that's easy.

The total volume is 3.

Okay, I see.

All right. So that's what we gotta do.

But of course this is the example with just two. It could be three

or five or 35 or whatever.

38:52

So we gotta get started.

Let's create the function file first.

This time I'll use the toolbar and the create-new-function command that's right here.

Let's see, somewhere there's the new-function command.

I create that and it's called "untitled".

39:09

And it gives me the keyword "function", so I don't have to type that in.

It's already put brackets here, and it says to put the output arguments there.

And then "untitled", that's the name of the function.

39:49

So we've got that.

And the input arguments.

Gotta put those in.

Those were,

they suggested, vol and wd.

This time I'm going to use the ones they suggested.

Doesn't really make any difference, but we're going to do that.

So good, let's delete the comments for now and,

we're ready to solve this problem.

Okay, so

we figured out when we read this that we basically have to multiply the volume

of each element with a corresponding density element to get the weight.

So we have to multiply the corresponding elements and

add the product together, something like maybe this.

40:44

[CLICKING]

That's the second volume times the second density, and

we don't know how many of these we've got. We can't be...

I mean: what if they give them, uh, 500, we can't be typing 500 in.

41:14

Well, that's exactly what array multiplication does, so

we can get rid all this.

And we can say x equals vol.*wd...

...*wd. That'll do it.

That'll multiply the first element times the first element,

the second element times the second element.

42:47

Well, that's another job for the sum function.

Only this one's even easier.

because we just...uh, wait a minute...

tv is total volume.

We just sum out, sum up the volume.

43:04

Let's come up with a value in the window here, Command Window.

For volume, we'll make it 1, 2, just like in the problem

that we're given in the homework set, and the same density.

44:12

Let's see that's correct.

The weight is 2, but where is the total volume? We're supposed to get that.

Who stole the total volume?

Well nobody did. We didn't ask MATLAB to do anything with the second output,

so it just silently swallowed it.

45:42

And when we submit that then we're finally telling the world

whether we did okay with them.

That'll be translated and you'll get the score, 100.

There's one more thing though.

Any nontrivial problem can be solved multiple ways.

So could we come up with, say, another approach here?

Let's see, we need to sum the element-wise products of two vectors.

46:14

I think we're on the right track, but for matrix multiplication

the inner dimensions must agree.

And here we have two row vectors of the same length.

What if we transpose the second vector?

That'll make wd a column vector.

46:30

And of course, vol is row vector.

So the interdimension's are the same,

their length, whatever that length might be.

They're supposed to have the same length of course.

46:55

"Unless otherwise indicated, you may assume that each function will be

given the correct number of inputs and those inputs have the correct dimensions."

And the correct dimensions here are that they're both the same length.

Of course we've already used that when we did this array multiplication,

but it's worth mentioning.

So, back to what we're trying to do, this second method.

tw is equal to vol star wd prime.

47:27

wd prime is a column vector, so it's 3 by 1, for

example, if it's three elements, or

five by one, or whatever it is,

n by one.

And vol is one by n.

One by n, n by one.

The inner dimensions are both the same.

And what's the result's dimensions?

Well, 1 by 1,

which is a scalar, and it's equal to the total weight.

48:22

Sum is 3.

Sum of the product, yeah, okay.

That's correct.

Okay. Now, we're ready for the grader, again.

Let's see if I can type it right this time.

And let's go for broke.

48:53

And here's another example of why.

Let's make a simple syntax error here.

Suppose we accidentally type v-o-l-l, you know, instead of v-o-l.

It's an easy mistake to make and easy to miss.

Now, let's run the grader.

49:11

Pick 3.

"Your solution is not correct."

You know, it doesn't give you any hint that there was a syntax error.

It tells you what it was tried on.

And you look at your code, and you think, oh my gosh, that's great.

MATLAB is giving you a hint right up here in the editor, which you might miss.

There's some yellowish mustard- color highlight with a little squiggle.

It says, "Input argument 'voll' might be unused."

Yeah, it's unused.

Voll, O-L-L, is not used anywhere.

Just vol, so that should give you a hint.

But, of course, if you run it here... let's run it...you know the last thing we did...

50:01

Now, it turns it around, and it doesn't know that this is wrong.

It thinks that maybe that was right and this was wrong.

But anyway, once you look at that, you'll figure out what the problem is.

But let's suppose you get this working.

50:16

And you want to tell somebody in a post that you might be having trouble.

You want to ask them questions about that.

If you can't figure out what's wrong with your solution on your own,

and you've tried hard to do it on your own,

you can go ahead and ask a question on the forum.

But when you do, provide as much information as possible.