Вы находитесь на странице: 1из 8

TUTORIAL 1

Introduction to Matrix Calculation using MATLAB

TUTORIAL 1

INTRODUCTION TO MATRIX CALCULATION USING MATLAB

Learning objectives
Getting started with MATLAB and its
user interface
Learn some of MATLABs commands
and syntaxes
Get a simple introduction to use of
MATLAB to collect data in matrix or
array form
Learn how to manipulate data in matrix
form

1. Introduction
This section introduces MATLAB through a step-by-step procedure of simple matrix
operations. The name MATLAB stands for MATrix LABoratory. The program is
specially developed for numerical calculation and visualization or plotting of data that is
mostly represented in matrix form.
Matrices that are also symbolized by arrays represent the most important data elements
in MATLAB. A list of elementary matrices and matrix operations is available by writing
the following command in the command window.
>> Help elmat

Starting MATLAB:
Start Matlab R2014 (server)
MATLAB starts with the following standard windows.
1. Command Window this window contains command lines where we write
codes and execute MATLAB expressions.
2. Workspace shows the variables that are generated since the current session
started.

Prepared by Hirpa Lemu./UiS - 31.07.2017 1


TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

3. Current Directory shows which directory MATLAB is currently using as a


working directory and the files that are saved in the directory.
4. Command History shows all executed commands both in the current and
previous sessions. Note that new sessions start at every start up of MATLAB.

Before starting the step-by-step tutorial, please notice the use of the following three
symbols:
>>: Command line(s) coming after this symbol are executable. Write the commands
after this symbol in the command window and press the enter-key, and observe
how MATLAB reacts.
: Exercise questions appear right after this symbol. Users are expected to write
appropriate MATLAB codes/commands to answer these questions.
%: Comment line - everything that comes to the right of the percent symbol % on a
line is comment and cannot be executed by MATLAB. As a user, you do not
need to write these comments as part of solving the exercise in this tutorial. The
comments are for your information.

2 Prepared by Hirpa Lemu./UiS - 31.07.2017


TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

2. Constructing matrices

A matrix has the dimensions mxn where m = number of rows (lines) and n = number of
columns. An element in matrix A located at row i and column j is denoted by A(i, j).
Rows are separated by using either a semicolon (;) or by the enter-key.
Columns or elements in a row are separated by a space or a comma (,).
1 2
For example: to setup the 2 x 2 matrix A = in MATLAB
3 4
>>A=[1 2;3 4] % Here the two columns are separated by spaces and the two rows
% are separated by a semicolon.

>> A=[1,2;3,4] % Here the columns are separated by a comma.

>> A=[1 2
3 4] %Here the columns are separated by spaces and rows by the enter-key.

Notice that MATLAB will always produce screen outputs for each input. One can
suppress the display of outputs on the screen on any command using a semicolon
(;) at the end of the command (i.e., before pressing the enter-key). This is
particularly important for large matrices.
For example, compare how MATLAB reacts for the following two commands:
>> A1=[1 2;3 4]
>> A2=[1 2;3 4];

An element of a matrix can be a real number, a complex number or an expression.


1 2i
For example, the matrix B = is valid and can be given in MATLAB as follows
3 4 + x
>> x = sqrt (10)
>> B=[1 2i;3 4+x]

A row vector is a 1-by-n matrix. That means it contains only one row. A row vector can
be constructed, for example, using the following command line:
>>L1 = [2 3 4 5 6]
or
>>L2 = [2:6] % A vector with element increment by 1.

A column vector is an m-by-1 matrix. That means it contains only one column. A
column vector can be constructed, for example, using the following command line:
>>L3 = [2; 3; 4; 5; 6]
Prepared by Hirpa Lemu./UiS - 31.07.2017 3
TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

Concatenation of a matrix: A larger matrix can be constructed by concatenating


existing matrices or vectors.
For example,
>> x=[1;3;6 ]; y=[2;4;6]; % Constructs two column vectors, x and y
>> M =[x,y] % Matrix M is constructed from matrix x in column 1 and y in column 2

>> L =[x;y] % Constructs a 6x1 matrix (a column vector) with elements from x and y.

(Exercise)
Construct a row vector from the above two vectors x and y.

Dimension of a matrix the size of a matrix, for example, that of matrices M and L
constructed above can be found using the command:
>>[m,n] = size(M)
>>[m,n] = size(L)

Addressing elements of a matrix an element of a matrix can be accessed as follows


>>w = M(3,1) % This gives the element in row 3 and column 1 of matrix M and
% assigns it to w.

The value of a given element of a matrix is given as follows:


>>M(5,1)=7 % This allocates the value of element x51 in M to 7.

3. Simple matrix operations

MATLAB uses the following simple arithmetic operations on matrices. The precedence
rules of the operations follow the given sequence.

Operator Matrix operations Array operations


+ Addition
- Subtraction
* Multiplication .* Element for element multiplication
/ Division ./ Element for element division
\ Left division
^ Power
Transpose

4 Prepared by Hirpa Lemu./UiS - 31.07.2017


TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

a) Addition/subtraction
Addition and subtraction of matrices is defined element-by-element. These operations
require that both matrices must have the same dimension, or one of them must be a
scalar.
Lets first construct two matrices A and B, having the same dimension.
>>A=[2 3 5;0 3 4], B=[1 2 3;3 2 1]
>>S1=A+B
>>S2=B+A
>>S3=A-B
(Exercise)
Show that matrix addition is both commutative and associative.

b) Multiplication by a constant or a scalar


>>s=10 % This assigns the value 10 to the variable s.

>>Smul1 = s*A
>>Smul2=10*B

c) Matrix multiplication and element-by-element multiplication


Let us construct two 3-by-3 mtrices using the built-in functions Pascal and Magic;
P= pascal(3) % Defines a Pascal matrix of dimension 3-by-3.

M = magic(3) % Defines a Magic matrix of dimension 3-by-3.

Mmul = (M*P)
Emul = (M.*P)
(Exercise)
What is the difference between the two multiplication operations M*P and M.*P?
What conditions should be fulfilled to execute the two operations?

d) Transpose of a matrix
MATLAB uses the apostrophe operator () as the transpose operator. The transpose
operation flips the matrix about its main diagonal.
For example, observe how MATLAB reacts for these two commands.
>>T1 = P
>>T2 = M

Prepared by Hirpa Lemu./UiS - 31.07.2017 5


TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

(Exercise)
Construct two matrices A and B with arbitrary and compatible sizes and show that
(A+B)T = AT+BT and
(A*B)T = BT*AT

Lets assume that the following two matrices are given:


>>A = [2 -1;0 3; -4 1]; B = [4 -3;1 2; -2 5];
Determine the products: ABT, BAT, AAT and ATA.
What do you observe from the results?

4. Special, square matrices


MATLAB has many built-in functions and matrices that can be easily picked and used.
Below are some of commonly used square matrices.
a) Null matrix:
>>NULL = zeros(n) % where n = size of a square matrix with all elements equal to zero (0).

b) Identity matrix:
>>ID = eye(n) % where n = size of a square matrix with all elements equal to one (1).

c) Diagonal matrix:
>>D = diag([3, 4, 6, 8]) % gives a diagonal matrix with 3, 4, 6 and 8 along the main diagonal.
d) Scalar matrix:
>>S = 5*ID
e) Triangular matrices: We have two forms of triangular matrices
1. Lower trinagular matrix: A matrix with all null entries over the main diagonal.
Lets assume a random matrix A. The operation tril(A) gives a lower triangular
matrix.
>>A=magic(4); L = tril(A)
2. Upper triangular matrix: A matrix with all null entries below the main diagonal.
The operation triu(A) gives an upper triangular matrix.
>> U = triu(A)
f) Submatrices: Lets construct the following 4-by-4 matrix
>>B=[10 15 20 25;30 35 40 45;50 55 60 65;70 75 80 90]
We can create submatices from this matrix, for example
>>B1= B(1:4,1:2) % Constructs a submatrix from the two columns

>>B1= B(3:4,3:4) % Constructs a 2-by-2 submatrix from the lower right hand entries.

6 Prepared by Hirpa Lemu./UiS - 31.07.2017


TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

(Exercise)
Construct a 3-by-3 submatrix from matrix B.

5. Computation of determinants
The determinant of a square matrix A can be found using this very simple command in
MATLAB:
>>A = [1 2 3;4 5 6;7 8 9];
>>d = det(A) % Notice that a determinant is a scalar value.

>>B=A; % This copies the entries of the transpose of matrix A to matrix B.

>>db=det(B) % Evaluates the determinant of matrix B and saves the scalar value in db.

(Exercise)
What do you conclude from the results of the two determinants, det(A) and det(B)?

6. Linere ligningssystemer
A linear equation of the form Ax = b can be solved for the unknown variable x using the
MATLAB command:
>> x = A\b
(Exercise)
We have seen the following system of linear equations in the lecture:

x1 + 2x 2 + x 3 = 4
3x1 + 8x 2 + 7 x 3 = 20
2 x1 + 7 x 2 + 9 x 3 = 23
Formulate the equations in a matrix multiplication form (Ax = b) and find x values
using MATLAB.

7. Inverting matrices
As shown below, matrix inversion is a very simple operation in MATLAB.
>> x = inv(A)*b

(Exercise)
Observe that x = A\b = inv(A)*b

Prepared by Hirpa Lemu./UiS - 31.07.2017 7


TUTORIAL 1
Introduction to Matrix Calculation using MATLAB

8 Prepared by Hirpa Lemu./UiS - 31.07.2017

Вам также может понравиться