You are on page 1of 18

UNIVERSITY OF MALAYSIA PERLIS

SCHOOL OF BIOPROCESS ENGINEERING


(BIOSYSTEMS ENGINEERING)
SEMESTER 2 2015/2016
ERT 355 / 2
BIOSYSTEMS MODELLING AND DESIGN

LABORATORY MODULE - MATLAB

WEEK 1
INTRODUCTION TO MATLAB
BASIC CALCULATIONS
VARIABLES
ARRAYS

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

Contents

Objective .......................................................................................... 3

Introduction .................................................................................... 3

Introduction to MATLAB ................................................................. 3


3.1

MATLAB Start Up and Layout ..................................................................... 3


3.1.1 Installation of MATLAB software ...................................................... 3
3.1.2 Starting MATLAB .............................................................................. 4
3.1.2 MATLAB Desktop .............................................................................. 4

3.2 Getting Help, Stop and Quitting MATLAB .................................................. 5


4

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

PREPARED BY FIRDAUS MUTTALIB

Page | 2

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

Objective

The objectives of this laboratory module are:


1. To acquire knowledge of starting up and understand layout of the
MATLAB software
2. To perform mathematical calculation using simple expression and
variables.
3. To visualize the calculated data by plotting the graphs.

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

MATLAB Start Up and Layout

3.1.1 Installation of MATLAB software


If you want to run a MATLAB software on your own computer especially the student
version, the software can be purchased and downloaded to your own computer from
the below link:
PREPARED BY FIRDAUS MUTTALIB

Page | 3

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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.

Figure 3.1. MATLAB R2014b desktop icon


3.1.2 MATLAB Desktop
After you open the MATLAB software, you will see a default MATLAB desktop as
shown in Figure 2. The MATLAB desktop or graphical user interface (GUI) is divided
into several separate windows such as Command Window, Current Folder, Editor,
Workspace, and Command History.
Command Window is a place where you write MATLAB commands. You will see a
command prompt (>>) inside the Command Window and blinking cursor (vertical
dashed line) if the window is active. The command prompt (>>) indicates that it is
ready for the input from the user. To command MATLAB, type your request and hit
Enter. MATLAB then evaluates your commands and displays the output (if any).
Current Folder is a place where MATLAB allows you to access and save the project
folders and files. MATLAB will first look at this folder to find a files that hold
programs that it needs.

PREPARED BY FIRDAUS MUTTALIB

Page | 4

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

Workspace

Command History

Current Folder

Editor

LAB MODULE MATLAB (WEEK 1)

Command Window

Figure 3.2. MATLAB desktop


Editor is a place where you can write a MATLAB script.
Workspace is a place where all the variables created and/or imported from files or
user command.
Command History is place where commands that are entered at the command
line is recorded.

3.2

Getting Help, Stop and Quitting MATLAB

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.

PREPARED BY FIRDAUS MUTTALIB

Page | 5

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

Simple expression

MATLAB can be used to calculate basic mathematical expressions. The basic


arithmetic operations are + (Addition), - (Subtraction), * (Multiplication), /
(Divison), and ^ (exponentiation).
Task 1:
Type the following command in the Command Window and check your answer.

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

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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

Variables is the important concept in any programming languages included


MATLAB. In MATLAB you use the equal sign (=) to assign a value to variable. For
example:
>> x = 8
x =
8
x is a variable that has a value of 8. You can use x in your command to represent 8 at
later stage.
PREPARED BY FIRDAUS MUTTALIB

Page | 7

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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

A vector is a one-dimensional array of numbers. MATLAB allows creating two types


of vectors row vectors and column vectors.
PREPARED BY FIRDAUS MUTTALIB

Page | 9

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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.

PREPARED BY FIRDAUS MUTTALIB

Page | 10

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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.

PREPARED BY FIRDAUS MUTTALIB

Page | 11

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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

Row and Column vectors are special cases of matrices. An m x n matrix is a


rectangular array of numbers having m rows and n columns. It is usual in a
mathematical setting to include the matrix in either round or square brackets we
shall use square ones. For example, when m=4, n=5 we have a 4 5 matrices such as

1 2 3 4 5
6 7 8 9 10

A
11 12 13 14 15

16 17 18 19 20

To create an A matrices you can


>> A =[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20;]
or
>> A = [1:5; 6:10; 11:15; 16:20;]
You use a semicolon (;) to separate rows.
6.2.1 Size of the matrix
You can check the size of matrices by:
>>size(A)
ans = 4 5
It means that A matrices contain 4 rows and 5 columns.

PREPARED BY FIRDAUS MUTTALIB

Page | 12

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

6.2.2 Transpose of a matrix


You can transpose the A matrices by entering the A'. Transposing a vector changes
it from a row to a column vector and vice versa.
>>A
ans =
1

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

PREPARED BY FIRDAUS MUTTALIB

0.50000
-1.00000
0.50000
Page | 13

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

6.2.4 Solving system of linear equations


Solving systems of linear equations is one of the most common computations in
science and engineering, and is easily handled by MATLAB. Consider the following
set of linear equations. 5x = 3y 2z + 10 8y + 4z = 3x + 20 2x + 4y 9z = 9. This set
of equations can be re-arranged so that all the unknown quantities are on the lefthand side and the known quantities are on the right-hand side.
5x 3y + 2z = 10
3x + 8y + 4z = 20
2x + 4y 9z = 9
This is now of the form AX = B, where A is a matrix of the coefficients of the
unknowns,

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

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

3.1982
1.1868

6.3

Referencing array elements

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

You can access an element in M matrices at row 2 and column 3 by:


>> M = magic(5);
>> M(2,3)
ans =
7
You can list entire 3rd row of the M matrices by:
>> M(3,1:end)
or
>> M(1,:)
ans = 4 6 13 20 22
1:end represent the column 1 to the end or you can use colon operator : to denote
all of the columns (all the columns in the third row are selected). The colon operator
can be used to denote all rows.
You can list entire 4th column of the M matrices by:
>> M(:,4)
PREPARED BY FIRDAUS MUTTALIB

Page | 15

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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

You can delete a row or column in a matrix by:


For example you want to delete the 4th row of H matrices
PREPARED BY FIRDAUS MUTTALIB

Page | 16

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

>> H=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20;];


>> H(4,:)=[]
H =
1

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

PREPARED BY FIRDAUS MUTTALIB

Page | 17

ERT 355/2: BIOSYSTEMS MODELLING AND SIMULATIONS

LAB MODULE MATLAB (WEEK 1)

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

6. Solve for x in the equation Ax = B, where A= 3 5 7 and B=

3 1 5

1
0

1

Hint = x=A-1B

2 1 6
5 9 4

7. Let C= 3 5 7 and D= 2 8 3 , determine the following:

3 1 5
1 7 6
i.
ii.
iii.
iv.
v.

Matrix addition between C and D.


Element by element multiplication of C and D.
Create new matrix E by combining the matrix C and D vertically.
Multiply matrix E and C.
Divide the results from question 7(ii) with matrix D.

8. Solve the following systems of linear equations. Remember to verify your


solutions.
x1 2x2 x3 + 3x4 = 10
x1 + 3x2 + x4 = 8
x1 4x3 2x4 = 3
x2 + 3x3 + x4 = 7

PREPARED BY FIRDAUS MUTTALIB

Page | 18

You might also like