0% found this document useful (0 votes)
5 views36 pages

BME6101+ +lecture+04+ (Part+I) + +slides+ +Matlab+Basics

Uploaded by

世杰 刘
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views36 pages

BME6101+ +lecture+04+ (Part+I) + +slides+ +Matlab+Basics

Uploaded by

世杰 刘
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

BME6101 Manufacturing of Biomedical

Devices

Lecture 04 (Part I)
Introduction to Matlab

Dr. Raymond H. W. Lam

(prepared based on materials from


BME2036 Engineering Computing)
1
Department of Biomedical Engineering City University of Hong Kong
2

MATLAB
• The name MATLAB stands for matrix laboratory.
– MATLAB was originally written to provide easy access to matrix
software developed by the LINPACK and EISPACK projects.
– Today, MATLAB uses software developed by the LAPACK and
ARPACK projects, which together represent the state-of-the-art
in software for matrix computation.
• MATLAB is an interactive system whose basic data
element is an array that does not require dimensioning.
• This allows users to solve many technical computing
problems, especially those with matrix and vector
formulations
• For numerical computation, it takes much less time to
write a program in MATLAB than in a scalar non-
interactive language such as C or Fortran.

Department of Biomedical Engineering City University of Hong Kong


3

MATLAB
• MATLAB features a family of application-specific
solutions called toolboxes.
• Toolboxes allow you to learn and apply
specialized technology.
• Toolboxes are comprehensive collections of
MATLAB functions (M-files) that extend the
MATLAB environment to solve particular classes
of problems.
• Areas in which toolboxes are available include
signal processing, control systems, neural
networks, fuzzy logic, wavelets, simulation, and
many others.

Department of Biomedical Engineering City University of Hong Kong


4
„>>‟ is MATLAB‟s
MATLAB command
prompt for users
to enter
commands
Launch
pad

Command
window

Command
history
(you can cut and paste
previous commands
into command window
to execute)

Department of Biomedical Engineering City University of Hong Kong


5

MATLAB
• Simple calculation – Try typing the BLUE
colour text at the command window
>> 55-16 The output generated by MATLAB.
A simple calculator!
ans =
39
MATLAB has automatically
>> ans+10 assigned to a variable, ans. Thus
you can now use ans in a
ans = subsequent calculation
49 The output generated by MATLAB.

Department of Biomedical Engineering City University of Hong Kong


6

MATLAB – Assignment of Scalar Values

• Assignment of value to a scalar variable – Try


typing the blue colour text

>> a = 4 This assign 4 to a scalar variable a

a= MATLAB automatically echo back the


value of a to confirm the operation
4
• Echo printing is a characteristic of MATLAB. It
can be suppressed by terminating the command
line with a semicolon, ; , character.

Department of Biomedical Engineering City University of Hong Kong


7

MATLAB – Assignment of Scalar Values

• Try typing the blue colour text:


>> A = 6; Semicolon stops the automatic echo function

>> It just returns the command prompt!

Note: All variable names are case-sensitive, i.e. a and A are


two different variables

• Try typing the blue colour text:


>> A Just type the variable name, MATLAB will return its value

A=

Department of Biomedical Engineering City University of Hong Kong


8

MATLAB – Assignment of Scalar Values

• Try typing the blue colour text:


Several commands on the same line can be
>> a, A
separated by commas or semicolon.
a=
If commas are used, the values will be echo back.
4

A=
6
>> a;A;
>> If semicolon are used, the values will NOT be echo back.

Department of Biomedical Engineering City University of Hong Kong


9

MATLAB – Arrays, Vectors and Matrices

• An array is a collection of values that are


represented by a single variable name.
• One dimensional arrays are called vectors.
• Two dimensional arrays are called
matrices.
• The scalar variables in the previous slides
are actually treated as a matrix with one
row and one column.

Department of Biomedical Engineering City University of Hong Kong


10

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• A row vector can be assigned by enclosing
the values of the vector with brackets
>> a = [1 2 3 4 5] Creating a row vector!
a=

1 2 3 4 5
Note: The above assignment overrides the previous assignment of a=4

Department of Biomedical Engineering City University of Hong Kong


11

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• Row vectors are rarely used to solve
mathematical problem
• Column vectors are usually used.
• To create a column vector, type the blue
color text: b=

>> b=[2;4;6;8;10] 2
MATLAB 4
generates
the output 6
8
10
Department of Biomedical Engineering City University of Hong Kong
12

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• Or to create a column vector by
transposing a row vector:
>> b =[1 3 5 7 9]' b=
MATLAB
generates 1
the output 3
5
7
9

Department of Biomedical Engineering City University of Hong Kong


13

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• To create a matrix, use semicolon to
separate each row. Try typing the blue
text:
>> A = [ 1 2 3; 4 5 6; 7 8 9]

A=
Output by MATLAB
1 2 3 A is a 3 x 3 matrix
4 5 6
7 8 9

Department of Biomedical Engineering City University of Hong Kong


14

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• The Enter key can also used to separate
each row. A=
>> A = [2 3 4 2 3 4
MATLAB
567 5 6 7
generates
8 9 10] the output 8 9 10

• Enter whos to get information about all


current variables Name Size Bytes Class

A 3x3 72 double array


>> whos a 1x5 40 double array
MATLAB ans 1x1 8 double array
generates b 5x1 40 double array
the output
Grand total is 20 elements using 160 bytes

Department of Biomedical Engineering City University of Hong Kong


15

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• To access an individual element of an array,
use the subscript notation i.e.
>> b(4) This is to access the fourth element of vector b
ans = The answer is 7
7
• To access an element in a matrix:
>> A(2,3) To access the element in 2nd row and 3rd
column
ans =
The answer is 7
7
Department of Biomedical Engineering City University of Hong Kong
16

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• The colon operator, : , is a powerful tool for
creating and manipulating arrays. Try typing
in the blue colour text:
MATLAB generate the output
>> t=1:5 t=
If a colon is used to
separate two numbers,
MATLAB generates the 1 2 3 4 5
numbers between them
using an increment of
one

Department of Biomedical Engineering City University of Hong Kong


17

MATLAB – Arrays, Vectors and Matrices


(cont‟)
If colons are used to separate three numbers,
>> t=1:0.5:3 MATLAB generates the numbers between first
and third numbers using an increment equal to
MATLAB output the second number
t=
1.0000 1.5000 2.0000 2.5000 3.0000

>> t=10:-1:5 The increment can be a negative number


MATLAB output
t=

10 9 8 7 6 5

Department of Biomedical Engineering City University of Hong Kong


18

MATLAB – Arrays, Vectors and Matrices


(cont‟)
• The colon operator can also be used as a
wildcard to select the individual rows and
columns of a matrix:
>> A(2,:) Select the second row of the matrix A
MATLAB output
ans =
5 6 7
• The colon operator can also selectively extract
a series of elements from within an array:
>> t(2:4) ans =
MATLAB output
9 8 7
Department of Biomedical Engineering City University of Hong Kong
19

MATLAB – Mathematical Operations

• The common operators, in order of priority,


are:
High
priority
^ Exponentiation
- Negation
*/ Multiplication and division
\ Left division – it applies to matrix algebra
Low
priority
+- Addition and subtraction

Department of Biomedical Engineering City University of Hong Kong


20

MATLAB – Mathematical Operations


• Examples:
>> 4*4 Simple calculation
ans =
16

>> y = 2 * 2 The result of the calculation can be assigned


to a variable
y=
4
>> y ^ 2 Operators can be applied to variables
ans =
16

Department of Biomedical Engineering City University of Hong Kong


21

MATLAB – Mathematical Operations (cont‟)

• Operation on matrices. Example:


>> a = [1 2 3 4 5]; Semicolon „;‟ stop MATLAB to echo the
>> b = [2 4 6 8 10]'; result
Use “ ‟ ” notation to create a column vector

>> a * b Mathematical operators can apply to


matrices
ans =

110

1  2  2  4  3  6  4  8  5 1 0

Department of Biomedical Engineering City University of Hong Kong


22

MATLAB – Mathematical Operations (cont‟)

• More examples on matrices


>> a = [1 2 3 4 5];
>> b = [2 4 6 8 10]';
2
>> b*a 4
 
ans =  6   1 2 3 4 5
8
2 4 6 8 10  
4 8 12 16 20 10
6 12 18 24 30
8 16 24 32 40
10 20 30 40 50

Department of Biomedical Engineering City University of Hong Kong


23

MATLAB – Mathematical Operations (cont‟)

• More examples
1 2 3
>> a=[1 2 3];
1 2 3  4 5 6
>> A=[1 2 3; 4 5 6; 7 8 9];
7 8 9 
>> a * A
ans = 1 1  2  4  3  7  3 0
30 36 42 12  2 5  3 8  36
13  2 6  3 9  42

Department of Biomedical Engineering City University of Hong Kong


24

MATLAB – Mathematical Operations (cont‟)

>> b=[4 5 6]';


>> A=[1 2 3; 4 5 6; 7 8 9]; 1 2 3  4 
>> A * b  4 5 6  5 
   
ans = 7 8 9  6 
32
77
14  2 5  3 6  32
122 4 4  5 5  6 6  77

Department of Biomedical Engineering City University of Hong Kong


25

MATLAB – Mathematical Operations (cont‟)

>> A=[1 2 3; 4 5 6; 7 8 9];


1 2 3 1 2 3
4 5 6  4 5 6
>> A^2    
ans = 7 8 9  7 8 9 
30 36 42
66 81 96
102 126 150
>> A * A
ans =
30 36 42
66 81 96
102 126 150

Department of Biomedical Engineering City University of Hong Kong


26

MATLAB – Mathematical Operations (cont‟)

• To square each element of A, .^ (array


operations and element-by-element
operations) can be used:
>> A = [1 2 3; 4 5 6; 7 8 9]; 12 22 32 
>> A .^ 2  2 
ans = 4 52 62 
1 4 9 7 2 82 9 2 
16 25 36
49 64 81

Department of Biomedical Engineering City University of Hong Kong


27

MATLAB – Graphics
>> t=0:2:20
t=
0 2 4 6 8 10 12 14 16 18 20
>> length(t) „length‟ function tell the length of the array t
ans =
11
>> for i=1:11 Use a loop to calculate the square of each element
v(i)=t(i)^2; „end‟ to end the „for‟ loop
end
>> v
v=
0 4 16 36 64 100 144 196 256 324 400

Department of Biomedical Engineering City University of Hong Kong


28

MATLAB – Graphics

>> plot(t,v)

>> plot(t,v)
>> title('v versus t')
>> xlabel('value t')
>> ylabel('value v')
>> grid

Department of Biomedical Engineering City University of Hong Kong


29

MATLAB – Graphics
>> plot(t,v)
>> title('v versus t')
>> xlabel('value t')
>> ylabel('value v')
>> grid
>> axis([0 22 0 450])

xmin xmax ymin ymax

Department of Biomedical Engineering City University of Hong Kong


30

MATLAB – Graphics

>> t=0:2:20;
>> for i=1:length(t)
vt(i)=t(i)^1.5;
v(i)=t(i)^2;
end
>> plot(t,v,'r',t,vt,'b')

Red color for v Blue color for vt

Department of Biomedical Engineering City University of Hong Kong


31

MATLAB – Formatted Text Output


%5d %12.5f
>> clear all % Clear all existing all variables
>> t=0:2:10;
>> for i=1:length(t) 0 0.00000
v(i)=t(i)^2;
vt(i)=t(i)^1.5;
end Combine vectors t, v and vt into matrix z
>> z=[t;v;vt];
%5d : display Integer t with 5 spaces
>> fprintf('%5d %12.5f %12.5f\n', z)
0 0.00000 0.00000 %12.5f : display floating point v with
2 4.00000 2.82843 12 space
4 16.00000 8.00000 %12.5f : display floating point vt
6 36.00000 14.69694 with 12 space
8 64.00000 22.62742 \n: new line
10 100.00000 31.62278

Department of Biomedical Engineering City University of Hong Kong


32

MATLAB – M-files
• M-file contains a series of statements that can be run all
at once.
• It is similar to subroutine or function of other computer
languages
• 2 type of M-files: script file and function file
• A script file is merely a series of MATLAB commands
that are saved on a file. It can be executed by typing the
filename in the command window
• A function file can accept input arguments and return
outputs. It is similar to user-defined functions in
programming languages such as C or BASIC

Department of Biomedical Engineering City University of Hong Kong


33

Create a Script M-file

>> findv
t v vt
0 0.00000 0.00000
2 4.00000 2.82843 findv.m
4 16.00000 8.00000
6 36.00000 14.69694
8 64.00000 22.62742
10 100.00000 31.62278

Department of Biomedical Engineering City University of Hong Kong


34

Create a Function M-file


• The syntax for a function M-file:

function outvarName = functioinName(arglist)


% help comments
Statements
outvarName = value

Department of Biomedical Engineering City University of Hong Kong


35

Create a Function M-file (cont‟)


Filename is same as
the name of the
function

>> clear all


function myresult = mysquare(n) >> t=0:2:10;
for i=1:length(n) >> v=mysquare(t)
x(i)=n(i)^2; v=
end
0 4 16 36 64 100
myresult=x;

Department of Biomedical Engineering City University of Hong Kong


36

Learning Outcomes
After this lecture, the student would be able to
understand the following:
– The details of the first coursework
– The basic concept of MATLAB
– How to use MATLAB to carry out some simple
arithmetic calculations and matrix calculations
– How to use graphics and formatted text output
in MATLAB
– How to create script and function M-files

Department of Biomedical Engineering City University of Hong Kong

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy