 Download Download Presentation Basics of MATLAB (1)

# Basics of MATLAB (1)

Download Presentation ## Basics of MATLAB (1)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Basics of MATLAB (1) Digital Signal Processing

2. Basics of Matlab (1) a. Matlab windows, general commands b. Arithmetic operations, exponential and logarithms c. Creating and working with arrays and vectors d. How to write a basic m-file: cleaning memory, saving data files e. Matrix and array operations f. Using built-in functions g. Plotting simple graphs Basics of Matlab (2) a. Programming in Matlab b. Using loops, scripts c. Using functions d. Review Syllabus

3. MATLAB? • MATrix LABoratory • Primarily a numerical computation package • Using vectors and matrices, • Ease of use, availability of built-in functions, ease of programming, and speed • Wide range of applications • Signal and image processing, communications, control design, test and measurement, financial modeling and analysis, computational biology,

4. MATLAB • User-written Functions or Built-in Functions • Graphics • Computations • Extra Functions - Toolboxes • Collection of specialized functions : DSP, Statistics, Optimization

5. MATLAB windows • Quit • Ctrl+Q

6. Command Window

7. Command History Statement Timestamp

8. MATLAB windows • Graphics window • Output of all graphics commands typed • Edit window • Write, edit, create, and save your own programs in files called ‘M-files’ • Help window • type in “helpdesk” or Help-product help on the menu bar

9. Help Window • In the Help browser Search field,

10. Files and Directories • Look where you are: see “current folder” or type in “pwd” (print working directory) • Change current directory • type in “cd directory name” or • navigate through the browse button

11. Useful Commands • clc: clear command window • clears all input and output from the Command Window display • clear: remove items from workspace • help fname: needs exact name of the function • lookfor keyword: keyword search

12. File formats Or use import wizard (File-import): type in “uiimport”

13. M files • Matlab executes files that have the extension “.m” (called M-files). To get Matlab to execute the commands in the file, simply type in “name” • Matlab will then search the current directory for the file “name.m", read the file, and execute the commands in the file. • If matlab cannot find the file you will get an error message: ??? Undefined function or variable ‘name’

14. Arithmetic calculation + :Addition - :Subtraction * :Multiplication / :Division ^ :Power ' :complex conjugate transpose On arrays + :Addition - :Subtraction .* :Element-by-element multiplication ./ :Element-by-element division .^ :Element-by-element power ' :array transpose Basic Operations

15. Functions • Trigonometric functions • Input can take a single scalar, vector, or matrix • sin(), asin(); cos(), acos(); tan(), atan(); sec(), csc() ex) q = [0 pi/2 pi] ; sin(q) gives [0 1 0] • Exponential functions • exp(), log(), log10(), sqrt() • Complex functions • conj() (complex conjugate), imag(), real()

16. Functions • Type in “help elfun” • List of elementary mathematical functions • pi : 3.14159265... • i or j : Imaginary unit • Inf : Infinity (dividing a nonzero value by zero) • NaN : Not-a-number (0/0 orInf-Inf) Exercise #1

17. Exercises #1 P=3*(sqrt(5)-1)/(sqrt(5)+1)^2-1 Q1=log(exp(3)) Q2=log10(exp(3)) R=(sin(pi/6))^2+(cos(pi/3))^2 S=(1+3i)/(1-3i) ------------------------------------------------------

18. Exercises #1 P=3*(sqrt(5)-1)/(sqrt(5)+1)^2-1 Q1=log(exp(3)) Q2=log10(exp(3)) R=(sin(pi/6))^2+(cos(pi/3))^2 S=(1+3i)/(1-3i) ------------------------------------------------------ P = -0.6459, Q1 = 3, Q2 = 1.3029 R = 0.5, S = -0.8 + 0.6i

19. Round-off functions fix: round towards 0 floor: round towards -∞ ceil: round towards +∞ round: round towards the nearest integer rem: remainder after division Ex) A=[-2.33 2.66] fix(A) = [-2 2] floor(A) = [-3 2] ceil(A) = [-2 3] round(A) = [-2 3] If a=[4 9 11] and b=[2 4 3] rem(a,b)=[0 1 2] Functions Exercise #2

20. Exercise #2 P=-0.6459 Q2=1.3029 (1) fix(P)+floor(Q2) = (2) ceil(Q2)+round(P) =

21. Exercise #2 P=-0.6459 Q2=1.3029 (1) fix(P)+floor(Q2) = 0 + 1 = 1 (2) ceil(Q2)+round(P) = 2 + (-1) = 1

22. Functions • “Inline” function • For a mathematical function, such as F(x) or F(x,y): requires just the independent variables for computing the value of the function name of f = inline (‘function formula’) ex) when x=1 and y=2 Fxy=inline(‘x^2*y’); U1=Fxy(1,2) Exercise #3

23. Exercise #3 Fxy=inline('(x^5+y^2)/exp(x/y)') (1) x= 5 and y=3: Fxy(5,3) = (2) x=-4 and y=2: Fxy(-4,2) =

24. Exercise #3 Fxy=inline('(x^5+y^2)/exp(x/y)') (1) x= 5 and y=3: Fxy(5,3) = 591.9361 (2) x=-4 and y=2: Fxy(-4,2) = -7536.8

25. Matrices and Arrays • Think of everything as a matrix • While other programming languages work with numbers one at a time,MATLAB allowsyou to work with entire matrices quickly and easily.

26. Entering Matrices • Enter an explicit list of elements. • Load matrices from external data files. • Generate matrices using built-in functions. • Create matrices with your own functions in M-files.

27. Separate the elements of a row with blanks or commas Use a semicolon, ; , to indicate the end of each row Surround the entire list of elements with square brackets, [ ] A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] Entering Matrices - Enter an explicit list of elements A= Exercise #4.(1)

28. Exercise 4.(1) k=[3 4 5 8;-2 7 1 2;5 2 -1 3] l=[3 4;1 -2;6 5;3 2]

29. Entering Matrices - Load matrices from external data files • Create a txt file of the matrix A • Save the file as matrixa.txt • Type in “load matrixa.txt” to the command window • Name it A: A=matrixa Exercise 4.(2)

30. Exercise 4.(2) Create a txt file including below format of numbers -2 1 7 1 5 3 2 4 3 5 -8 6 6 9 4 -3

31. Entering Matrices - Generate matrices using built-in functions • zeros: All zeros • ones: All ones • eye: 1 on the main diagonal • rand:Matrix of random numbers Ex) Z = zeros(2,4) Z = 0 0 0 0 0 0 0 0 F = ones(2,3) F = 1 1 1 1 1 1 F = eye(3,4) F = 1 0 0 0 0 1 0 0 0 0 1 0 Exercise 4.(3)

32. Exercise 4.(3) B = 5*ones(5,3) C=zeros(3,2) D=eye(5,4)

33. Entering Matrices - Generate matrices using built-in functions • diag (A) pulls out the diagonal of A in a column vector • diag (A,1) specifies the off-diagonal vector to be pulled out • Positive value: upper off-diagonal vector • Negative value: lower off-diagonal vector Exercise # 5.

34. diag(A) returns a vector 4 3 1 0 diag(A,2) returns a vector 7 4 Example Exercise #5

35. Use the MATLAB Editor or another text editor to create a filecontaining the same statements you would type at the MATLAB commandline Go to “Desktop” on the menu bar – Click “Editor” Create new script (m file) that will give the same matrix as A we created before Type in A_script= [16 3 2 13; 5 1011 8; 9 6 7 12; 4 15 14 1]; And name the file as scriptA or whatever you like Exercise: Create a new script that creates k, l, m, B, C, D matrices Entering Matrices - Create matrices with your own functions in M-files.

36. Matrix Operations - Concatenation • Joining small matrices to make bigger ones • Typing in “D = [A A+32; A+48 A+16]” creates D = 16 3 2 13 48 35 34 45 5 10 11 8 37 42 43 40 9 6 7 12 41 38 39 44 4 15 14 1 36 47 46 33 64 51 50 61 32 19 18 29 53 58 59 56 21 26 27 24 57 54 55 60 25 22 23 28 52 63 62 49 20 31 30 17

37. Matrix Operations • kron(A,B): Kronecker tensor product Exercise #6

38. Exercise #6 >> A=[3 5;1 7] A = 3 5 1 7 >> X=kron(eye(3),A) X = 3 5 0 0 0 0 1 7 0 0 0 0 0 0 3 5 0 0 0 0 1 7 0 0 0 0 0 0 3 5 0 0 0 0 1 7

39. Recap • help: needs exact name of the function • lookfor: keyword search • Arithmatic calculations (putting ‘.’ makes it element-by-element calculation) • + :Addition, - :Subtraction, * :Multiplication, / :Division, ^ :Power

40. Recap • Trigonometric functions • Input can take a single scalar, vector, or matrix • sin, asin; cos, acos; tan, atan; sec, csc • Exponential functions • exp, log, log10, sqrt • Type in “help elfun” • List of elementary mathematical functions

41. Recap • Round-off functions • fix: round towards 0 • floor: round towards -∞ • ceil: round towards +∞ • round: round towards the nearest integer • rem: remainder after division • “Inline” function • For a mathematical function, such as F(x) or F(x,y): requires just the independent variables for computing the value of the function name of f = inline (‘function formula’,’x’,’y’)

42. Recap • Fpq=inline(‘p^2+q’,’p’,’q’) • Fpq(1,2) gives value when p=1 and q=2 • Fpq=inline(‘p^2+q’,’q’,’p’) • Fpq(1,2) gives value when p=2 and q=1

43. Recap • zeros: All zeros ex) zeros(a,b) • ones: All ones ex) ones(a,b) • eye: 1 on the main diagonal ex) eye(a,b) • rand: Matrix of random numbersex) rand(a,b) • diag (A) pulls out the diagonal of A in a column vector • diag (A,1) specifies the off-diagonal vector to be pulled out • Positive value: upper off-diagonal vector • Negative value: lower off-diagonal vector

44. Matrix Operations - Sum, Transpose, and Subscripts Try sum(A) How about the row sums? By transpose: ’ or .’ (real numbers) Sum (A’)’ A(i,j): The element in row i and column j Make the code adding the elements of the second row inv(A): inverse matrix of A A=