270 likes | 289 Views
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
E N D
MATLAB Lecture Two Tuesday 5 July 2005
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 • g = 9.81 (no [ … ] needed) • X = [ ] creates a null matrix • Examples on page 51.
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.
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
Matrices and Vectors • Matrix Manipulation • array as index, A([1 3 4],:) • reshape(…) function • Transpose by .’ • Hermitian conjugate by ’ • Initialization
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
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
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
Matrices and Vectors • Matrix operations • + addition • - subtraction • * multiplication • / division • ^ exponentiation
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.
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.
Relational Operations • < Less than • <= Less than or equal • == equal • > greater than • >= greater than or equal • ~= not equal (different from C)
Logical Operations • & logical AND • | logical OR • ~ logical complement (NOT) • xor exclusive OR (a function)
Logical Functions • all, any, exist, isempty, isinf, isfinite, isnan, find
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)
Character Strings • Strings are quoted by single quotes • message = 'Leave me alone' • Strings are considered as 1xn matrix.
Eval Function • Eval interprets string as a MATLAB commend • eval('x = 5*sin(pi/3)')
Lookfor • Lookfor search for key word in the function documentation • eigenvalue example
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
Recording a Session with "diary" • diary filename • Save the session to filename • diary off • Turn of the diary recording
Plotting • plot(x, y) • plot(x1,y1,x2,y2) • xlabel, ylabel, title, print
Exercises • Lesson 5, exercise 3. • Write a function factorial to computer factorial n! for any integer n.
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.
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
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)