Lesson1

advertisement
LESSON 1
TOPIC 1. Scalars. Arithmetic Operations.
Task 1. Start a Matlab session.
“…The name MATLAB stands for MATrix LABoratory…Matlab is a high-performance
language…whose basic data element is an array that does not require dimensioning…”1
Run Matlab from the list of programs or double click on its icon on the Windows desktop. If
there is no one, then
1. Open Matlab\ bin\ folder;
2. Right click on matlab.exe file and run “Create Shortcut” command from the popup
menu;
3. If the shortcut is offered to be created on the Desktop, press “Yes”. Otherwise, cut the
newly created shortcut (“Ctrl” + “X”) and paste it on the Desktop (“Ctrl” + “V”).
The opened “Matlab Command Window” is the main place – workspace, where commands
are entered and the corresponding results are computed. It has a command line, marked by “»”
prompt, where, as usually, the insertion point (blinking bar) indicates the position of characters
to be entered.
ATTENTION: Commands are executed on pressing of “Enter” key.
Task 2. Estimate monthly expenses of electricity use in an imaginary apartment.
Assume the apartment has 6 bulbs of 100 Watt/h each, 1 heater of 2kWatt/h and a videoaudio electronic system of 2.5 kWatt/h. Also assume every day 3 bulbs and the heater are
switched on during 2 morning hours and, in evenings, all 6 bulbs are on during 4 hours and the
heater and the electronics are operated for 3 hours.
The standard list of Arithmetic Operators on numbers – scalars, in Matlab is shown below in
increasing order of their precedence:
 Addition “+” and subtraction “-” – these are of the lowest precedence and executed after all
other operations in expressions;
 Multiplication “*” (“Shift” + “8”), right division “/”and left division “\”– these are executed
before additions and subtractions;
 Rising to a power “^” (“Shift” + “6”) – it is executed before multiplications and divisions;
 Parenthesis “(” (“Shift” + “9”) and “)” (“Shift” + “0”) – everything between them has the
highest precedence and executed first.
ATTENTION: The right division reads from left to right. For example, 6 / 2 results in 3. The left
division reads from right to left. The same example can be equivalently executed
as 2 \ 6 and will produce the same result of 3. Obviously, 6 \ 2 is equivalent to 2 /
6 and results in 0.3333.
1. The first command is an easy computation of the daily use of the electricity:
» (3 * 0.1 + 2) * 2 + 6 * 0.1 * 4 + (2 + 2.5) * 3
There is no need in spaces before and after operators and operands. Such spaces,
however, essentially increase the readability of commands. The prompt mark, of course,
must not be typed – it simply shows that the given expression is a Matlab command. Finally,
do not forget to press “Enter” key, in order to see the result.
2. The result is computed as follows:
ans =
20.5
1
Getting started with Matlab, The MathWorks, Inc., p. 1-2 [http://www.mathworks.com]
There is nothing interesting in the numeric outcome. The important point is that Matlab
stores this result in the automatically created variable ans – a room in the computer’s
memory, the contents of which can be referred to by the variable name and used in future
computations.
3. Having the variable ans, it is quite easy now to compute the average monthly expenses:
» ans * 30 * 25
The result is again placed in the same variable ans:
ans =
15375
ATTENTION: Every time a new value is placed in a variable, its old value is lost. For example,
before the second command, the value of ans is 20.5, while, afterwards, it
becomes 15375.
4. It is pretty apparent that one variable cannot keep both numbers for later references. The
solution is in creation of other variables. In order to create a variable, its value or the
expression resulting in it must be assigned to its name:
» daily_use = (3 * 0.1 + 2) + 6 * 0.1 * 4 + (2 + 2.5) * 3
Now, the result is kept in a new variable called daily_use:
daily_use =
20.5
Modifying Existing Commands
The last command does not require lengthy input. Instead, activate the very first command
line by pressing “” key twice. Single press brings in the most recently executed command. All
earlier commands can be reactivated on more presses. They can appear in opposite order as well,
on pressing of “” key. When the necessary line appears, modify it using “”, “”, “Delete”
and “Backspace” keys.
Variable Names
There are rules valid variable names must satisfy. They start from a letter and consist of
arbitrary amount of letters, digits and underscores. Matlab, however, recognizes them by their
first 31 characters only. They are case sensitive. For example, daily_use and Daily_Use are
two different variables. In order to see the value of a variable, simply type in the command line
its name. For example,
» daily_use
daily_use =
20.5
5. The average monthly expenses are computed in similar fashion, if the price for 1 kWatt is
assumed to be 25 drams:
» el_exp = daily_use * 25 * 30
el_exp =
15375
6. Now, it is easy to estimate the electricity expenses in February – just recomputed the variable
el_exp:
» el_exp = daily_use * 25 * 28
el_exp =
14350
Task 3. Implement a “tricky” game, where Matlab will guess some suitably prepared
hidden digit.
The game has the following rules:
1. Write down on a piece of paper some integer number of at least 4 digits.
2. Obtain another integer by mixing up in arbitrary order the digits of the initial one.
3. Subtract these numbers from each other.
4. Discard all zeros from the result and keep in mind one of the digits.
5. Provide Matlab with the remaining digits.
6. Matlab will restore the missing digit.
The game is based on a simple trick: both the number under the step 3 and the sum of its
digits can be divided by 9, no matter what the initial choice was. Therefore, the sum of the
reported digits must be divided by 9 and the remainder must be subtracted from 9, in order to
reveal the missing one.
Let’s assume the arbitrary chosen number is 946572, and, after mixing up the digits, the
second one is 265497. The difference, then, is 681075, from where, after discarding 0, 68175 is
obtained. Now, let’s memorize 8 and provide Matlab with 6, 1, 7 and 5.
1. Arrays are natural objects for keeping several values. Four elements will be put in a single
row as follows:
» known_row = [6, 1, 7, 5]
known_row =
6
1
7
5
A single row is called a row-vector. In order to create a row-vector, specify its elements
between brackets [] separating them by comma or space. For example, the above command
could be executed as
known_row = [6 1 7 5]
2. The same values can be placed in a single column, as follows:
» known_column = [6; 1; 7; 5]
known_column =
6
1
7
5
A single column is called a column-vector. In order to create a column-vector, specify its
elements between brackets [] separating them by semicolon or “Enter” mark. For example,
the above command could be executed as
known_column = [6
1
7
5]
3. In order to compute the sum of elements in a vector (row or column), use the function sum():
» known_sum = sum(known_row)
known_sum =
19
Matlab Built-in Functions
Matlab has rich collection of functions. Each function is a Matlab program that takes known
input data as arguments and produces the corresponding result.
Getting Matlab Help
Fast and quality explanations of functions, operators and general topics can be obtained from
the help command, which can be launched in the following two ways:
a. From the “Help” menu of the “Matlab Command Window” run “Help Window”
command.
b. Double click on a help topic of the interest in the “Matlab Help Window”.
c. Double click on the specific subtopic of the opened topic.
d. After reading the article, it is very useful to double click on its “See also …” link.
OR
a. Enter in the command line the following simple command, in order to read the entire list
of the help topics in the “Matlab Command Window”:
» help
b. Enter the same command, followed by the name of the specific topic, in order to read the
corresponding article. For example,
» help sum
ATTENTION: Every time the help article of a function is to be opened, specify in help
command the function name without its arguments.
The command help is useful when the exact name of a function, an operator or a general
topic is known. Even a slightest mistyping will result in “not found” error. For example:
» help sun
sun.m not found
Usually, however, it is easier to start the search from a keyword, rather than the exact name.
All the topics that include a string “sum” can be found by the command lookfor:
» lookfor sum
Once the list of those topics that include the keyword is produced, choose the most suitable one
and open its help article in the ways discussed above.
4. Now, in order to reveal the unknown digit, the remainder after division of known_sum by 9
must be subtracted from 9:
» unknown = 9 - rem(known_sum, 9)
unknown =
8
rem(x, y) – returns the remainder after division of x by y. It has the same sign as x. For example,
rem(5, 3) = 2; rem(-5, 3) = -2 and rem(5, -3) = 2.
mod(x, y) – returns the modulus (the signed remainder after division of x by y). It has the same
sign as y. If x and y have the same sign, then rem(x, y) – mod(x, y) = 0. Otherwise,
rem(x, y) – mod(x, y) =  y. For example, mod(5, 3) = 2; mod(-5, 3) = 1 and
mod(5, -2) = -1.
Download