370 likes | 455 Views
Qiwei Wang. Graduate teaching Assistance “ Matlab_intro ” ECE 351 Winter 2014. Why MATLAB?. The name MATLAB ( mat rix lab oratory), is Developed by Mathworks . http://www.mathworks.com/products/matlab/index.html
E N D
Qiwei Wang Graduate teaching Assistance “Matlab_intro” ECE 351 Winter 2014
Why MATLAB? • The name MATLAB (matrix laboratory), is Developed by Mathworks. http://www.mathworks.com/products/matlab/index.html • While the software has progressed well beyond its original goal as a tool dedicated to performing matrix computations, it is still based on the notation of arrays and matrices. • The latest version is R2013b
MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks. • High-level language for technical computing • Development environment for managing code, files, and data
Interactive tools for iterative exploration, design, and problem solving • Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration • 2-D and 3-D graphics functions for visualizing data
Matrices, Arrays and Vectors • A matrix is simply a rectangular list of numbers • The "size" of a matrix is given as two numbers, the first is traditionally the number of rows in the matrix while the second is the number of columns in the matrix. • Matrices are usually written in tabular form contained between two large parentheses or square brackets.
Arrays/vectors: matrices with only ONE row or column • Example A 2×3 (two by three)matrix 34 56 31 -45 6 43 A 1×3 row vector 34 56 31 A 2×1 column vector 34 -45
To Start Matlab • Command window Single line commands, results • Editor Edit scripts • Workspace Store variables • Current Folder Store files
To Start Matlab • Command window Only one line a time • Create a new .m file File->New->Script • Check the usage of ‘;’, ‘%’,‘%%’ • Save and Run
Present Matrices in MATLAB >> a=[1 2 3; 4 5 6] Or >> a=[1, 2, 3; 4, 5, 6] Try on your own computer if you haven’t done so.
Arithmetic Operations • Scalar operations: There are four scalar operations • addition: + • subtraction: - • multiplication: * • division: /
Example >> a=[1 2 3; 4 5 6] >> b=3*a >> b=[3 6 9; 12 15 18]
Matrix Addition and Subtraction For two matrices to be added or subtracted they must be of the same size. • The entries are computed by adding or subtracting the corresponding entries in the two original matrices. • Example >> a=[1 2 3; 4 5 6] >> b=[2 4 6; 3 5 7] >> c=a-b c=[-1 -2 -3; 1 0 -1]
Multiplication and Division 2 different types: componentwise and conventional • Recall: How to multiply matrices?
Normal multiplication: A, B, C is n by n • In MATLAB >> C=A*B
Componentwise multiplication: Same problem as before, • In MATLAB >> C=A.*B
Functions and Shortcuts • Functions: operations that can be called in a scripts • Zeros() • Ones() • Eye() • Diag() • Linspace() • …
Zeros(n) to create an n by n matrix with all entries zero. Zeros(n, m) create an n by m one. • Ones(n) to create an n by n matrix with all entries 1. Used as the same fashion as zeros(n) • Eye(n) to create an n by n identity matrix. • Diag([]) to create a diagonal matrix. Vector given shows as diagonal elements.
Two shortcuts for row vectors: 1. vector=a:n:b the vector starts with a and end with b, n is the step size Example >> t=1:0.5:3 t=1 1.5 2 2.5 3 If n is not given, default value is 1 >> t=1:3 t=1 2 3
2 linspace(a, b, n) to create a row matrix with n elements, start from a and end with b, with equal step size. Example: >> t=linspace(0,10,11) t=0 1 2 3 4 5 6 7 8 9 10
size() • inv() • []’ • fft() • Functions can be self defined. • Most functions work componentwise
Self-defined function • File->New->Function • Input arguments • Output arguments • Function name • How to call function
How to plot figures? Plot(x,y): 2 vector input, x will be horizontal axis and y be the vertical one. x, y must be the same size s: applicable parameters: color, plot symbol, line type used as character strings.
t=0:0.1:5 • y=sin(2*pi*t) • Plot(t,y)
t=0:0.001:5 • y=sin(2*pi*t) • Plot(t,y)
Legend, title and label legend(‘strings1’,’strings2’…..’location’,’orientation’) e.g legend('sin function') • Title Title(‘text’) e.g title('function') • Label Xlabel(‘text’) e.gxlabel('time') Similar: ylabel, zlabel
Example 1 • t=0:0.001:5 • x=cos(2*pi*t) • y=sin(2*pi*t) • plot(t,x,'g') • hold on • plot(t,y,'r')
Example 2 • t=0:0.001:5 • x=cos(2*pi*t) • y=sin(2*pi*t) • subplot(2,1,1) • plot(t,x,'g') • subplot(2,1,2) • plot(t,y,'r')
Hw1 • t = (-2:0.01:2)/1000; • a1 = 500; • x1 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a1*t); • a2 = 750; • x2 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a2*t); • a3 = 1000; • x3 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a3*t); • %Plot Resutls • figure(1); • clf; • plot(t,x1,'b'); • hold on • plot(t,x2,'k:'); • plot(t,x3,'r--'); • hold off • xlabel('time (sec)') • ylabel('Amplitude') • title('Exponentially Damped Sinusoid') • axis([-2/1000 2/1000 -120 120]) • legend('a = 500','a = 750','a = 1000')
2. semilogy(x, y, s) logarithmic (base 10) scale is used for the Y-axis. Used as the same fashion as plot. • Similar: semilogx, loglog. Example:
Basic Programming Syntax • FOR ‘for’ is used for repeating statements Example: t=0; for i=1:5 t=t+i; end Also see WHILE
IF If Conditionally execute statements. Example t=0; for i=1:5 if i>3 t=t+i; end end