Lesson 1 introduces the most basic features of MATLAB.
By the end of this lesson, you should be able to:
To Read | Lesson pages |
---|---|
To Do |
|
To Deliver | Quiz in Canvas |
If you have questions, please feel free to post them to the Questions? forum in Canvas. I will check this forum daily. While you are there, feel free to post your own responses if you, too, are able to help a classmate.
+=ans;whosiskeyword
Once you've installed MATLAB and started it, or logged into it through PSU's webapps, you are presented with an interface that looks like this.
The central panel is the Command Window, where you can type what you want MATLAB to do. Go ahead and put your cursor in there right now. Type an obvious math problem such as 3 + 4 and then hit return. You should get back:
>> 3+4 ans = 7 >>with a blinking cursor after the last >>
Good! You're on the way to being an expert already. The ans is short for "answer" and it is also a temporary variable that you could use in the very next command. Try typing ans+2 and you'll see that MATLAB will reassign the value 9 to ans.
To assign a variable in MATLAB first type the name of the variable and then use a single equals sign to set that variable equal to whatever value you want to give it. For example, to make a variable called "a" that is equal to 3, type a=3 into the command window. MATLAB tells you:
>>a= 3 a = 3 >> |
If you don't want MATLAB to repeat everything back to you, type a semicolon at the end of your command, as in a=3;
You can also tell MATLAB to assign the results of a calculation to a variable. For example, type b=a+4;
MATLAB has now behind the scenes assigned the value 7 to b. If you look over at the Workspace panel on the right, you'll see a listing of variables there. Another way to find out what variables are currently in use the the command whos. If you ever want to know the value of a variable without looking at the Workspace panel you can just type the name of the variable and hit return.
Variables can't start with a number and cannot have spaces in them, but they can be a whole word or phrase as long as you jam the words together. Most people use underscores or camel case for readability. Variables are also case sensitive. Some people like to use conventions such as capitalizing matrices and using lowercase for vectors and scalars. MATLAB doesn't care how you go about it, but sometimes adhering to a consistent naming scheme makes your programs more human-readable, thus making you a more popular collaborator.
Variable | OK? | notes |
---|---|---|
x | yes | |
X | yes | capital X is a different variable than lowercase x |
1x | no | don't start a variable name with a number |
x1 | yes | variables can have numbers in their names as long as they aren't first |
eliza is cool | no | variable names can't have spaces, even if the sentence is a true statement |
eliza_is_cool | yes | totally |
elizaIsCool | yes | camel case is fine |
eliza.is.cool | no | don't use punctuation other than the underscore because most of them have special values |
for | no | don't use any keywords that are on the reserved list. the command iskeyword tells you what's on the list |
fourScoreAndSevenYearsAgo | yes | but there isn't room for the whole Gettysburg Address because variable names can have a maximum of 63 characters |
helplookfor-*/\^( )
Time to introduce my two favorite MATLAB commands: help and lookfor. If you type help commandName MATLAB will print to the screen the manual page for that command together with examples. This is a great resource, but it only works perfectly well if you already know the name of the command and you are merely wanting to double check its usage. Sometimes you can't remember the exact name of the command but you have a guess, or at least you have an idea that there is a command that does something similar to what you want. In this case lookfor is your new best friend because it will find and list all the commands that have part of the word you typed. Try it out for yourself!
MATLAB works like a calculator and supports the obvious math operations commonly found on your keyboard such as + (addition) -(subtraction) *(multiplication) / \ (left and right division). Use ^ to raise a number to a power. For example, if you type a=3;b=4;c=b^a MATLAB reports back to you that the value of c is 64. That's because you set a equal to 3 and you set b equal to 4 and then you set c equal to b raised to the power of a. 4 cubed is 64, so we're good.
Other algebraic expressions work as expected. For example, raising a number to a negative power is equivalent to writing 1 over that number raised to the same power. For example, following on our previous work in which a = 3 and b = 4:
>>a^(-b) ans = 0.0123 >> 1/(a^b) ans = 0.0123
Precedence refers to the order in which mathematical operations are executed. If you can recall back to any algebra classes you took before college, you probably remember the rules of precedence. MATLAB follows the standard rules, which are
If you want to change the order, then use parentheses. Expressions are evaluated from the innermost set of parentheses outwards, with the rules listed above applying within each nested set of parentheses.
log log10 sin sind pi
One of the rare counterintuitive (to me anyway) commands is log. When I see "log" written I always assume base 10, whereas "ln" means natural log (base e). But that's not how MATLAB expresses it. In MATLAB,log means natural log. If you want base 10, use log10.
Surprise #2 is that the default for trig functions (sine, cosine, tangent, etc.) is radians. To convert to degrees instead, multiply your angle by pi/180 or else use sind. "pi" is a "special variable" in MATLAB. By default you can just type the word to use it. The "d" in sind to make MATLAB use degrees also works for other trig functions, like, for example, cosd and tand.
Here's a tip: If you are ever working with a programming language, spreadsheet program, or calculator and you aren't sure whether you are working in degrees or radians, just try calculating the trig function of an angle that has an obvious well-known answer, such as the sine of 90 degrees:
>> sin ( 90 ) ans = 0.8940 >> sin ( 90 *pi/ 180 ) ans = 1 >> sind( 90 ) ans = 1 |
Reassigning a variable to a new value does not automatically re-perform any past calculations involving that variable. For example, see the following set of commands:
>> a= 3 ;b= 4 ; >> c=a+b c = 7 >> a= 5 ; >> c c = 7 |
In this series of operations, I first set a equal to 3 and b equal to 4. I set c equal to the sum of a and b. MATLAB tells me c is 7. So far, so good. Then I set a equal to 5. In a spreadsheet program, c would be recalculated to take into account the new value of a but MATLAB doesn't do that, which I can verify by typing c to find out its value. MATLAB tells me c is still 7.
Take the time to work through these problems to check your grasp of the concepts introduced so far. Tip! This is an ungraded quiz for self-checking purposes. You should start MATLAB and try out for yourself anything that you don't know the answer to.
add summary here
You have reached the end of Lesson 1! Double-check the to-do list on the Lesson 1 Overview page [1] to make sure you have completed all of the activities listed there before you begin Lesson 2.