You are on page 1of 22

1.

Introduction to Matlab
 Matlab: Matrix Laboratory
 Numerical Computations
with matrices
• Every number can be
represented as matrix

 Why Matlab?
• User Friendly (GUI)
• Easy to work with
• Powerful tools for complex mathematics.

 C.B. Pham 1
Matlab desktop

When you start MATLAB, MATLAB desktop appears,


containing tools (graphical user interfaces) for managing
files, variables, and applications associated with MATLAB.

Command window

 C.B. Pham 2
“Help” in Matlab

>> help <function_name>


Shows help document for a give function
Eg. help sqrt

>> lookfor <keyword>


Searches all the help documents for a given keyword
Eg. lookfor sqrt

>> demo

 C.B. Pham 3
2. Matrix operations

Addition Substraction
>> c = a + b >> c = a - b

Multiplication
>> c = a*b % matrix multiplication
>> c = a.*b % element wise multiplication

Division (left, right)


>> c = a/b % a * b-1
>> c = a./b % element wise division
>> c = a\b % a-1 * b
>> c = a.\b % element wise division

 C.B. Pham 4
Variable: constant, vector, matrix

Real Complex
>> x = 5 >> x = 5 + 10*i
x= x=
5 5.0000 + 10.0000i
Row vector Column vector
>> x = [1, 2, 3] % [1 2 3] >> x = [1; 2; 3]
x= x=
1 2 3 1
2
Matrix 3
>> x = [1 2 3; 4 5 6]
x=
1 2 3
4 5 6 Note: Variable Names are case sensitive
 C.B. Pham 5
Vectors

>> [0 : 0.2 : 1] % 0 to 1 in increments of 0.2


ans =
0.0 0.2 0.4 0.6 0.8 1.0

>> linspace(0, 1, 6) % 6 points from 0 to 1 on a linear scl


ans =
0.0 0.2 0.4 0.6 0.8 1.0

>> logspace(0, 1, 6) % 6 points from 100 to 101 on a log scl


ans =
1.0000 1.5849 2.5119 3.9811 6.3096 10.0000

 C.B. Pham 6
Matrix

>> ones(2, 3) % generates an all one 2 x 3 matrix


ans =
1 1 1
1 1 1

>> zeros(2, 3) % generates an all zero 2 x 3 matrix


ans =
0 0 0
0 0 0

 C.B. Pham 7
Matrix

>> eye(2) % generates the 2 x 2 identity matrix


ans =
1 0
0 1

>> rand(2, 3) % generates a random 2 x 3 matrix


ans =
0.9501 0.6068 0.8913
0.2311 0.4860 0.7621

 C.B. Pham 8
Accessing matrix elements

>> M = [ 1 2 3 4; 5 6 7 8; 9 10 11 12]
M=
1 2 3 4
5 6 7 8
9 10 11 12
>> x = M(2, 3) % element at row 2 & column 3 of M
x=
>> y = M(2, :) % select the 2nd row M
7
y=
5 6>>7z 8
= M(2:3, 2:3) % select sub-matrix of M
z=
6 7
 C.B. Pham
10 11 9
Concatenating, Appending, …

>> A = [ 1 2 3 ] ;
>> B = [ 4 5 6 ] ;
>> R = [ A B ]
R= >> S = [ A; B ]
1 2 3 4 5 6 S= >> S(3, 3) = 7
1 2 3 S=
4 5 6 1 2 3
4 5 6
0 0 7

Note: if you store a value in an element outside of the


matrix, the size increases to accommodate the newcomer.
 C.B. Pham 10
Complex number operations >> x = 3 + 4*i;

>> abs(x) % Absolute value


ans = >> angle(x) % Phase angle (in radians)
5 ans =
0.9273
>> conj(x) % Complex conjugate
ans = >> imag(x) % Complex imaginary part
3.0000 – 4.0000i
ans =
4

>> real(x) % Complex real part


ans =
3
 C.B. Pham 11
Some Useful Functions

• Some useful math functions:


sin(x), cos(x), tan(x), atan(x), exp(x), log(x), log10(x),
sqrt(x), …
>> t = [0 : 0.1 : 10] ;
>> x = sin(2 * pi * t) ;

• Some useful matrix and vector functions:


>> length(x) >> size(x)
ans = ans =
101 1 101

 C.B. Pham 12
More Operators & Functions
>> M = [ 1 2; 5 6 ]
M=
>> sum(M)
1 2
ans =
5 6
6 8
>> sum(ans) % equivalent to sum(sum(M))
ans =
14 >> M’ % equivalent to transpose(M)
ans =
1 5
2 6 >> diag(M) % diagonal elements
ans =
1
 C.B. Pham 6 13
More Operators & Functions

• Find the roots of the polynomial

>> f = [ 1 2 3 4 ] ; % create f = x3 + 2x2 + 3x + 4


>> roots(f)
ans =
-1.6506
-0.1747 + 1.5469i
-0.1747 - 1.5469i

 C.B. Pham 14
More Operators & Functions
5x  3
• Partial Fraction Expansion x 3  3x 2  4

8 7 8
9 3  9
x  2 ( x  2) 2 x  1

>> [R, P, K] = residue([ 5 3 ] , [ 1 3 0 -4 ])


R= P= K=
-0.8889 -2.0000 []
2.3333 -2.0000
0.8889 1.0000
 C.B. Pham 15
3. Plots in Matlab

 C.B. Pham 16
Continuous time plots - plot

The plot function has different forms, depending on the


input arguments.
>> x = [ 0 : 0.1 : 2*pi ] ;
>> y = sin(x) ;

>> plot(y) >> plot(x, y)

 C.B. Pham 17
Discrete time plots - stem

Stem function is very similar to plot. It is used to


plot discrete time sequences.

>> x = [ 0 : 0.5 : 10 ];
>> y = x.^2;
>> stem(x, y)
>> grid on
>> xlabel(‘Time, t’)
>> ylabel(‘Speed, s’)
>> title(‘Graph s = t^2’)

 C.B. Pham 18
Multiple Data Sets in One Graph

>> t = [ 0 : 0.001 : 2*pi ];


>> x = sin(t);
>> y = sin(t - 0.5);
>> z = sin(t - 1);
• Approach 1
>> plot(t, x, t, y, t, z)
• Approach 2
>> plot(t, x, t, y, t, z)
>> plot(t, x, ‘b’)
>> hold on
>> plot(t, y, ‘g’)
>> plot(t, z, ‘r’) >> grid on
>> hold off >> legend(‘sin(t)’, ‘sin(t – 0.5)’, ‘sin(t - 1)’)
 C.B. Pham 19
Plot with two different y-axes

>> t = [0 : 0.001 : 2*pi];

>> p = sin(2*t);
>> v = 2*cos(2*t);

>> plot(t, p)
>> plot(t, v)

>> plotyy(t, p, t, v)

 C.B. Pham 20
4. Function in Matlab
• Syntax
function [out1, out2, …] = function_name (in1, in2, …)
…..
….. % commands

• Description:
function [out1, out2, ...] = function_name (in1, in2, ...)
defines function function_name that accepts inputs in1,
in2, etc. and returns outputs out1, out2, etc.

• Note: When you create a function in a Matlab .m file and


you want to call it from the workspace, make sure the name
of the .m file is the same as the name of the function itself
(good programming practice).
 C.B. Pham 21
Example: make function of area

File saved as ‘dien_tich.m’


>> dien_tich(2, 3, 1)
function a = dien_tich(l1, l2, d) ans =
if (d == 1) % rectangle 6
a = l1 * l2 ; >> dien_tich(2, 3, 2)
elseif (d == 2) % triangle ans =
a = l1 * l2 / 2 ; 3
else % others >> dien_tich(2, 3, 3)
a = -1 ; ans =
end -1

a A = a.b
h
A = a.h / 2
b
 C.B. Pham a 22

You might also like