Professional Documents
Culture Documents
WEEK 1
INTRODUCTION TO MATLAB
BASIC CALCULATIONS
VARIABLES
ARRAYS
Contents
Objective .......................................................................................... 3
Introduction .................................................................................... 3
Simple expression............................................................................6
Variables ......................................................................................... 7
Arrays ..............................................................................................9
6.1
Vectors .......................................................................................................... 9
6.2 Matrices....................................................................................................... 12
6.2.1 Size of the matrix ................................................................................ 12
6.2.2 Transpose of a matrix ........................................................................ 13
6.2.3 Inverse of a matrix ............................................................................. 13
6.2.4 Solving system of linear equations .................................................... 14
6.3 Referencing array elements ........................................................................ 15
Page | 2
Objective
Introduction
MATLAB is derived from MATrix LABoratory which is invented by Prof. Cleve Moler
(MathWorks Inc.) in 1970s. MATLAB is one of the top 10 programming languages
and is used at 5000 universities around the world for teaching, research, and projectbased learning. It is a high-level programming language and initially developed to
easily solve numerical problems. It allows matrix manipulations, plotting of
functions and data, implementation of algorithms, creation of user interfaces,
interfacing with programs written in other languages, including C, C++, Java, and
FORTRAN, analyse data, develop algorithms, and create models and applications. It
also has numerous built-in commands and math functions that help you in
mathematical calculations, generating plots, and performing numerical methods.
MATLAB is widely used in various applications such as signal processing and
communications,
control
systems,
image
and
video
processing,
and
test
environment.
Introduction to MATLAB
3.1
Page | 3
http://www.mathworks.com/academia/student_version/?s_tid=main_sv_ML_tb
MATLAB offers a good price for student version which is approximately $89.
3.1.2 Starting MATLAB
In Windows, you start MATLAB by clicking the Start menu, under a name like
MATLAB R2014b (the MATLAB version might be different) or alternatively you can
double click on the MATLAB R2014b desktop icon located on your desktop as shown
in Figure 1.
Page | 4
Workspace
Command History
Current Folder
Editor
Command Window
3.2
If you want to get help use the help, and lookfor commands. help should be used
to find out information about a known command, e.g. help stats. Please read the
document in the help section if you have any doubt. lookfor can be used to find a
command to perform a task e.g. lookfor average.
If you do not get the command prompt for some reason, you may have typed a syntax
error, or have put MATLAB into an infinite loop. You can stop the MATLAB software
by pressing Ctrl-C and you'll get the prompt back.
You can quit the MATLAB software by entering quit in the command window.
Alternatively you can click (X) close button at the top right of the MATLAB desktop.
Page | 5
Simple expression
1. >> 5+5
ans =
10
2. >> 11-8
ans =
3
3. >> 6*8
ans =
48
4. >> 14/7
ans =
2
5. >> 5^3
ans =
125
PREPARED BY FIRDAUS MUTTALIB
Page | 6
6. >> sin(2*pi)+exp(3/2)
ans =
0.2231
Exercise 1:
1. Compute the following:
3
i.
ii.
2
3
2
12 3 4 2
35
iii. sin( )
5
iv. cos( )
v. log 10 (10 5 )
vi. log( 3)
2. What is the difference between log 10 and log command in the MATLAB?
Variables
Page | 7
For example:
>> z = x + 3
z =
11
Once a variable is entered into the system, you can refer to it later. Variables must
have values before they are used. When an expression returns a result that is not
assigned to any variable, the system assigns it to a variable named ans, which can
be used later.
You can have multiple assignments on the variable in one line for example:
>> x=6; y=7; z=x*y
z =
42
You may have noticed that ; is used in the above example. The function of ; is to
supress the output result to the screen, hence the results for x=6 and y=7 are not
appeared on the Command Window.
You can check the variables that you have assigned in the MATLAB by using whos
command.
>> whos
Name
Size Bytes
Class
1x1
8 double
1x1
8 double
1x1
8 double
Attributes
You can save all the variables in the Workspace by using the save command. All the
variables will be save in the matlab.mat under the Current Folder. You can easily
load the saved variable by using typing the load command in the Command
Window.
PREPARED BY FIRDAUS MUTTALIB
Page | 8
You can clear all the variables stored in the Workspace by entering the clear
command.
You can clear all the commands in the Command Window by typing clc in the
Command Window.
You can assign a value to the long variables name such as:
>> mass=1000; acceleration=2; force=mass*acceleration
force =
2000
Another example:
>> initial_velocity = 0;
>> acceleration = 9.8;
>> time = 20;
>> final_velocity = initial_velocity + acceleration * time
final_velocity =
196.
Task 2:
Convert the following using appropriate variables.
1. kilometre to metre
2. second to nanosecond
Arrays
Arrays are lists of numbers or expressions arranged in horizontal rows and vertical
columns. A single row, or single column array is called a vector. An array with m
rows and n columns is called a matrix of size mn.
6.1
Vectors
Page | 9
Row vectors are created by enclosing the set of elements in square brackets, using
space or comma to delimit the elements.
>> x = [1 2 3 4]
x =
1 2 3 4
>> y = [5; 6; 7; 8;]
y =
5
6
7
8
You can transpose a created vector by using a single quotation mark '.
>> x'
ans =
1
2
3
4
>> y'
ans =
5 6 7 8
You can create vector that has a certain range especially those that contain many
values.
Page | 10
For example:
You want to create a vector that contain 1 to 100 in step of 1.
>> a = [1:10]
a = 1 2 3 4 5 6 7 8 10
You want to create a vector that contain odd number from 1 to 9.
>> b = [1:2:9]
b = 1 3 5 7 9
You can add, subtract, divide, and multiply among the vectors.
>> a = [1:5]; b=[6:10]; a+b
ans = 7 9 11 13 15
>> a-b
ans = -5 -5 -5 -5 -5
>> a*10
ans = 10 20 30 40 50
>> a.*b (element by element multiplication)
ans = 6 14 24 36 50
>>a./b (element by element division)
ans = 0.1667 0.2857 0.3750 0.4444 0.5000
Task 3:
1. Create the vector consisting of the whole even numbers between 15 and 27.
2. Create the vector consisting of the whole odd numbers between 15 and 27.
3. Create the vector of the previous question in decreasing order.
Page | 11
4. Let x = [1 3 5 7] and b= [3 5 7 9], please find the answer for >> x.*y and
>>x*y'. What is the difference between these two commands?
6.2
Matrices
1 2 3 4 5
6 7 8 9 10
A
11 12 13 14 15
16 17 18 19 20
Page | 12
11
16
12
17
13
18
14
19
10
15
20
You may have noticed that the transposed matrices has 5 rows and 4 columns.
6.2.3 Inverse of a matrix
The inverse of a matrix A is denoted by B1 such that the following relationship holds
BB-1=B-1B=1.
The inverse of a matrix does not always exist. If the determinant of the matrix is zero,
then the inverse does not exist and the matrix is singular.
Inverse of a matrix in MATLAB is calculated using the inv function. Inverse of a
matrix B is given by inv(B).
>>B= [ 1 2 3; 2 3 4; 1 2 5]
>>inv(B)
b =
1
2
1
2
3
2
3
4
5
ans =
-3.50000
3.00000
-0.50000
2.00000
-1.00000
0.00000
0.50000
-1.00000
0.50000
Page | 13
5 3 2
A 3 8 4
2
4 9
x is the vector of unknowns,
x
X y
z
and B is a vector containing the constants.
10
B 20
9
To solve these linear equations, the rules of matrix algebra apply i.e. the result of
multiplying a N N matrix by a N 1 vector, is a N 1 vector.
>> A = [5 3 2; 3 8 4; 2 4 9];
>> B = [10; 20; 9;];
>> X = A\B
X = 3.4442
PREPARED BY FIRDAUS MUTTALIB
Page | 14
3.1982
1.1868
6.3
You can access the elements in the vector or matrix by identifying the rows and
columns of vector/matrix by using matrix_name(row, column).
For example:
M = 17
24
15
23
14
16
13
20
22
10
12
19
21
11
18
25
Page | 15
ans = 8
14
20
21
2
You can create new matrices by extract sub-matrix of M matrices by:
>> M ([2,3,4],[2,3,4])
or
>> M ([2:4],[2:4])
ans =
5
14
13
20
12
19
21
You can replace or append elements inside the M matrices of 1st row and entire
columns to [1 2 3 4 5] by:
>>M(1,:) = [1 2 3 4 5]
M =
1
23
14
16
13
20
22
10
12
19
21
11
18
25
Page | 16
10
11
12
13
14
15
You can link two separate matrices to create a larger matrices. Use comma (,) to add
the two matrices horizontally and use semicolon (;) to add two matrices vertically.
>> a = [10 12 23 ; 14 8 6; 27 8 9]
>> b = [12 31 45 ; 8 0 -9; 45 2 11]
>> c = [a, b]
>> d = [a; b]
a =
10
14
27
12
8
8
23
6
9
31
0
2
45
-9
11
12
8
8
23
6
9
12
8
8
31
0
2
23
6
9
45
-9
11
b =
12
8
45
c =
10
14
27
12
8
45
31
0
2
45
-9
11
d =
10
14
27
12
8
45
Page | 17
Task 4:
1. Let N =magic(3), add 5 to all elements in column 1 and 3.
2. Create a new P matrices by extracting all elements in row 2,3, and 5 and odd
columns.
Exercise 2:
1. Create a vector consisting of the squares of the numbers from 1 to 100.
2. Compute the sum of the powers of 0.5 to the numbers from 1 to 10.
3. Let O=magic (5), create a new P matrices by extracting all elements in row 2,3,
and 5 with odd columns.
4. Compute the summation of all elements in 3rd row of P matrices.
5. Replace all elements in 2nd column P matrices by these elements [3 4 5]
2 1 6
3 1 5
1
0
1
Hint = x=A-1B
2 1 6
5 9 4
3 1 5
1 7 6
i.
ii.
iii.
iv.
v.
Page | 18