Download presentation
Presentation is loading. Please wait.
Published byEgbert Berry Modified over 9 years ago
1
MATLAB Lecture Two Tuesday 5 July 2005
2
Chapter 3
3
Matrices and Vectors l Input matrix A = [ 1 2 5; 3 9 0] l Use semicolon ";" to suppress output l Use … for line continuation
4
Matrices and Vectors l Vector is a special case of matrix l Scalar is considered 1x1 matrix g = 9.81 (no [ … ] needed) l X = [ ] creates a null matrix l Examples on page 51.
5
Matrices and Vectors l 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 l Dimension B(2,3) = 5 matrix B created large enough to have the (2,3) entry, I.e., B is 2 x 3. l The dimension is increased dynamically
7
Matrices and Vectors l Matrix Manipulation array as index, A([1 3 4],:) l reshape(…) function l Transpose by.’ l Hermitian conjugate by ’ l Initialization
8
Matrices and Vectors l Appending a row or column A = [A; u]; A = [ A u'] l Deleting a row or column A(2, :) = [ ], delete 2nd row A(:,1) = [ ], delete 1st column
9
Matrices and Vectors l Utility matrices eye(m,n), zeros(m,n), ones(m,n), rand(m,n), diag(A) l rot90, fliplr, flipud, tril, triu l Examples on page 56
10
Matrices and Vectors l Creating vectors v= initialV : increment : finalV e.g., a = 0:10:100 l linspace(a,b,n) and logspace(a,b,b) functions
11
Matrices and Vectors l Matrix operations + addition - subtraction * multiplication / division ^ exponentiation
12
Matrices and Vectors l 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 l Element-by-element operation.* element-wise multiply./ element-wise right division.\ element-wise left division.^ element-wise exponentiation.' nonconjugated transpose l Examples on page 60.
14
Relational Operations l <Less than l <=Less than or equal l ==equal l >greater than l >=greater than or equal l ~=not equal ( different from C)
15
Logical Operations l &logical AND l |logical OR l ~logical complement (NOT) l xorexclusive OR (a function)
16
Logical Functions l all, any, exist, isempty, isinf, isfinite, isnan, find
17
Math Functions l sin, asin, cos, acos, tan, atan, atan2, sec, cot, sinh, exp, log, log10, sqrt, abs, angle, conj, real, imag, fix, floor, ceiling, sign, etc. l Matrix functions expm(A), logm(A), sqrtm(A)
18
Character Strings l Strings are quoted by single quotes message = 'Leave me alone' l Strings are considered as 1xn matrix.
19
Eval Function l Eval interprets string as a MATLAB commend eval('x = 5*sin(pi/3)')
20
Lookfor l Lookfor search for key word in the function documentation l eigenvalue example
21
Save and Loading Data l save save entire workspace in the file matlab.mat l load load the saved file l save file.mat x y z save the values x, y, z in file.mat
22
Recording a Session with "diary" l diary filename Save the session to filename l diary off Turn of the diary recording
23
Plotting l plot(x, y) l plot(x1,y1,x2,y2) l xlabel, ylabel, title, print
24
Exercises l Lesson 5, exercise 3. Write a function factorial to computer factorial n! for any integer n.
25
Exercises l Lesson 5, exercise 5. Write a function to compute the sum of a geometric series 1 + r + r 2 + r 3 + … + r n for a given r and n.
26
Exercises on page 74, problem 1, 4, and 5 l Enter matrices A = [2 6; 3 9] B = [1 2; 3 4] C = [-5 5; 5 3] l Create a big matrix that has A, B, C on the diagonal
27
Exercises on page 74, problem 1, 4, and 5 l Delete the last row and last column l Extract the first 4x4 matrix from G l Replace G(5,5) with 4 l What do you get for G(13)? l What happens if you type G(12,1)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.