1 / 37

Qiwei Wang

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

mahon
Download Presentation

Qiwei Wang

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Qiwei Wang Graduate teaching Assistance “Matlab_intro” ECE 351 Winter 2014

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

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

  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. To Start Matlab • Command window Single line commands, results • Editor Edit scripts • Workspace Store variables • Current Folder Store files

  8. To Start Matlab

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

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

  11. Arithmetic Operations • 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. Functions and Shortcuts • 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. Self-defined function • File->New->Function • Input arguments • Output arguments • Function name • How to call function

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

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

  29. 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')

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

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

  32. Hw1

  33. 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:

  34. Basic Programming Syntax • FOR ‘for’ is used for repeating statements Example: t=0; for i=1:5 t=t+i; end Also see WHILE

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

  36. Questionos

More Related