DOC PREVIEW
CMU CS 15780 - MATLAB Tutorial

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1MATLAB Tutorial1Based on IPLab@SUTMatlab TutorialContents¾ What is Matlab?¾ Matrices Numerical Arrays String Arrays¾ Elementary Math Logical Operators Math Functions¾ Importing and Exporting Data2Contents Continued¾ Graphics Fundamentals 2D plotting Subplots 3D plotting¾ Editing and Debugging M-files¾ Script and Function Files¾ Basic Parts of an M-file¾ Flow Control Statements¾ M-file Programming¾ Data Types3What is MATLAB? high-performance software  Computation Visualization Easy-to-use environment. high-level language  Data types Functions Control flow statements Input/output Graphics Object-oriented programming capabilities 5Calculations at the Command Line» a = 2;» b = 5;» a^bans = 32» x = 5/2*pi;» y = sin(x)y = 1» z = asin(y)z = 1.5708» a = 2;» b = 5;» a^bans = 32» x = 5/2*pi;» y = sin(x)y = 1» z = asin(y)z = 1.5708Results assigned to “ans” if name not specified() parentheses for function inputsSemicolon suppresses screen outputMATLAB as a calculatorAssigning VariablesA Note about Workspace:Numbers stored in double-precision floating point format» -5/(4.8+5.32)^2ans =-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)^2ans =-0.0488» (3+4i)*(3-4i)ans =25» cos(pi/2)ans =6.1230e-017» exp(acos(0.3))ans =3.54709General Functions whos: List current variables clear: Clear variables and functions from memory cd: Change current working directory ls: List files in directory102Getting helphelp command (>>help)lookfor command (>>lookfor)Printable Documents “Matlabroot\help\pdf_doc\”11Matrices Entering and Generating Matrices Subscripts Scalar Expansion Concatenation Deleting Rows and Columns Array Extraction Matrix and Array Multiplication12» a=[1 2;3 4]a = 1 23 4» b=[-2.8, sqrt(-7), (3+5+6)*3/4]b =-2.8000 0 + 2.6458i 10.5000» b(2,5) = 23b =-2.8000 0 + 2.6458i 10.5000 0 00 0 0 0 23.0000» a=[1 2;3 4]a = 1 23 4» b=[-2.8, sqrt(-7), (3+5+6)*3/4]b =-2.8000 0 + 2.6458i 10.5000» b(2,5) = 23b =-2.8000 0 + 2.6458i 10.5000 0 00 0 0 0 23.0000• Any MATLAB expression can be entered as a matrix element• Matrices must be rectangular. (Set undefined elements to zero)Entering Numeric ArraysRow separatorsemicolon (;)Column separatorspace / comma (,)Use square brackets [ ]13The Matrix in MATLAB4101 6 281.29 4 257.2 5 7 1 1100.54 5 5623 83 13 0 1012Rows (m) 345Columns(n)1 2 3 4 51 6 11 16 212 7 12 17 223 8 13 18 234 9 14 19 24510152025A =A (2,4)A (17)Rectangular Matrix:Scalar: 1-by-1 arrayVector: m-by-1 array1-by-n arrayMatrix: m-by-n array14» w=[1 2;3 4] + 5w =6 78 9» x = 1:5x =1 2 3 4 5» y = 2:-0.5:0y = 2.0000 1.5000 1.0000 0.5000 0 » z = rand(2,4)z =0.9501 0.6068 0.8913 0.45650.2311 0.4860 0.7621 0.0185» w=[1 2;3 4] + 5w =6 78 9» x = 1:5x =1 2 3 4 5» y = 2:-0.5:0y = 2.0000 1.5000 1.0000 0.5000 0 » z = rand(2,4)z =0.9501 0.6068 0.8913 0.45650.2311 0.4860 0.7621 0.0185Scalar expansionCreating sequences:colon operator (:)Utility functions for creating matrices.Entering Numeric Arrays15Numerical Array Concatenation» a=[1 2;3 4]a = 1 23 4» cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a]cat_a =1 2 2 43 4 6 83 6 4 89 12 12 165 10 6 1215 20 18 24» a=[1 2;3 4]a = 1 23 4» cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a]cat_a =1 2 2 43 4 6 83 6 4 89 12 12 165 10 6 1215 20 18 24Use [ ] to combine existing arrays as matrix “elements”Row separator:semicolon (;)Column separator:space / comma (,)Use square brackets [ ]Note:The resulting matrix must be rectangular 4*a163Deleting 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.17Array Subscripting / Indexing41016281.29 4 257.2 5 7 1 1100.54 5 5623 83 13 0 10123451 2 3 4 51 6 11 16 212 7 12 17 223 8 13 18 234 9 14 19 24510152025A =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)’18Matrix Multiplication» a = [1 2 3 4; 5 6 7 8];» b = ones(4,3);» c = a*bc =10 10 1026 26 26» a = [1 2 3 4; 5 6 7 8];» b = ones(4,3);» c = a*bc =10 10 1026 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.*bc =1 4 9 165 12 21 32» a = [1 2 3 4; 5 6 7 8];» b = [1:4; 1:4];» c = a.*bc =1 4 9 165 12 21 32c(2,4) = a(2,4)*b(2,4)Array Multiplication19Matrix 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• repmat: Replicate and tile a matrix20Matrix Manipulation Functions• det: Matrix determinant• inv: Matrix inverse • eig: Evaluate eigenvalues and eigenvectors• rank: Rank of matrix21Elementary MathLogical OperatorsMath FunctionsPolynomial and Interpolation254Logical Operations» Mass = [-2 10 NaN 30 -11 Inf 31];» each_pos = Mass>=0each_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>=0each_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 =


View Full Document

CMU CS 15780 - MATLAB Tutorial

Download MATLAB Tutorial
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB Tutorial and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB Tutorial 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?