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] Okay, I promised that we were done with guess my number. But I didn't say we were done with if statements. I still need to show you an example with multiple else if branches. I've got one here. It's called day of the week. And I can pop it up in the editor just by double clicking it, and here it is. It's a little longer, so I'll need to have some more space, and I can get that by just stretching the editor window down like this. Woah, how long is this thing woah, not much command window left, but at least we can see all of our function. Our new function will the print the name of the day of the week when we call it and give it the number of the day of the week as an input argument. It's number n. And it will also identify it as either a weekday or a weekend day. We have a long if else if else statement here, because it has six else if branches. So, counting its if and its else branches, it has a total of eight branches. After this long if else if else statement comes a separate if else statement. As I've mentioned, the function takes n, the number identifying the day of the week, as an input. The long if else if else statement then checks n, comparing it one by one to the numbers one, two, three, four, five, six, and seven. When the first true condition is reached, let's say n is three, we would print out the day of the week. In that case, would be Tuesday. And then, we set day type, a variable called day type here, equal to one, if it's a weekday. Or two if it's a weekend day, here is a two, here is a two. The else branch here is just there to handle errors. If the input is not one of the integers one to seven, then none of these first seven conditions can be true. So we take that else branch. And in that branch we print out an appropriate error message. Right here. And then we return from the function. We'll say more about this return keyword a little bit later. This second if statement, this if else statement down here is here to tell the user whether the day is a weekday or not. It checks the day type variable, and if it's equal to one, it prints, which is a weekday, otherwise it prints, which is a weekend day. In either case after the y here of the word day, we included a backslash n. This is the escape sequential, remember, that puts us to a new line. We wanted to get ready for the MATLAB prompt. Notice, though, that we omitted the new line in all the fprintf statements above. And we did that so when the name of the day and the comma are printed out, this text following it, which is a weekday or which is a weekend day, will be on the same line. Notice one last detail about the printing, we included a space here before the switches so that there will be a space between this comma after the weekday or weekend day name is printed, and this phrase, which is a. Well it's time to try this function out. Well, we'll note that this is an American function. The week starts on Sunday. And, that looks good. Wednesday is what we would expect. We would put in a 4. Let's see, Sunday, Monday, Tuesday, Wednesday. Yep, that's right. And we can see that Sunday is identified properly as a weekend day. And Wednesday is a weekday. So far, so good. So, we've tried a weekend day. We've tried a weekday. Now, we need to make a mistake and see if we handle errors properly. [SOUND] Well, it handled the error just right. It's passed all the tests. I'd say this is a working function. Well, it handled the error just right, and it did that because of the return statement inside this else branch. Return is not only a keyword, it's an entire statement. And it does just what it's name suggests. When MATLAB executes the return statement, it immediately quits the function and disregards all the other statements following it. Most typical use of the return is exactly what we're doing here. We encounter an error. And it's time to quit the function. Functions normally quit only when they reach the end of their statements. But you may want to have a function return earlier, either because of an error, just like this case, that was detected before the end of the function is reached, or simply because the function is done with its work before then end is reached. In either case, the return is what you need. Let's just remove the return statement for a second and see what happens with this bad input. I'm going to hit delete here. So, let's just run it again. Since that's the last thing we did, all I have to do is hit up arrow and return. And what did we get here? Let me stretch this up a little bit so we can see better. We got the error message, Number must be from one to seven. But then this is an ugly result, here. This ugliness happens because day_type is not assigned a value when the input is out of range. And, none of these else ifs or this if happens, and so, none of these assignment statements take place. day_Type equals. Since it doesn't have a value, MATLAB has forced to step in and take charge. This sort of behavior is termed an ungraceful exit. And it's an embarrassment for any self-respecting programmer. But with the return statement in there, and let's put it back. There. The function ends gracefully, and that's how all nice functions behave. So let's do it one more time. There. Return statement is a convenient way to handle this situation. And indeed, it's a convenient way to handle any situation for which there's nothing more that a function can do or needs to do. We've seen quite a few variants of the if statement. Let's summarize what we've learned. The simple if statement has a condition and a block of statements that's executed if the condition is true. If the condition is false, then the statements in the block will not be executed. The if else statement lets you specify a block of statements for the case when the condition is false, also. So, there's a separate block of statements for each of two cases. That is true or false. Because of that else branch, exactly one block of statements will always be executed. The if else if statement lets you specify multiple conditions that are checked one by one until the first true condition is found. Then, the corresponding block of statements is executed, and MATLAB skips to the end. If none of the conditions is true, then none of the statements in the blocks will be executed. The if else if else-statement is very similar to the if else if statement. The only difference is that we add an else branch after the last else if branch. The else block at the end will be executed only if all the previous conditions are false. But because of that else branch, exactly one block of statements will always be executed. [MUSIC] [APPLAUSE]