You are on page 1of 12

Matlab Tutorial for CBE 109

• Introduction to Matlab
• Matlab syntax
– Matrices and arrays
– Flow and loop control
– Input and output, string operation
– Plotting and graphics
• Additional learning resources

10/2/2009 CBE109 09Fall 1


Introduction to Matlab
• Matlab stands for Matrix laboratory
• A user-friendly language: easy-to-use,
come with good documents, simple syntax
and powerful build-in functions and
toolboxes.
• Integrated environment for computing,
programming and visualization results

10/2/2009 CBE109 09Fall 2


Start Using Matlab
• Command Window: where you type your command.
Used to run simple commands. You can use it like a
calculator
• Editor: where you create scripts and functions, save
them as .m files so that you do not need to retype the
commands
• Current Directory: where you can save the files you
want to run
• Workspace: shows you all the variables in memory
• Path (File-> Set path): if Matlab cannot find a command
under current directory, it will search other the folders
listed in the path.

10/2/2009 CBE109 09Fall 3


Data Structure in Matlab
• Most numerical data is saved as matrix in
Matlab. Scalar is considered as a 1-by-1 matrix.
• Row vector is a 1-by-n matrix and column vector
is n-by-1 matrix

10/2/2009 CBE109 09Fall 4


Data Structure in Matlab
>> a = [1 2 3 4] >> b = 1:1:5 >> A = [1 2;3 4]
a= b= A=
1 2 3 4 1 2 3 4 5 1 2
3 4
>> a = [1 2 3 4 5] >> c = [1,2,3]
a= c= >> A(2,1)
1 2 3 4 5 1 2 3 ans =
3
>> a = [1 2 3] >> c = [1,2,3];
a= >> x = pi
1 2 3 >> a = [1;2;3] x=
a= 3.1416
>> a(1) 1 >> x = inf
ans = 2 x=
1 3 Inf

10/2/2009 CBE109 09Fall 5


Expressions
>> A = [1 2;3 4]; >> x = [3;4]
+ Addition >> B = [5 6;7 8]; x=
>> C = A + B 3
- Subtraction C= 4
6 8 >> b = A*x;
* Multiplication 10 12 >> x = A\b
x=
/ Division >> A*B 3.0000
ans = 4.0000
\ Left division 19 22
43 50
^ Power
For more, see Help/Functions – By
‘ Matrix Category/ Mathematics/ Arrays and
transpose Matrices

10/2/2009 CBE109 09Fall 6


Use functions
>> A = [1 2;3 4];
>> sA = sin(A)
• Matlab first search the current
sA = directory for file sin.m
0.8415 0.9093
0.1411 -0.7568 • If it is not found, then it search
the directories listed in the path
>> size(A)
ans = • See more at Help/Functions –
2 2
By Category/ Mathematics/
• Linear Algebra
• Elementary Math

10/2/2009 CBE109 09Fall 7


Print result and visualization
>> fprintf(1,'sin(pi) = %f\n',sin(pi));
sin(pi) = 0.000000

>> omega = 0:0.1*pi:2*pi;


>> plot(omega,sin(omega))
>> xlabel('\omega');
>> ylabel('sin(\omega)');

10/2/2009 CBE109 09Fall 8


Use script
• You can save a set of commands in .m file
so that you do not need to retype it in the
future.
% Calculate A+B and >> Example1_Script
A*B ans =
A = [1 2;3 4]; 6 8
10 12
B = [5 6;7 8];
A+B ans =
19 22
A*B 43 50

10/2/2009 CBE109 09Fall 9


Flow and loop control
% Find the maximum element in >> Example2_Flow_and_loop_control
matrix A The maximum element in A is
A = [1 2;3 4]; 4.000000
mA = -inf;
for i = 1:2
for j = 1:2
if A(i,j) > mA
mA = A(i,j);
end
end
end
fprintf(1,'The maximum element
in A is %f\n',mA);

10/2/2009 CBE109 09Fall 10


Functions
function m = CBE109_max(B) % Find the maximum value in matrix A and B
and compare
% m = CBE109_max(B)
% Algorithm:
% Find the maximum element in
matrix B % (1) Find the maximum element in A
% Author: CBE109 TA % (2) Find the maximum element in B
% Date: 10-02-2009 % (3) Compare mA and mB
m = -inf; A = [1 2;3 4]; B = [5 6;7 8];
[row,col] = size(B); mA = CBE109_max(A); mB = CBE109_max(B);
for i = 1:row if(mA > mB)
for j = 1:col fprintf('max(A) > max(B)\n');
if B(i,j) > m elseif(mA < mB)
m = B(i,j);
fprintf('max(B) > max(A)\n');
end
else
end
fprintf('max(B) = max(A)\n');
end
end

10/2/2009 CBE109 09Fall 11


Learning resources of Matlab
• Documents come with Matlab
• Tutorials are available at
http://www.mathworks.com/academia/stud
ent_center/tutorials/launchpad.html

10/2/2009 CBE109 09Fall 12

You might also like