Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introdução ao M ATLAB M ATLAB Basico www.opencadd.com.br.

Similar presentations


Presentation on theme: "Introdução ao M ATLAB M ATLAB Basico www.opencadd.com.br."— Presentation transcript:

1 Introdução ao M ATLAB M ATLAB Basico

2 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Online Help The help command >> help The help window >> helpwin The lookfor command >> lookfor » help cd CD Change current working directory. CD directory-spec sets the current directory to the one specified. CD.. moves to the directory above the current one. CD, by itself, prints out the current directory. WD = CD returns the current directory as a string. Use the functional form of CD, such as CD('directory-spec'), when the directory specification is stored in a string. See also PWD. » help cd CD Change current working directory. CD directory-spec sets the current directory to the one specified. CD.. moves to the directory above the current one. CD, by itself, prints out the current directory. WD = CD returns the current directory as a string. Use the functional form of CD, such as CD('directory-spec'), when the directory specification is stored in a string. See also PWD.

3 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Calculations at the Command Line » -5/( )^2 ans = » (3+4i)*(3-4i) ans = 25 » cos(pi/2) ans = e-017 » exp(acos(0.3)) ans = » -5/( )^2 ans = » (3+4i)*(3-4i) ans = 25 » cos(pi/2) ans = e-017 » exp(acos(0.3)) ans = »a = 2; »b = 5; »a^b ans = 32 »x = 5/2*pi; »y = sin(x) y = 1 »z = asin(y) z = »a = 2; »b = 5; »a^b ans = 32 »x = 5/2*pi; »y = sin(x) y = 1 »z = asin(y) z = Results assigned to “ans” if name not specified »cmd_line () parentheses for function inputs Semicolon suppresses screen output MATLAB as a calculator Assigning Variables Numbers stored in double-precision floating point format

4 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Working with Files & Variables CD / PWD, LS / DIR - navigating directories WHAT - displays the files within a directory (grouped by type) ! - invoke operating system WHICH - identifies the object referenced by given name (function / variable) CLEAR - remove function / variable from memory WHOS - lists workspace variables and details (size, memory usage, data type) SIZE - returns the size of matrix Ref: Utility Commands

5 Introduction to M ATLAB Copyright  by The MathWorks, Inc. »workspace Command line variables saved in MATLAB workspace Workspace Browser

6 Introduction to M ATLAB Copyright  by The MathWorks, Inc. »openvar ans Array Editor double-click / Open For editing 2-D numeric arrays

7 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Working with Matrices MATLAB == MATrix LABoratory »load durer »image(X); »colormap(map) »load detail »image(X); »colormap(map)

8 Introduction to M ATLAB Copyright  by The MathWorks, Inc. The Matrix in MATLAB Rows (m) Columns (n) A = A (2,4) A (17) Rectangular Matrix: Scalar:1-by-1 array Vector:m-by-1 array 1-by-n array Matrix:m-by-n array Matrix elements can be EITHER numbers OR characters

9 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Any MATLAB expression can be entered as a matrix element Entering Numeric Arrays »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i Row separator: semicolon (;) Column separator: space / comma (,) »num_array1 Use square brackets [ ] Matrices must be rectangular. (Set undefined elements to zero)

10 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Entering Numeric Arrays - cont. »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = Scalar expansion Creating sequences: colon operator (:) Utility functions for creating matrices. (Ref: Utility Commands) »num_array2

11 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Numerical Array Concatenation - [ ] »num_cat »a=[1 2;3 4] a = »cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = »a=[1 2;3 4] a = »cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = Use [ ] to combine existing arrays as matrix “elements” Row separator: semicolon (;) Column separator: space / comma (,) Use square brackets [ ] The resulting matrix must be rectangular. 4*a

12 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Array Subscripting / Indexing A = A(3,1) A(3) A(1:5,5) A(:,5) A(21:25) A(4:5,2:3) A([9 14;10 15]) Use () parentheses to specify index colon operator (:) specifies range / ALL [ ] to create matrix of index subscripts ‘end’ specifies maximum index value A(1:end,end) A(:,end) A(21:end)’

13 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Create a 5x5 Pascal matrix ( "P_mat" ) Extract elements to create row vectors for the first 5 rows of Pascal’s Triangle (Look for patterns in the element locations in "P_mat" ) Combine the vectors into a single matrix with zeros above the diagonal "P_mat" Exercise: Creating Matrices Pascal’s Triangle row1 = 1 row2 = 1 1 row3 = row4 = row5 = row1 = 1 row2 = 1 1 row3 = row4 = row5 =

14 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution appears on following page.

15 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution: Creating Matrices (1) »P_mat = pascal(5)% Create Pascal's Matrix »row1 = P_mat(1)% Extract Rows »row2 = P_mat(2:4:6)..... »row5 = P_mat(5:4:21) »new_mat = row1 % Create New Matrix »new_mat(2,1:2) = row »new_mat(5,1:5) = row5 % NOTE: GENERALIZED FORM: % Nmax = length(P_mat); % rowN = P_mat(N:Nmax-1:(N-1)*Nmax+1) % new_mat(N,1:N) = rowN »P_mat = pascal(5)% Create Pascal's Matrix »row1 = P_mat(1)% Extract Rows »row2 = P_mat(2:4:6)..... »row5 = P_mat(5:4:21) »new_mat = row1 % Create New Matrix »new_mat(2,1:2) = row »new_mat(5,1:5) = row5 % NOTE: GENERALIZED FORM: % Nmax = length(P_mat); % rowN = P_mat(N:Nmax-1:(N-1)*Nmax+1) % new_mat(N,1:N) = rowN »create_mat

16 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution: Creating Matrices (2) »P_mat = pascal(5)% Create Pascal's Matrix % Rearrange Matrix - Pascal Triangle in upper triangle % Flip Matrix Horizontally (LEFT-RIGHT): % - Could also use FLIPLR(): »temp = P_mat(1:5, 5:-1:1) % Extract diagonals (rows of Pascal’s Triangle) »row1 = diag(temp,4)' »row2 = diag(temp,3)' »row3 = diag(temp,2)' »row4 = diag(temp,1)' »row5 = diag(temp)' % Create New Matrix as before »P_mat = pascal(5)% Create Pascal's Matrix % Rearrange Matrix - Pascal Triangle in upper triangle % Flip Matrix Horizontally (LEFT-RIGHT): % - Could also use FLIPLR(): »temp = P_mat(1:5, 5:-1:1) % Extract diagonals (rows of Pascal’s Triangle) »row1 = diag(temp,4)' »row2 = diag(temp,3)' »row3 = diag(temp,2)' »row4 = diag(temp,1)' »row5 = diag(temp)' % Create New Matrix as before »create_mat

17 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution: Creating Matrices (3) »P_mat = pascal(5)% Create Pascal's Matrix % Rearrange Matrix - Pascal Triangle in lower triangle % Flip Matrix Vertically (UP-DOWN): % - Could also use FLIPUD(): »temp1 = P_mat(5:-1:1, 1:5) % Extract lower triangular matrix: »temp2 = tril(temp1) % Create New Matrix by sorting columns of "temp2": »new_mat = sort(temp2) »P_mat = pascal(5)% Create Pascal's Matrix % Rearrange Matrix - Pascal Triangle in lower triangle % Flip Matrix Vertically (UP-DOWN): % - Could also use FLIPUD(): »temp1 = P_mat(5:-1:1, 1:5) % Extract lower triangular matrix: »temp2 = tril(temp1) % Create New Matrix by sorting columns of "temp2": »new_mat = sort(temp2) »create_mat

18 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Matrices & Linear Algebra »help ops »help matfun (In order of precedence)

19 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Matrix Multiplication Inner dimensions must be equal Dimension of resulting matrix = outermost dimensions of multiplied matrices Resulting elements = dot product of the rows of the 1st matrix with the columns of the 2nd matrix »a = [ ; ]; »b = ones(4,3); »c = a*b c = »a = [ ; ]; »b = ones(4,3); »c = a*b c = [2x4] [4x3] [2x4]*[4x3] [2x3] a(2nd row).b(3rd column) »mat_mult

20 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solving Simultaneous Equations using “Left Division” If [A] is square matrix (m = n): For overdetermined system (m>n): using Least squares regression “curve fit” of data Warning if rank deficient (dependent columns) - solution not unique For undetermined system (m

21 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solve this set of simultaneous equations: Example: Solving Equations »A = [-1 1 2; ;-1 3 4]; »b = [2;6;4]; »x = inv(A)*b x = »x = A\b x = »A = [-1 1 2; ;-1 3 4]; »b = [2;6;4]; »x = inv(A)*b x = »x = A\b x = x 1 + x 2 + 2x 3 = 2 3x 1 - x 2 + x 3 = 6 -x 1 + 3x 2 + 4x 3 = 4 »solve_examp

22 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Array Multiplication Matrices must have the same dimensions Dimensions of resulting matrix = dimensions of multiplied matrices Resulting elements = product of corresponding elements from the original matrices Same rules apply for other array operations »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = »array_mult c(2,4) = a(2,4)*b(2,4)

23 Introduction to M ATLAB Copyright  by The MathWorks, Inc. In most languages - use loops: In MATLAB - use Array Operations: Example: Array Operations » tic; for I = 1:1000 Density(I) = Mass(I)/(Length(I)*Width(I)*Height(I)); end; toc elapsed_time = » tic; for I = 1:1000 Density(I) = Mass(I)/(Length(I)*Width(I)*Height(I)); end; toc elapsed_time = »array_examp » tic; Density = Mass./(Length.*Width.*Height); toc elapsed_time = 0 » tic; Density = Mass./(Length.*Width.*Height); toc elapsed_time = 0 Use TIC and TOC to measure elapsed time Vectorized code is much faster than loops

24 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Boolean Operations » Mass = [-2 10 NaN Inf 31]; » all_pos = all(Mass>=0) all_pos = 0 » each_pos = Mass>=0 each_pos = » pos_fin = (Mass>=0)&(isfinite(Mass)) pos_fin = » Mass = [-2 10 NaN Inf 31]; » all_pos = all(Mass>=0) all_pos = 0 » each_pos = Mass>=0 each_pos = » pos_fin = (Mass>=0)&(isfinite(Mass)) pos_fin = = TRUE 0 = FALSE »bool_ops

25 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Exercise: Matrix Operations You are given measured data of current vs. applied DC voltage for a “black box” circuit Determine the linear resistance of the circuit

26 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution appears on following page.

27 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution: Matrix Operations »mat_ops(uses: >>mat_ops_setup.m & >>mat_ops.mat) % Assign loaded data: »A = voltage; b = current; % Using left division »x = A\b % Using Pseudoinverse »pseudo_inv_A = inv(A'*A)*A’; »x = pseudo_inv_A*b % Repeat using linear data »indices = find(voltage<8); »A = voltage(indices); b = current(indices);..... % Assign loaded data: »A = voltage; b = current; % Using left division »x = A\b % Using Pseudoinverse »pseudo_inv_A = inv(A'*A)*A’; »x = pseudo_inv_A*b % Repeat using linear data »indices = find(voltage<8); »A = voltage(indices); b = current(indices);..... Initial Fit Improved fit

28 Introduction to M ATLAB Copyright  by The MathWorks, Inc. String Arrays Created using single quote delimiter (') Each character is a separate matrix element (16 bits of memory per character) Indexing same as for numeric arrays »str = 'Hi there,' str = Hi there, »str2 = 'Isn''t MATLAB great?' str2 = Isn't MATLAB great? »str = 'Hi there,' str = Hi there, »str2 = 'Isn''t MATLAB great?' str2 = Isn't MATLAB great? »string_array 1x9 vector str = Hithere,

29 Introduction to M ATLAB Copyright  by The MathWorks, Inc. »str ='Hi there,'; »str1='Everyone!'; »new_str=[str, ' ', str1] new_str = Hi there, Everyone! »str2 = 'Isn''t MATLAB great?'; »new_str2=[new_str; str2] new_str2 = Hi there, Everyone! Isn't MATLAB great? »str ='Hi there,'; »str1='Everyone!'; »new_str=[str, ' ', str1] new_str = Hi there, Everyone! »str2 = 'Isn''t MATLAB great?'; »new_str2=[new_str; str2] new_str2 = Hi there, Everyone! Isn't MATLAB great? 1x19 vector 1x9 vectors String Array Concatenation »string_cat Using [ ] operator: Each row must be same length Row separator: semicolon (;) Column separator: space / comma (,) For strings of different length: STRVCAT STR2MAT »new_str3 = strvcat(str, str2) new_str3 = Hi there, Isn't MATLAB great? »new_str3 = strvcat(str, str2) new_str3 = Hi there, Isn't MATLAB great? 2x19 matrix (zero padded) 1x19 vectors

30 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Working with String Arrays String Comparisons STRCMP - compare whole strings STRNCMP - compare first ‘N’ characters FINDSTR - finds substring within a larger string Converting between numeric & string arrays: NUM2STR - convert from numeric to string array STR2NUM - convert from string to numeric array

31 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Exercise: String Arrays Create a 5x5 magic matrix ("M") Use "M" to create a string array which will display as follows: »disp(string_array) 5x5 Magic Matrix: 'A' 'B' 'C' 'D' 'E' »disp(string_array) 5x5 Magic Matrix: 'A' 'B' 'C' 'D' 'E' Tab character New line character HINT: Create rows separately & concatenate

32 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution appears on following page.

33 Introduction to M ATLAB Copyright  by The MathWorks, Inc. Solution: String Arrays »M = magic(5) »M_string = num2str(M) »heading = ['5x5 Magic Matrix:', char(10)] »row0 = [char(9), '''A'' ''B'' ''C'' ''D'' ''E'''] »row1 = ['1', char(9), M_string(1,:)] »row2 = ['2', char(9), M_string(2,:)] »row3 = ['3', char(9), M_string(3,:)] »row4 = ['4', char(9), M_string(4,:)] »row5 = ['5', char(9), M_string(5,:)] »string_array = strvcat(heading,row0,row1,row2,row3,row4,row5) »M = magic(5) »M_string = num2str(M) »heading = ['5x5 Magic Matrix:', char(10)] »row0 = [char(9), '''A'' ''B'' ''C'' ''D'' ''E'''] »row1 = ['1', char(9), M_string(1,:)] »row2 = ['2', char(9), M_string(2,:)] »row3 = ['3', char(9), M_string(3,:)] »row4 = ['4', char(9), M_string(4,:)] »row5 = ['5', char(9), M_string(5,:)] »string_array = strvcat(heading,row0,row1,row2,row3,row4,row5) »string_soln

34 Introduction to M ATLAB Copyright  by The MathWorks, Inc Multidimensional Arrays » A = pascal(4); » A(:,:,2) = magic(4) A(:,:,1) = A(:,:,2) = » A(:,:,9) = diag(ones(1,4)); » A = pascal(4); » A(:,:,2) = magic(4) A(:,:,1) = A(:,:,2) = » A(:,:,9) = diag(ones(1,4)); Page N Page 1 »mult_dim


Download ppt "Introdução ao M ATLAB M ATLAB Basico www.opencadd.com.br."

Similar presentations


Ads by Google