Matlab: Matrix Laboratory
Getting started: Menu / Help / Examples and
demos
|
X = 12 |
X is
a 1*1 matrix |
Math
functions:
|
log(X) log(33) sin(X) cos(X) cosh(X) sqrt(X) … |
|
Basic
stuff:
|
a
= [1 9 –8.9 0 5] a
= [1 9 –8.9 0 5]; sin(a) b
= a + 2 a
= a * 2 d
= a .* a plot(d) grid
on gird
off bar(b) pie(b) A
= zeros(5) B
= ones(8) B
= ones(2,3) C
= rand(2) a
= 32:50 a
= 0:0.1:2*pi; b
= sin(a).*sin(a); plot(b) plot(a,b) hold off
hold
on c
= cos(a) plot(a,
c); hold
off plot(a, b, ':r', a, c,
'-b')
s
= size(a) str = sprintf('Sin^2(t) and
str =
title(str) |
Doesn’t echo the
result Sin() of each
element Each
element +2 Each
element time 2 Element by
element 5*5
matrix all elements 0 8*8
matrix all elements 1 2*3
matrix all elements 1 2*2 matrix all elements random numbers between 0 and 1 (all
numbers are in long format) an
array x
axis: b’s index x
axis: a’s elements Hold is on Ž both plots A nicer way to have both plots. :r means dotted red -b means solid blue See help (icon: ?) on plot Use plot in 2D. For 3D use mesh, surf, plot3, … formatted print into a string NOTE: single quotations unlike C |
|
legend('sin(t)^2',
'cos(t)') xlabel('t') ylabel(''); gtext('Blah blah')
axis ([ -1 8 -1.5
1.5]) a
= 1:0.1:50; b
= a.^2 + 3*a; plot(a,
b); semilogx(a,
b); grid |
Can drag with mouse (notice how nice ^2 is shown in the title and in the legend!) put text with mouse [x_min x_max y_min y_max ] logarithmic charts: use
semilogx, semilogy, and loglog (instead of
plot) polar for polar
plots |
Matrix
calculations:
|
A = [1 2 3 ; 4 5 6; 7 8
9] B =
A' C
= A*B
CC = A .*
A det(A) D
= inv(A) A * AV
= eig(A)
Svd(A) [v, d] =
eig(A) B
= A * v B
./ v A(:,
1) A(2,
::) A(:,
2:3) B
= [ A(1,:) 2 4
6] |
3*3
matrix Transpose Matrix
multiplication Element by
element determinant Inverse of A (inf if
non det(A)==0) is
I Eigen values of matrix
A (
A.V = l.V ) if
det(A)==0 => at least one 0.0 eigenvalue singular value
decomposition eigenvalue decomposition
The
first column The
second row The
2nd and 3rd rows |
VERY IMPORTANT
NOTICE FOR C PROGRAMMERS: index from 1 (NOT
0)
Example
(convolution):
|
clear a
(1, 1:100) = 0; a(1, 30:70) =
2; plot(a) axis([0 100 -1 5])
b(1, b(1, 30:40) =
3; plot(b) axis([0 60 -1
5]) c
= conv(a, b); plot(c) axis([0 200 -10
100]) |
Trapezoid
(if the two pulses have
equal length Ž triangle) |
Solving
equations:
|
A
= [1 0 1] Roots(A) |
X2 + 1 =
0 1
and –1 |
Or
simply: Impulse
response: |
clear A
= [0 1; -1 -2] B = [0;
1] C = [1
0] D =
1 t
= 0:0.01:10; y
= step(A, B, C, D, 1, t); plot(t,y) y
= step(A, B, C, D) grid A = [0 1 ; -2
-0.5] y
= step(A, B, C, D, 1, t); plot(t,y) grid
on y
= impulse(A, B, C, D, 1, t); plot(t,y) |
Y = C X + D
U Step
response Has
imaginary poles left side of the jw axis Ž damping
oscillation |
Step
response of an LTI system defined by its transfer
function
|
num = [1
1] den1 = [0 1
2] den2 = [1 1
1] den = conv(den1,
den2) G
= tf(num, den) Damp(G) Pole(G) Tzero(G) pzmap(G) rlocus(G) [k, poles ] = rlocfind
(G) step(G) impulse(G) bode(G) |
Transfer
function Eigenvalues, damping and
natural frequencies Poles and
zeros Pole
& zero map Root
locus Step
and impulse responses Bode
diagram |
change
into space-space representation:
Question: What would the dimension
of 'A' be?
|
Pss =
ss(G) step(Pss) [a, b, c, d] =
ssdata(Pss) |
Same
as step(G) |
Note to C/C++ programmers: notice
that in Matlab functions can have multiple return values
State-space Ž transfer
function
|
[num, den] = ss2tf(a, b, c,
d, 1) |
|
M files:
Apart from
very basic things, everything else is an M file.
Example:
lti\impulse.m
Interpreter (not a compiler)
Ž in case of error stops at the
error
Save your
files with .m extension and add your folder to Matlab’s
path
A simple
m file that calculates the
square root of a number
Another
simple m file that has a
function that returns the sinus and cosinus of a number
Example: an m file that shows the step
response of the following system for 3 different values for k: 2, 20 and
100
A hint
for having faster m files: for loops are very slow. Avoid
them as much as you can!
Exmple:
Instead
of:
|
For i =0:100 A(i+1) =
sin(i); end |
|
Use
this:
|
A
= sin([0:100]) |
|
Want to
learn more:
1)
Use
the help and demos
2)
Google “Matlab Tutorial” and you’ll
fine a zillion! (there are especially some good tutorials on how to avoid for
loops)