Mauro Borgo , Alessandro Soranzo and Massimo Grassi MATLAB for Psychologists 2012 10.1007/978-1-4614-2197-9_1 Springer Science+Business Media, LLC 2012
1. Basic Operations
This chapter gives an overview of MATLAB and compares MATLAB with a scientific calculator. The chapter gives also an overview of basic arithmetic operations and functions as well as a short introduction to matrices and matrix manipulation.
It is supposed that you have already installed MATLAB on your computer. When you start MATLAB, the MATLAB desktop opens, as shown in Fig. (or something similar, depending on your MATLAB version). In this first chapter we refer only to the Command Window, where the special >> prompt appears. The other windows have the following meaning:
Fig. 1.1
The MATLAB desktop. MATLAB release 2011b
The Workspace Window contains a list of variables that are in use in the working session.
The Command History contains the list of all commands you have typed in the command window.
The Current Folder window shows the list of the files contained the folder you are working on.
When the prompt >> is visible, this means that MATLAB is waiting for a command. You can quit MATLAB at any time in either of the following ways:
Select Exit MATLAB from the desktop File menu.
Enter quit or exit after the command window prompt >> , and press the Enter key.
Alternatively, select File with the mouse from the top menu bar, and then exit MATLAB.
Observe that the tab above the Workspace shows the Current Directory Window. For example, in the Windows operating system, the path might be as follows: C:\MATLAB\Work, indicating that directory Work is a subdirectory of the main directory MATLAB, which is installed in drive C. Clicking on the arrow in the Current Directory Window shows a list of recently used paths. Clicking on the button to the right of the window allows the user to change the current directory. Knowing which is the current path is fundamental: from the Command Window you have access to the file stored in the given directory. It is, of course, possible to change your working directory.
Before continuing our introduction to MATLAB, we want to highlight a very useful window: the HELP Window. This window is the most useful window for beginning MATLAB usersand for expert users as well: select Help PRODUCTHELP from the top bar menu. The Help Window has most of the features you would see in any web browser, including clickable links, a back button, and a search engine. All MATLAB commands and functions are explained with examples: you have simply to search for the desired word.
Now let us begin with a description of the MATLAB language. The word MATLAB is the concatenation of the words MATrix LABoratory, meaning that MATLAB is an interactive software system for numerical computation, especially designed for computations with matrices. Before going into the details of matrix computations, let us first see how to use MATLAB to do simple arithmetic operations: Type 1+1 after the >> prompt, followed by Enter; that is, press the Enter key, as indicated by
>> 1+1
MATLAB gives its quick answer by displaying the following message:
ans =
You can perform other arithmetic operations, such as multiplication, subtraction, and division, and MATLAB always returns the correct result. If such is not the case, there is certainly something wrong with what you typed. For example, you can try the following operations (type the operation after the >> prompt followed by Enter):
To TYPE after prompt >> followed by Enter | MATLAB answer | Meaning of the operation |
---|
35*12 | ans = | Multiplication |
2/45 | ans = 0.0444 | Division |
| ans = | Subtraction |
2^3 | ans = | Exponentiation |
Note that to type numbers such as the Avogadros number 6.023 1023, you can either write the expression 6.023*10^23 or you can represent the number in scientific notation. To enter Avogadros number in scientific format, type 6.023e23 ,where 6.023 is the mantissa and 23 is the exponent. Mantissa and exponent must be separated by the letter e (or E):
>> 6.023*10^23 ans = 6.0230e+023 | >> 6.023e23 ans = 6.0230e+023 |
Such numbers are also defined to be floating point.
MATLAB warns you in the case of in invalid operation or unexpected results. What do you think MATLAB will show us if we type 12/0 or 0/0? Lets try it:
To TYPE after prompt >> followed by Enter | MATLAB answer | Meaning of the answer |
---|
12/0 | ans = Inf | You should not divide by zero, but if you do, the result is Infinity |
0/0 | ans = NaN | Unable to find the answer, so the result is NaN = Not a Number |
11+ | ??? 11+ | Error: Expression or statement is incomplete or incorrect | If you want to perform this operation, you must complete the expression with another term |
As you can see, MATLAB is unable to stay quiet. It quickly answers your commands by displaying something in the command window. In the previous cases, the answer was a special value such as Inf (Infinity) or NaN (Not a Number) . You can use these special values on their own, typing, for example:
>> 12/Inf
ans =
MATLAB can be used as a scientific calculator, combining several operations in one expression. The computation of the expression is based on well-known mathematical rules. In particular, some operations are performed before others, based on precedence rules, which are given in the following table:
Precedence | Operator |
---|
| Parentheses (round brackets) |
| Exponentiation, left to right |
| Multiplication and division, left to right |
| Addition and subtraction, left to right |
If you want to know the result of the operation {2+[5*3/(75)2]/3} you have to type:
>> (2+(5*3/(7-5)^2)/3)
ans =
3.2500
In this example, MATLAB first calculates (75) = 2, then it squares 2^2 = 4, then it performs the multiplication 5*3 = 15 (multiplication left to right), and then divides the result by the previously computed result, i.e., 15/4 = 3.75. The result in brackets is divided by 3 (3.75/3 = 1.25) and then added to 2, giving the result. Note that in MATLAB, parentheses are always given by round brackets.