Scalar arithmetic and introduction to built in functions
Matlab can be used as a calculator. Just type the calculation in the command window (which has a prompt that looks like this >>) and then hit return:
>> 2*2
ans =
4
If you type something that doesn't make sense you will get an error message:
>> 2 * / 5
??? 2 * / 5
|
Error: Unexpected MATLAB operator.
Notice that Matlab attempts to show you where you have made a mistake with a line pointing to the symbol it doesn't understand.
Press the up arrow to edit and re-type the line correcting the error.
Matlab has hundreds of built-in functions eg:
>> sqrt(2)
ans =
1.4142
A definitive list of the functions is available in the product help. The list of available functions depends which toolboxes (packages) you have installed. Here are some for you to try that are available on all installations because they are not part of any additional toolbox:
>> sin(3)
>> exp(1)
>> mod(10,3)
>> factor(123)
If you want to know more about the functions and how to use them then type 'help' followed by the name of the function
>> help factor
Or you can press F1 to look up the function in the product help files.
You can type in expressions of more or less any length and complexity. For example if you want to know the length of the hypotenuse of a triangle with sides 3 and 4 you might try
>> sqrt(3*3 + 4*4)
ans =
5
For some expressions it is possible to type things that give answers, but do not give you the answers you want! This might be because of the order in which the calculation is done. For example:
>> 7 + 4 / 2
ans =
9
the mean value of 7 and 4 is not 9. This expression gives you 7 + (4 / 2) not (7 + 4) / 2. To calculate the mean value of 7 and 4 we need:
>> (7 + 4) / 2
ans =
5.5000
The brackets ensure that the addition is done before the division.