MatLab Tutorial by Aaron Rababaah
MatLab Tutorial by Aaron Rababaah
AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 1) MatLab: Matrix Laboratory interpreted vs. compiled
2) Interface Windows
command shell
command history
3) Calculator
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 Format: long, short, hex, bank (#.##), rat
>> 0.5714
>> ans
>> 4/7
Declaring variables
Expressions
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 ans
unsigned int
signed int
realmin, realmax
Matrices: m = [;;;], zeros(nr, nc), ones(nr, nc), rand(nr, nc), randn(nr, nc), eye(nr, nc) (identity)
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 Vectors: v = [], 0:1:100, use other functions as in matrices above, transpose ()
fliplr(m) flip left-right, flipud(m) flip up down, flipdim(m,dim) flips m in the direction of dim, dim=1,2,3
Advanced mat ops: tril, triu(m) (lower/upper triangles),diag(m), trace(m) =(sum of eigen vals), det=determinant, inv=inverse, eig=eigen vals, svd (singular value decomp), poly(m) =characteristic polynomial of det(lmda.I A), roots(poly(m)) = eigen vector. Same as eig(m). row reduced echelon form: rref(m), rrefmovie(m), rank(m) = number of non-zero rows in the RREF form.
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 length, size, numel (for 2D mat = rows*cols), sum, cumsum (cumulative sum, good CDF from hist or PDF), prod (v), min, max, mean, median, range, std, var, find(v<.1), any(v) (Boolean if any elem is true), all(v) (Boolean if all elems are true), sort(v, 'ascend or descend)
Reshape:
>> x = rand(4,3)
>> z = reshape(y, 4, 3)
Cell: x = {}; x{1} = rand(3), x{2} = 'hello', x{3} = 123, x{4} = 99;
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011
5) Utility Functions
isprime(x)
pause(n_sec)
d = fix(clock); 2012
25
10
52
40
6) Symbolic Math
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 Declare symbolic vars: syms x y z
Subs substitutes, (one var at a time): subs(f, x, 3), subs(f, y, 5) or all at once: subs(f, {x,y}, {3,5})
for a given function f= (x-1)*(x-2)*(x-3): collect(f), expand(f), horner(f) (poly nested rep), factor(f).
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 polynomial roots(p): p is coeff vector: f = -1+x^2, roots([-1 0 1]), polyval(p,x): p=coefficients of a polynomial,x=aval at wich the ploy need to be evaluated. e.g. 1+5x-3x^2, p=[3 5 1].
Fzero(fun, x0): fun can be anonymous, string expression or mfile, x0 = initial guess:
m-file: x = fzero(@myfun,x0);
anonymous: x = fzero(@(x)sin(x*x),x0);
7) Signal Generation
t = -10:.01:10;
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 x = ones( length(t), 1 ); plot(t, x)
% uniform
% gaussian
x = sin(2*pi*.25*t);plot(t,x)
x = cos(2*pi*.25*t);plot(t,x)
x = square(2*pi*.25*t);plot(t,x)
x = sawtooth(2*pi*.5*t);, plot(t,x)
x = sinc(2*pi*.25*t);plot(t,x)
x = gauspuls(.001*t);, plot(t,x)
x = rectpuls(t);, plot(t,x)
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 x = tripuls(t);, plot(t,x)
How about: combination of different signals, adding simulated uniform noise, Gaussian noise
8) Plotting
Basic: plot(x, y, L-C): L=line/marker symbol: ., -, --, +, *, x, etc. C= line color: r=red, g=green, b=blue, etc.
xlabel(x-axis')
xlabel('y-axis')
grid on
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 title('my plot x vs. y')
for i=1:5
end
legend(p, lgnd);
v0 = rand(100,1); plot(v0); bar(v0), stem(v0), area(v0), hist(v0), scatter([1:1:length(v0)], v0, [1:1:length(v0)]) = (x,y, size, color)
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 area: v1 = rand(100,1); v2 = 2 * rand(100,1);v3 = 3 * rand(100,1); area([v1 v2 v3])
subplot(nr, nc, which): subplot(2,2,1); plot(v0), subplot(2,2,2); bar(v0), subplot(2,2,3), stem(v0), subplot(2,2,4); hist(v0)
bar3(v0)
bar-colormap:
>> x = rand(1,5);
>> h = bar(x);
>> set(h_child,'CData',x);
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 meshgid: [x y] = meshgrid([0:.1:2], [0:.2:4]), z = cos(x) .* sin(y); surf(x,y,z)
9) Programming M-Files
sub functions
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 global vs. local vars
if cond
instructions;
else
instructions;
end
==, <, >, <=, >=, ~=, & (&&), | (||), ~, or, and, not, xor, bitor, bitand, bitxor,bitshift(A,nBits): +ve n = right shift, -ve n = left., bitcomp (complement), bitset(A, bitpos, binval), bitget(A, bitpos)
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 dec2bin(int, nbits), bin2dec(binstring), hex2dec(hexstring), dec2hex(int, nhexdig), base2dec(anybasestring, base): base2dec(10212, 3)
case val1
instructions;
case val2
instructions;
case valn
instructions;
otherwise
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 instructions;
end
for i=i0:di:i1
instructions;
end
nested for
for i=1:1:n
for j=1:1:m
disp( m(I,j) );
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 end
end
while condition
instructions;
end
fprintf('%6d
%6.3f
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 6d: 6 = total width of the decimal num, space-padded
6.3f: 6=width of the int part, .3= ndecimal points (precision), space-padded
addpath(dir)
10)
CPU Time
tic;
for i=1:n
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 x = i^10;
end
dt = 1000 * toc;
// time in (ms)
11)
Strings:
x = 99; y = 11.88; str = ['another way to write to strings' 'x=' num2str(x) '; y=' num2str(y)]
12)
FileIO:
dlmread: m = dlmread(fname.txt)
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011 dlmwrite: dlmwrite('txt.txt', rand(9), 'delimiter', '\t', 'precision', '%.3f')
xlsread: m = xlsread('c:\matlab\work\myspreadsheet.xls');
m = xlsread('c:\matlab\work\myspreadsheet.xls','sheet2');
m = xlsread('c:\matlab\work\myspreadsheet.xls','sheet2','a2:j5')
m = xlsread('c:\matlab\work\myspreadsheet.xls',2,'a2:j5')
the 4th call is the same as the 3rd only using sheet index (2) instead of the sheets name.
xlswrite: xlswrite('c:\matlab\work\myspreadsheet.xls',sheet2,'a2:c4')
UNIVERSITY OF MARYLAND EASTERN SHORE DEPARTMENT OF MATH AND COMPUTER SCIENCE DR. AARON RABABAAH TUTORIAL ON MATLAB BASICS FALL 2011