1 / 27

MATLAB

MATLAB. Lecture Two Tuesday 5 July 2005. Chapter 3. Matrices and Vectors. Input matrix A = [ 1 2 5; 3 9 0] Use semicolon ";" to suppress output Use … for line continuation. Matrices and Vectors. Vector is a special case of matrix Scalar is considered 1x1 matrix

wilhelminar
Download Presentation

MATLAB

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. MATLAB Lecture Two Tuesday 5 July 2005

  2. Chapter 3

  3. Matrices and Vectors • Input matrix • A = [ 1 2 5; 3 9 0] • Use semicolon ";" to suppress output • Use … for line continuation

  4. Matrices and Vectors • Vector is a special case of matrix • Scalar is considered 1x1 matrix • g = 9.81 (no [ … ] needed) • X = [ ] creates a null matrix • Examples on page 51.

  5. Matrices and Vectors • Matrix indexing • A(i,j) ith row jth column • A(m:n, k:l) specifies submatrix • A(:, j) jth column • A(i,:) ith row • A(m:k:n,:) row m, m+k,m+2k, etc.

  6. Matrices and Vectors • Dimension • B(2,3) = 5 • matrix B created large enough to have the (2,3) entry, I.e., B is 2 x 3. • The dimension is increased dynamically

  7. Matrices and Vectors • Matrix Manipulation • array as index, A([1 3 4],:) • reshape(…) function • Transpose by .’ • Hermitian conjugate by ’ • Initialization

  8. Matrices and Vectors • Appending a row or column • A = [A; u]; A = [ A u'] • Deleting a row or column • A(2, :) = [ ], delete 2nd row • A(:,1) = [ ], delete 1st column

  9. Matrices and Vectors • Utility matrices • eye(m,n), zeros(m,n), ones(m,n), rand(m,n), diag(A) • rot90, fliplr, flipud, tril, triu • Examples on page 56

  10. Matrices and Vectors • Creating vectors • v= initialV : increment : finalV • e.g., a = 0:10:100 • linspace(a,b,n) and logspace(a,b,b) functions

  11. Matrices and Vectors • Matrix operations • + addition • - subtraction • * multiplication • / division • ^ exponentiation

  12. Matrices and Vectors • Left division and right division • A / B is A B-1 (right division) • A \ B is A-1 B (left division), in particular the solution to equation Ax = b, is x = A\b.

  13. Matrices and Vectors • Element-by-element operation • .* element-wise multiply • ./ element-wise right division • .\ element-wise left division • .^ element-wise exponentiation • .' nonconjugated transpose • Examples on page 60.

  14. Relational Operations • < Less than • <= Less than or equal • == equal • > greater than • >= greater than or equal • ~= not equal (different from C)

  15. Logical Operations • & logical AND • | logical OR • ~ logical complement (NOT) • xor exclusive OR (a function)

  16. Logical Functions • all, any, exist, isempty, isinf, isfinite, isnan, find

  17. Math Functions • sin, asin, cos, acos, tan, atan, atan2, sec, cot, sinh, exp, log, log10, sqrt, abs, angle, conj, real, imag, fix, floor, ceiling, sign, etc. • Matrix functions • expm(A), logm(A), sqrtm(A)

  18. Character Strings • Strings are quoted by single quotes • message = 'Leave me alone' • Strings are considered as 1xn matrix.

  19. Eval Function • Eval interprets string as a MATLAB commend • eval('x = 5*sin(pi/3)')

  20. Lookfor • Lookfor search for key word in the function documentation • eigenvalue example

  21. Save and Loading Data • save • save entire workspace in the file matlab.mat • load • load the saved file • save file.mat x y z • save the values x, y, z in file.mat

  22. Recording a Session with "diary" • diary filename • Save the session to filename • diary off • Turn of the diary recording

  23. Plotting • plot(x, y) • plot(x1,y1,x2,y2) • xlabel, ylabel, title, print

  24. Exercises • Lesson 5, exercise 3. • Write a function factorial to computer factorial n! for any integer n.

  25. Exercises • Lesson 5, exercise 5. • Write a function to compute the sum of a geometric series 1 + r + r2 + r3 + … + rn for a given r and n.

  26. Exercises on page 74, problem 1, 4, and 5 • Enter matrices • A = [2 6; 3 9] • B = [1 2; 3 4] • C = [-5 5; 5 3] • Create a big matrix that has A, B, C on the diagonal

  27. Exercises on page 74, problem 1, 4, and 5 • Delete the last row and last column • Extract the first 4x4 matrix from G • Replace G(5,5) with 4 • What do you get for G(13)? • What happens if you type G(12,1)

More Related