Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Similar presentations


Presentation on theme: "ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )"— Presentation transcript:

1 ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

2 W HY 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

3 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

4 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

5 M ATRICES, A RRAYS AND V ECTORS 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.

6 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

7 T O S TART M ATLAB Command window Single line commands, results Editor Edit scripts Workspace Store variables Current Folder Store files

8 T O S TART M ATLAB

9 Command window Only one line a time Create a new.m file File->New->Script Check the usage of ‘;’, ‘%’, ‘%’ Save and Run

10 P RESENT M ATRICES 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.

11 A RITHMETIC O PERATIONS Scalar operations: There are four scalar operations addition: + subtraction: - multiplication: * division: /

12 Example >> a=[1 2 3; 4 5 6] >> b=3*a >> b=[3 6 9; 12 15 18]

13 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]

14 Multiplication and Division 2 different types: componentwise and conventional Recall: How to multiply matrices?

15 Normal multiplication: A, B, C is n by n In MATLAB >> C=A*B

16 Componentwise multiplication: Same problem as before, In MATLAB >> C=A.*B

17 F UNCTIONS AND S HORTCUTS Functions: operations that can be called in a scripts Zeros() Ones() Eye() Diag() Linspace() …

18 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.

19 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

20 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

21 size() inv() []’ fft() Functions can be self defined. Most functions work componentwise

22 S ELF - DEFINED FUNCTION File->New->Function Input arguments Output arguments Function name How to call function

23 H OW 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.

24 t=0:0.1:5 y=sin(2*pi*t) Plot(t,y)

25 t=0:0.001:5 y=sin(2*pi*t) Plot(t,y)

26 plot(t,y,'--')

27 plot(t,y,'g')

28 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.g xlabel('time') Similar: ylabel, zlabel

29

30 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')

31 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')

32 HW1HW1 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')

33 HW1HW1

34 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:

35 B ASIC P ROGRAMMING S YNTAX FOR ‘for’ is used for repeating statements Example: t=0; for i=1:5 t=t+i; end Also see WHILE

36 IF If Conditionally execute statements. Example t=0; for i=1:5 if i>3 t=t+i; end

37 Q UESTIONOS


Download ppt "ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )"

Similar presentations


Ads by Google