Introduction To Matlab
Introduction To Matlab
Sumitha Balasuriya
http//www.dcs.gla.ac.uk/~sumitha/
Matlab
• Stands for MATrix LABoratory
• Interpreted language
• Scientific programming environment
• Very good tool for the manipulation of matrices
• Great visualisation capabilities
• Loads of built-in functions
• Easy to learn and simple to use
Workspace /
Current Directory
Command
Window
Command
History
Matlab comment
prompt suppress operator
assign
command
operator
output Try the same line without the
semicolon and comments
Introduction to Matlab Sumitha Balasuriya 4
Variables (continued …)
• View variable contents by simply typing the
variable name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
>>A(1,2)
ans= indices of matrix element(s)
2
• Remember Matrix(row,column)
• Naming convention Matrix variables start
with a capital letter while vectors or scalar
variables start with a simple letter
Introduction to Matlab Sumitha Balasuriya 10
The : operator
• VERY important operator in Matlab
• Means ‘to’
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
Try the following
ans = >> x=0:pi/12:2*pi;
>> y=sin(x)
1 3 5 7 9
Introduction to Matlab Sumitha Balasuriya 11
The : operator and matrices
A=
>>A(3,2:3) 3 2 1
ans = 5 1 0
2 1 7
1 7
>>A(:,2)
ans =
2
1
What’ll happen if you type A(:,:) ?
1
Create matrices A and B and try out the the matrix operators in this slide
Introduction to Matlab Sumitha Balasuriya 13
Scripts
• Matlab editor
• Use scripts to execute a series of Matlab
commands
Matlab
Desktop
Press to create
new m-file in the
matlab editor
function keyword
Without ; to
print output
i=
4
i=
16
i= Method is linear
256 >>
Figure 1
Figure 2