BME6101+ +lecture+04+ (Part+I) + +slides+ +Matlab+Basics
BME6101+ +lecture+04+ (Part+I) + +slides+ +Matlab+Basics
Devices
Lecture 04 (Part I)
Introduction to Matlab
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.
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.
Command
window
Command
history
(you can cut and paste
previous commands
into command window
to execute)
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.
A=
A=
6
>> a;A;
>> If semicolon are used, the values will NOT be echo back.
1 2 3 4 5
Note: The above assignment overrides the previous assignment of a=4
>> 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
A=
Output by MATLAB
1 2 3 A is a 3 x 3 matrix
4 5 6
7 8 9
10 9 8 7 6 5
110
1 2 2 4 3 6 4 8 5 1 0
• 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 12 2 5 3 8 36
13 2 6 3 9 42
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
MATLAB – Graphics
>> plot(t,v)
>> plot(t,v)
>> title('v versus t')
>> xlabel('value t')
>> ylabel('value v')
>> grid
MATLAB – Graphics
>> plot(t,v)
>> title('v versus t')
>> xlabel('value t')
>> ylabel('value v')
>> grid
>> axis([0 22 0 450])
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')
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
>> 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
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