Theory, 7ex Matlab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Elements of Matlab

Instructions are for Matlab. However, much of Matlab's syntax also works in the clone
software Octave, which can be downloaded for home use from
https://www.gnu.org/software/octave/download.html
If it is known that something does not work in Octave as described in the instructions, it
has been written in blue text. Matlab can be used in most of the school's computer labs; for
more information, check the address
http://secure.puv.fi/software-search/

1 Basic Arithmetic in Matlab

Matlab can be opened simply by clicking on the software icon. Alternatively, it can be
opened by invoking it from the command line. Similarly, the program can be closed by
clicking the close button or, if desired, by typing quit on the command line.

In addition to being a computational software, Matlab is also a programming language. The


program can be used for short and simple operations directly from the command line. For
longer operations, it is more sensible to open the editor and create a separate script (m-le).
Once the script is ready and saved, it can be called from the command line, and Matlab will
execute all the script commands.

In these exercises, Matlab is used directly from the command line to get a feel for it. The
program can be used like a normal calculator for basic arithmetic operations. The >> on the
command line means that the program is waiting for a command. For example, by typing
2+5 and pressing Enter, the program will display the result of the calculation on the screen.
Try what happens if you type the following commands:

1 >> 3−2 <Enter>


2 >> 2*3 <Enter>
3 >> 1/2 <Enter>
4 >> 23 <Enter>
5 >> 2\11 <Enter>
6 >> 2^4 <Enter>
7 >> 2^(−1) <Enter>

1
Note that the decimal separator must be a dot instead of a comma. The program won't give
you the result you expect if you type, for example, 2*1,5.

1 >> 2*1,5 <Enter>


2
3 ans =
4
5 2
6
7
8 ans =
9
10 5

2 Dening Variables

Variables can be dened in Matlab's memory by rst writing the desired name, followed
by an equals sign and then the value, for example, a=5. This is stored in memory, and if
afterwards you type, for example, a*5, the program will display 25 on the screen. Note the
dierence from Mathcad! If before pressing Enter you add a semicolon ;, the program will
still save the variable but won't display it on the screen. Try the following commands if you
wish:

1 >> b = 3; <Enter>
2 >> x1 = 2; y2 = 3; <Enter>
3 >> z = x1 + y2 <Enter>
4 >> z=z+8 <Enter>

3 Vectors and Matrices

Matlab's eciency is based on its eective use of vector and matrix calculations. A matrix is
input by writing the elements of the matrix inside square brackets []. You can change to a
new row by pressing Shift + Enter. Also, a semicolon is interpreted as a new line. Elements
can be separated by a space or a comma, either works.

1 >> [1 2 3] <Enter>
2 >> [2,4,5] <Enter>
3 >> [1 2 3] +[2,4, 5] <Enter>
4 >> [1 2 3
5 2 3 4
6 7 8 9] <Enter>
7 >> [1, 2, 3; 2, 3, 4; 7, 8, 9] <Enter>

Variables can also be dened as a vector or matrix. Try the following commands if you wish:
1 >> A=[1 2 3] <Enter>
2 >> B=[2;4;5]; <Enter>
3 >> A−B= <Enter>

Matrix multiplication can be calculated using the asterisk *, and exponentiation can be done
using the caret ^. The inverse of a matrix, when it exists, can be calculated using ^(−1).
The transpose can be obtained with transpose(), where the matrix in question is enclosed
in parentheses, or by typing .'. Try the following commands if you wish:

1 >> A=[1 2 3] <Enter>


2 >> B=[2;4;5]; <Enter>
3 >> C=A*B= <Enter>
4 >> C^3 <Enter>
5 >> C^(−1) <Enter>
6 >> C.' <Enter>
7 >> transpose(C) <Enter>

Often, there is a need to create a vector whose members are from an arithmetic sequence,
i.e., the members are numbers with a constant dierence, which is the same for all members.
This can be done with the command start:step:end, where now the written members
must be replaced with numbers. The program gives a row vector whose members are the
members of the arithmetic sequence in increasing order. For example:

1 >> 1:2:11
2
3 ans =
4
5 1 3 5 7 9 11
6
7 >> 1:0.2:3
8
9 ans =
10
11 1.0000 1.2000 1.4000 1.6000 1.8000 2.0000 2.2000 ...
2.4000 2.6000 2.8000 3.0000

If the step is not specied, the program assumes it to be 1.

1 >> a=1:10;
2 >> a
3
4 a =
5
6 1 2 3 4 5 6 7 8 9 10

To convert a row vector a to a column vector or vice versa, simply transpose the vector, for
example by typing a'.

You can clear the command window with clc. If you want to know what variables are
currently in memory, you can display them on the screen with whos. You can remove a
single variable, such as the variable a, with the command clear a. Note that once a
command is given, i.e., Enter is pressed, it cannot be undone! The Undo and Redo
functions requested from the menu or keyboard only aect what is currently written on the
command line.

You might also like