Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.

Similar presentations


Presentation on theme: "1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language."— Presentation transcript:

1 1 M ATLAB Short Course

2 History of Calculator 2

3 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language

4 4 Matlab Basics Basic data type: Matrix A matrix is a two- dimensional array of numbers rows and columns. A vector is a one- dimensional matrix. It may have either one row or one column.

5 5 Toolboxes Collections of functions to solve problems of several applications. Control Toolbox Neural Network Toolbox Fuzzy Logic Toolbox Genetic Algorithms Toolbox Communication Toolbox …………..

6 6 Main Working Windows After the “>>” symbol, you can type the commands

7 7 Display Windows Graphic (Figure) Window Displays plots and graphs Creates response to graphics commands M-file editor/debugger window Create and edit scripts of commands called M-files

8 8 Getting Help To get help: MATLAB main menu -> Help -> MATLAB Help

9 9 Getting Help Type one of the following commands in the command window: help – lists all the help topics help topic – provides help for the specified topic help command – provides help for the specified command helpwin – opens a separate help window for navigation Lookfor keyword – search all M-files for keyword Online resource

10 10 Variables Variable names: Must start with a letter. May contain only letters, digits, and the underscore “_”. MATLAB is case sensitive, for example one & ONE are different variables. MATLAB only recognizes the first 31 characters in a variable name. Assignment statement: Variable = number; >> t=1234 Variable = expression; >>t=a+b

11 11 Variables Special variables: ans: default variable name for the result. pi: π = 3.1415926 …… eps: ε= 2.2204e-016, smallest value by which two numbers can differ inf: ∞, infinity NAN or nan: not-a-number Commands involving variables: who: lists the names of the defined variables whos: lists the names and sizes of defined variables clear: clears all variables clear name: clears the variable name clc: clears the command window clf: clears the current figure and the graph window

12 12 Calculations at the Command Line »a = 2; »b = 5; »a^b ans = 32 »x = 5/2*pi; »y = sin(x) y = 1 »z = asin(y) z = 1.5708 »a = 2; »b = 5; »a^b ans = 32 »x = 5/2*pi; »y = sin(x) y = 1 »z = asin(y) z = 1.5708 Results assigned to “ans” if name not specified () parentheses for function inputs Semicolon suppresses screen output MATLAB as a calculator Assigning Variables A Note about Workspace: Numbers stored in double-precision floating point format » -5/(4.8+5.32)^2 ans = -0.0488 » (3+4i)*(3-4i) ans = 25 » cos(pi/2) ans = 6.1230e-017 » exp(acos(0.3)) ans = 3.5470 » -5/(4.8+5.32)^2 ans = -0.0488 » (3+4i)*(3-4i) ans = 25 » cos(pi/2) ans = 6.1230e-017 » exp(acos(0.3)) ans = 3.5470 9

13 13 A = [ 100 0 99 ; 7 -1 -25 ] The semi colon separates the rows of a matrix. To print the matrix, type the name. A= 100099 7-1-25 Creating Matrices

14 14 To print the element in the 2 nd row, 3 rd column, type: A(2, 3 ) Matlab prints ans = -25 Accessing Matrix Elements By entering x = A(1,1) + A( 2,3) Matlab returns x = 75

15 15 To change an entry, enter the new value: A(2,3) = -50 Matlab prints the new matrix A A = 100 0 99 7 -1 -50 Changing Matrix Elements

16 16 Numerical Array Concatenation »a=[1 2;3 4] a = 1 2 3 4 »cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24 »a=[1 2;3 4] a = 1 2 3 4 »cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = 1 2 2 4 3 4 6 8 3 6 4 8 9 12 12 16 5 10 6 12 15 20 18 24 Use [ ] to combine existing arrays as matrix “elements” Row separator: semicolon (;) Column separator: space / comma (,) Use square brackets [ ] 4*a

17 17 The : operator x = 1 : 7 or x = [ 1 : 7 ] creates the same vector as the command x = [ 1 2 3 4 5 6 7] y = 0 : 3 : 12 is the same as y = [ 0 3 6 9 12 ] y = 0 : 3 : 11 is the same as y = [ 0 3 6 9 ] z = 15 : -4 : 3 is the same as z = [ 15 11 7 3 ] w = 0 : 0.01 : 2 is the same as w = [ 0 0.01 0.02... 1.99 2.00 ]

18 18 Deleting Rows and Columns »A=[1 5 9;4 3 2.5; 0.1 10 3i+1] A = 1.0000 5.0000 9.0000 4.0000 3.0000 2.5000 0.1000 10.0000 1.0000+3.0000i »A(:,2)=[] A = 1.0000 9.0000 4.0000 2.5000 0.1000 1.0000 + 3.0000i »A(2,2)=[] ??? Indexed empty matrix assignment is not allowed. »A=[1 5 9;4 3 2.5; 0.1 10 3i+1] A = 1.0000 5.0000 9.0000 4.0000 3.0000 2.5000 0.1000 10.0000 1.0000+3.0000i »A(:,2)=[] A = 1.0000 9.0000 4.0000 2.5000 0.1000 1.0000 + 3.0000i »A(2,2)=[] ??? Indexed empty matrix assignment is not allowed.

19 19 Array Subscripting / Indexing 410162 81.29425 7.257111 00.54556 238313010 1234512345 1 2 3 4 5 16111621 27121722 38131823 49141924 510152025 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]) A(1:end,end) A(:,end) A(21:end)’

20 20 Special Matrices and Commands Z = zeros( 4, 5 )Z = 0 0 0 0 0 K = ones( 4, 5 ) is a matrix of 1’s Id = eye( 4, 5 ) Id = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0

21 21 Special Matrices and Commands The transpose operator is ’ transZ = Z’ Z = 1 2 3 4 5 6 transZ = 1 4 2 5 3 6

22 22 >> A = [ 100 0 99 ; 7 -1 -25 ] A = 100 0 99 7 -1 -25 >> size(A) a vector of rows and columns in A ans = 2 3 >> length(A) ans = 3 Special Matrices and Commands

23 23 >> A = [ 100 0 99 ; 7 -1 -25 ] A = 100 0 99 7 -1 -25 >> v=diag(A) a vector with the diagonal elements of A v = 100 >> B=diag(v) a matrix with v on the diagonal and 0’s off the diagonal B = 100 0 0 -1 Special Matrices and Commands The diag command shows that Matlab commands can depend on the nature of the arguments.

24 24 Matrix Manipulation Functions zeros : Create an array of all zeros ones : Create an array of all ones eye: Identity Matrix rand: Uniformly distributed random numbers diag: Diagonal matrices and diagonal of a matrix size: Return array dimensions fliplr: Flip matrices left-right flipud: Flip matrices up and down repmat: Replicate and tile a matrix

25 25 Matrix Manipulation Functions transpose ( ’ ): Transpose matrix rot90: rotate matrix 90 tril: Lower triangular part of a matrix triu: Upper triangular part of a matrix cross: Vector cross product dot: Vector dot product det: Matrix determinant inv: Matrix inverse eig: Evaluate eigenvalues and eigenvectors rank: Rank of matrix

26 26 Arithmetic Operations A, B are matrices, x is a scalar x*A multiplies each entry of A by x x + A adds x to each entry of A same as x*ones(size(A)) + A x – A same as x*ones(size(A)) – A A + B standard matrix addition A – B standard matrix subtraction

27 27 Arithmetic Operations (cont.) A*B Standard matrix multiplication cols A = rows B A.*B element-wise multiplication size( A) = size(B) A.\B divide elements of B by those in A A./B divide elements of A by those in B

28 28 Matrix Multiplication »a = [1 2 3 4; 5 6 7 8]; »b = ones(4,3); »c = a*b c = 10 10 10 26 26 26 »a = [1 2 3 4; 5 6 7 8]; »b = ones(4,3); »c = a*b c = 10 10 10 26 26 26 [2x4] [4x3] [2x4]*[4x3] [2x3] a(2nd row).b(3rd column) »a = [1 2 3 4; 5 6 7 8]; »b = [1:4; 1:4]; »c = a.*b c = 1 4 9 16 5 12 21 32 »a = [1 2 3 4; 5 6 7 8]; »b = [1:4; 1:4]; »c = a.*b c = 1 4 9 16 5 12 21 32 c(2,4) = a(2,4)*b(2,4) Array Multiplication

29 29 Linear Equations Example: a system of 3 linear equations with 3 unknowns (x 1, x 2, x 3 ) 3 x 1 + 2x 2 - x 3 = 10 - x 1 + 3x 2 + 2x 3 = 5 x 1 - 2x 2 - x 3 = -1 Let: Then, the system can be described as: Ax = b

30 30 Complex Numbers Matlab works with complex numbers, so sqrt( -3 ) is a valid command. Lowercase i is reserved for the special value sqrt(-1)

31 31 Special Values pi = 3.14159.... Inf = infinity (divide by 0) M = [ pi sqrt(-3); Inf 0] M = 3.1416 0 + 1.7321i Inf 0

32 32 Linear Equations Solution by Matrix Inverse: Ax = b A -1 Ax = A -1 b Ax = b MATLAB: >> A = [3 2 -1; -1 3 2; 1 -1 -1]; >> b = [10;5;-1]; >> x = inv(A)*b x = -2.0000 5.0000 -6.0000 Solution by Matrix Division: Ax = b Can be solved by left division b\A MATLAB: >> A = [3 2 -1; -1 3 2; 1 -1 - 1]; >> b = [10;5;-1]; >> x =A \ b x = -2.0000 5.0000 -6.0000

33 33 Example

34 34 Example

35 35 Example P=0, Vz=0, My=0, Vy=-1000 lb, Mz=100,000 lb.in

36 36 Elementary Math Function abs, sign: Absolute value and Signum Function sin, cos, asin, acos…: Triangular functions exp, log, log10: Exponential, Natural and Common (base 10) logarithm ceil, floor: Round toward infinities fix: Round toward zero

37 37 Elementary Math Function round: Round to the nearest integer gcd: Greatest common devisor lcm: Least common multiple sqrt: Square root function real, imag: Real and Image part of complex rem: Remainder after division

38 38 Elementary Math Function max, min: Maximum and Minimum of arrays mean, median: Average and Median of arrays std, var: Standard deviation and variance sort: Sort elements in ascending order sum, prod: Summation & Product of Elements trapz: Trapezoidal numerical integration cumsum, cumprod: Cumulative sum, product diff, gradient: Differences and Numerical Gradient

39 39 Polynomials and Interpolation Polynomials Representing Roots ( >> roots ) Evaluation ( >> polyval ) Derivatives ( >> polyder ) Curve Fitting ( >> polyfit ) Partial Fraction Expansion ( residue ) Interpolation One-Dimensional ( interp1 ) Two-Dimensional ( interp2 )

40 40 polysam=[1 0 0 8]; roots(polysam) ans = -2.0000 1.0000 + 1.7321i 1.0000 - 1.7321i Polyval(polysam,[0 1 2.5 4 6.5]) ans = 8.0000 9.0000 23.6250 72.0000 282.6250 polyder(polysam) ans = 3 0 0 [r p k]=residue(polysam,[1 2 1]) r = 3 7 p = -1 -1 k = 1 -2 polysam=[1 0 0 8]; roots(polysam) ans = -2.0000 1.0000 + 1.7321i 1.0000 - 1.7321i Polyval(polysam,[0 1 2.5 4 6.5]) ans = 8.0000 9.0000 23.6250 72.0000 282.6250 polyder(polysam) ans = 3 0 0 [r p k]=residue(polysam,[1 2 1]) r = 3 7 p = -1 -1 k = 1 -2 Example

41 41 x = [0: 0.1: 2.5]; y = erf(x); p = polyfit(x,y,6) p = 0.0084 -0.0983 0.4217 -0.7435 0.1471 1.1064 0.0004 x = [0: 0.1: 2.5]; y = erf(x); p = polyfit(x,y,6) p = 0.0084 -0.0983 0.4217 -0.7435 0.1471 1.1064 0.0004 Example interp1(x,y,[0.45 0.95 2.2 3.0]) ans = 0.4744 0.8198 0.9981 NaN interp1(x,y,[0.45 0.95 2.2 3.0]) ans = 0.4744 0.8198 0.9981 NaN

42 42 Logical Operations » Mass = [-2 10 NaN 30 -11 Inf 31]; » each_pos = Mass>=0 each_pos = 0 1 0 1 0 1 1 » all_pos = all(Mass>=0) all_pos = 0 » all_pos = any(Mass>=0) all_pos = 1 » pos_fin = (Mass>=0)&(isfinite(Mass)) pos_fin = 0 1 0 1 0 0 1 » Mass = [-2 10 NaN 30 -11 Inf 31]; » each_pos = Mass>=0 each_pos = 0 1 0 1 0 1 1 » all_pos = all(Mass>=0) all_pos = 0 » all_pos = any(Mass>=0) all_pos = 1 » pos_fin = (Mass>=0)&(isfinite(Mass)) pos_fin = 0 1 0 1 0 0 1 = = equal to > greater than < less than >= Greater or equal <= less or equal ~ not & and | or isfinite(), etc.... all(), any() find Note: 1 = TRUE 0 = FALSE


Download ppt "1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language."

Similar presentations


Ads by Google