Description: This video lecture introduces the MATLAB interface, how to carry out basic mathematical operations involving both numbers and variables, and how to use the help files to learn more about MATLAB's built-in functions.
Instructor: Yossi Farjoun
YOSSI FARJOUN: This is Dr. MATLAB, Lecture 1-- Using MATLAB for the First Time.
When you turn MATLAB on for the first time, you'll be greeted with a window that looks like this. The first thing we do is close all the windows that we don't need initially-- Current Directory, Workspace, and Command History. We're left with a command window. This is the command prompt. And this is the flashing cursor where our input will be placed.
The first thing we want to do is try using MATLAB as a giant calculator. So for example, we can ask MATLAB to calculate 1 plus 1. The answer is 2. So we type what we want, 3 plus 4. Hit Enter and we get the answer-- 3 times 4, 1 plus 2 times 3, 2 to the power of 4, 5 to the power of 3, et cetera.
We can also ask whether two numbers are the same, or whether one is greater than the other. So to see if 4 is greater than 5, we type this. This will tell us that the answer is no, 4 is not greater than 5. We can ask is 4 less than 5. The answer is yes, 4 is less than 5.
Sometimes we need a function and expression, not just an operator. So for example, to find the remainder, we need to use the function rem. Here is the remainder of 15 when divided by 4-- rem(15,4). The answer is 3. To find out more about a function, we use the help command. help rem will tell us everything we need to know about rem-- the remainder after division. There's a bit of an explanation, some conventions. And the "see also"-- this is perhaps one of the most important parts of the help file-- is to help you find other commands that are useful and related to this command. I encourage you to try to find the difference between mod and rem.
Other than calculating expressions, we can also assign their value into variables. So for example, if I want to create a variable named x, I type x equals and then the value that I want. So for example, x=1 will create a variable called x and put 1 as the value into it. I can also put the result of an expression into a variable. Here's y=2*3. Of course, that's 6. So it calculates the 6 and puts 2 times 3 into it. So now y is 6 and x is 1. And I can use x and y if they are numbers. x+y, the answer is 7. If I try to use a variable that doesn't yet exist, for example z+y, I get an error-- undefined function or variables.
Sometimes, you're looking for a command but you don't know the command. You know that you're looking for something, bu you just don't know what it's called in MATLAB. In that case, you should be using lookfor to find the command you're looking for. So for example, lookfor square will give you all the commands that have the word square in their help file. Of course, I was looking for, in this case, square root. And here it is. I can either click on this and that will give me the help file of the square root. Or I can just type help sqrt. And that will give the same.
Square root finds the square of the elements. So if I do sqrt(4), I get 2. Notice that I get complex results if I get a non-positive input. So sqrt(-3) is the square root of three times i, which is, of course, very nice to know. So 2i^2 should give me -4, and indeed, 2i squared gives me -4.
On the command line, there is a useful trick involving the "up" button. Sometimes, you're looking for something or you're typing something and you make a typo, like this. I've swapped the L and the E. Now I could retype everything from scratch, or I can press the up key-- up arrow-- and then the left arrow until I can delete the E, and put it in the right place. Now when I hit Enter and I get the help info.
With the up arrow and the down arrow, I can browse through the history of all the commands that I did. If I have a longish expression on the command line and I want to erase the whole thing, I could start erasing it. Or I could press Control-C.
That's probably enough for today. To exit MATLAB, I either look for the Quit MATLAB here or I can Apple-Q. Or from the command line, I can type exit.