Variable Number of Function Arguments

This course teaches computer programming to those with little to no previous experience. It uses the programming system and language called MATLAB to do so because it is easy to learn, versatile and very useful for engineers and other professionals. MATLAB is a special-purpose language that is an excellent choice for writing moderate-size programs that solve problems involving the manipulation of numbers. The design of the language makes it possible to write a powerful program in a few lines. The problems may be relatively complex, while the MATLAB programs that solve them are relatively simple: relative, that is, to the equivalent program written in a general-purpose language, such as C++ or Java. As a result, MATLAB is being used in a wide variety of domains from the natural sciences, through all disciplines of engineering, to finance, and beyond, and it is heavily used in industry. Hence, a solid background in MATLAB is an indispensable skill in today’s job market.
Nevertheless, this course is not a MATLAB tutorial. It is an introductory programming course that uses MATLAB to illustrate general concepts in computer science and programming. Students who successfully complete this course will become familiar with general concepts in computer science, gain an understanding of the general concepts of programming, and obtain a solid foundation in the use of MATLAB.
Students taking the course will get a MATLAB Online license free of charge for the duration of the course. The students are encouraged to consult the eBook that this course is based on. More information about these resources can be found on the Resources menu on the right.

CM

It was a very involving course! Everything is taught by doing it practically. Assignments are really good and make you adept in what you learn the whole week. As far, the Best MOOC I've completed.

RR

Apr 26, 2019

Filled StarFilled StarFilled StarFilled StarFilled Star

This was an amazing course! I attended mainly because I needed to refresh my modest Matlab knowledge. It was a great help and the exercises were so much fun!! Thanks for such a great opportunity!

從本節課中

Selection

Selection is the means by which MATLAB makes decisions about the order in which it executes its statements. We will learn how to use the if-statement, which is the most important method of selection. We will learn how to use relational operators and logical operators. We will learn how to write polymorphic functions and how to make functions resistant to error.

Akos Ledeczi

Mike Fitzpatrick

腳本

[MUSIC] Back in lesson four, we introduced polymorphic functions. Remember that polymorphic functions are functions that behave differently based on the number of input arguments or output arguments that they're called with or even the type of input or output arguments. Many built-in functions of polymorphic, such as the square root function, max, size, plot and on and on. It's one of MATLAB's strongest features. How we make I functions polymorphic? What's actually quite simple, really? The reason we waited until now to show you is we really couldn't do it right without if statements. Now we'll combine if statements with a couple of built-in functions that we haven't mentioned yet. First one's nargin or nargin, which means number of arguments input. It gives the number of actual input arguments that the caller passed in. The other ones nargout or in nargout. It returns the number of actual output arguments that the caller requested. These two functions can be called from inside our functions to make our functions polymorphic. Here's a function declaration of a function called called multable with two output arguments and two input arguments. It returns in n-by-m multiplication table in the output argument called table. Optionally, it can also return the sum of all elements in the output argument in the variable summa. If it's called with only one input argument, it returns and n-by-m matrix. So let's see the code. First, we check to see whether the function was called with one or two arguments. If nargin is smaller than two, then it was called with only a single argument. A single argument is always put in the first position, which in this case is the variable n. When m is omitted, we're supposed to return an n-by-m matrix, so we set m equal to n. This is a classic example of setting a default value for an input that's omitted. Now we're ready to compute the multiplication table. It's actually pretty simple. We create two row vectors, one going from one to n and the other from one to m. We transpose the first vector to be a column vector and use matrix multiplication to create the table. Now, it's time to determine whether we need to compute the sum of the matrix as well. We need to do that only if the user requested a second output argument. So we check nargout. If it's two, we call the sum function. Remember that sum computes the sums of the columns of a matrix and returns them in a vector? Well, that's not what we want. So here, we use the column argument to create a vector from all the elements of the table. That's done with a colon. And then we sum and give that to sum and it'll add them all up. If nargout is one, then we avoid sum entirely. But there's no harm in computing summa, even if the caller doesn't request it. If we calculate it when the user doesn't request it, MATLAB will simply discard it when the function returns. So, all we've done is save a few microseconds of calculation time here. But you will definitely want to use nargout to avoid long calculations, perhaps many minutes when the user doesn't ask for them. In any case, here's our first polymorphic function. Let's see it in action. Well, here's our mult table function in the Editor all ready to go. Let's pitch it a few different inputs to see how well it does. [SOUND] There we gave it three by four and it gives us a three by four array and that looks like a multiplication table. Let's ask it for a couple of outputs. There's a table and there's the sum and that's the correct sum. Okay. This time, we're just asked for one output and, but we only gave it one input argument and you'll remember that, that defaults to five by five instead of just five. So far so good. It's hitting them out of the park. Lets see how it handles a curve ball. Well, we managed to make it fail by giving it no argument at all. MATLAB tells us the line on which the failure occurred and we can see what happened on that line right here. When MATLAB tried to look up the value of n, it found no value of n, because we gave it no value. Well, it might seem like cheating, more like a spit ball than a curve ball. Let's be a little more fair. Let's give it some arguments. Well let's do this. Hm, I've got the empty matrix back. Clearly, we didn't plan on negative inputs, but can you see where the empty matrix came from? Well, the colon operator here, one to n on line seven became one to minus 3, because we gave a minus 3 for n. And the one colon m became one colon minus 5. And as we saw way back in lesson two when the end is less than the beginning and we don't get, include a negative increment with it, the result's an empty matrix. Here we've got two of them. And now we see that the product of two empty matrices is also an empty matrix. So now we can understand why we got the empty matrix here. [SOUND] And we can also understand why we got this error. But both of these cases represent traps for the user, we should do something about them. [MUSIC] [APPLAUSE]