1 / 52

Intro to MATLAB

Intro to MATLAB. reading . Chapter 8 DO LAB 8!. what is a matrix?. Collections of numbers like this - whose rows and columns indicate properties and relationships Example: "3D" means [ width, height, depth ]. 2 2 3 9 7 4 60 20 18 400 20 20.

linda-pratt
Download Presentation

Intro to MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to MATLAB

  2. reading • Chapter 8 • DO LAB 8!

  3. what is a matrix? • Collections of numbers like this - whose rows and columns indicate properties and relationships • Example: "3D" means [ width, height, depth ] 2 2 3 9 7 4 60 20 18 400 20 20

  4. imagine 4 such objects e.g. a bug: [ width, height, depth ] a rock: [ width, height, depth ] a person: [ width, height, depth ] a tree: [ width, height, depth ] object 2 2 3 9 7 4 60 20 18 400 20 20 = property (or variable)

  5. Matrix Dimensions • Also called SIZE • Rows x Columns • e.g "3 by 5" means 3 rows, 5 columns 10 34 67 17 9 5 66 24 98 10 12 4 56 9 12

  6. A Matrix A 4x3 MATRIX (Rows x Columns) 4 ROWS 3 COLUMNS ROW 1 = [ 2 2 3 ] COLUMN 2 = [ 2 7 20 20 ] ITEM (4,1) is at R4, C1 = 400 2 2 3 9 7 4 60 20 18 400 20 20

  7. "Vectors" a VECTOR is a (1 x N) or an (N x 1) matrix a (1 x 12) ROW vector: x = [ 1 2 3 4 5 6 7 8 9 10 11 12] usually refers to one object an (8 x 1 ) COLUMN vector: y = [ 1 refers to one property 2 3 4 5 6 7 8 ]

  8. Square matrix : m x m A = [ 0 1 2 3 4 5 6 7 8 ] is a 3 x 3 square matrix The DIAGONAL is: 0 4 8 note: an M x M matrix with 1's in the diagonal and 0's everywhere else has special properties, and is called the IDENTITY matrix I = [ 1 0 0 0 1 0 0 0 1 ]

  9. variables type here files and folders command history

  10. Use UB’s VCL • Virtual Computing Library http://www.buffalo.edu/ubit/service-guides/software/my-virtual-computing-lab.html • Via Microsoft’s Remote Desktop (on both PCs and Mac) • Login using • Off campus: CISCO ANYVConnect (username & password) • Then… MS Remote Desktop (ad\username & password) • Step-by-step in Lab 8

  11. some useful commands • variable (scalar) definition: x = 10 • a scalar is a single-valued variable, but MATLAB regards it as a 1x1 matrix • no semicolons needed, although a semicolon will stop echo • comments start with %, used mostly in scripts • assignments work, as in C++ t = 0 t = t + 1

  12. Variables Like most other programming languages, the MATLAB language provides mathematical expressions, but unlike most programming languages, these expressions involve entire matrices. Always. MATLAB does not require any type declarations. When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. For example, num_students = 25 creates a 1-by-1 matrix named num_students and stores the value 25 in its single element. To view the matrix assigned to any variable, simply enter the variable name. Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. Although variable names can be of any length, MATLAB uses only the first N characters of the name, (where N is the number returned by the function namelengthmax), and ignores the rest. Hence, it is important to make each variable name unique in the first N characters to enable MATLAB to distinguish variables.

  13. Numbers MATLAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Imaginary numbers use either i or j as a suffix. Some examples of legal numbers are 3 -99 0.0001 9.6397238 1.60210e-20 6.02252e23 1i -3.14159j 3e5i

  14. To define a matrix, you can treat it just the way it looks A = [ 1 2 3 4 5 6 7 8 9]

  15. MATLAB is “INTERPRETED” • Meaning, there are two convenient ways to enter commands • In the command window, one at a time • In a script ( .m file), which are run like programs • Script files must be “saved” and “opened” via icon selections • The MATLAB SAVE and LOAD commands are not for saving and loading scripts.

  16. There are three steps to running MATLAB • Step one: IF YOU ARE OFF CAMPUS, run the Cisco AnyConnect vpn client • vpn = virtual private network • Looks to UB, like you are on campus

  17. 1. PC - run the vpn client host: ubvpn.buffalo.edu This means that you can connect to UB secure systems over the Internet 2. 3.

  18. Mac - Run the Cisco UB VPNhost: ubvpn.buffalo.edu In the dock

  19. Step 2 – run the Microsoft Remote Desktop

  20. For Windows The Microsoft Remote Desktop App: Download this file and run it: http://wings.buffalo.edu/computing/ubcms/ubvcl.rdpLog in using your UBIT name and password. AD\username Then find MATLAB on the remote desktop start button. More help: http://www.buffalo.edu/ubit/service-guides/software/my-virtual-computing-lab.html note the AD\

  21. In the App store

  22. Step 3 – save and submit your work See Lab8PC and Lab8Mac for a full explanation

  23. the amazing MATLAB Help window

  24. The MATLAB videos

  25. Enter a row vector degrees = [ 0 10 20 30 40 50 60 ] or commas: degrees = [ 0, 10, 20, 30, 40, 50, 60 ] or degrees = [0 : 60 ] 0,1,2,3,4,5,6,7,8 … 60 0 to 60 degrees = [ 0 : 20 : 60 ] 0, 20, 40, 60 0 to 60 by 20s

  26. Many Rows: • Rows may be separated by semi-colons rather than a new line B = [ 1 2 3; 4 5 6; 7 8 9] • You can also define a matrix like that: D = [1:5; 6:10; 11:2:20 ] last row - 11 to 20, by twos

  27. Enter a column vector x = [ 0; 10; 20; 30; 40; 50; 60 ] x = [ 0 10 20 30 40 50 60 ]

  28. matrices get very comforable with the RxC designation, and what that means.... 3 x 4 means a matrix with 3 rows and 4 columns (3,4) means the single elemant at R=3, C=4

  29. plot a sine curve >> q = [0: 10: 180] q = Columns 1 through 14 0 10 20 30 40 50 60 70 80 90 100 110 120 130 Columns 15 through 19 140 150 160 170 180 >> x = q*pi/180 x = Columns 1 through 8 0 0.1745 0.3491 0.5236 0.6981 0.8727 1.0472 1.2217 Columns 9 through 16 1.3963 1.5708 1.7453 1.9199 2.0944 2.2689 2.4435 2.6180 Columns 17 through 19 2.7925 2.9671 3.1416

  30. plot a sine curve

  31. plot a sine curve >> y = sin(x) y = Columns 1 through 8 0 0.1736 0.3420 0.5000 0.6428 0.7660 0.8660 0.9397 Columns 9 through 16 0.9848 1.0000 0.9848 0.9397 0.8660 0.7660 0.6428 0.5000 Columns 17 through 19 0.3420 0.1736 0.0000

  32. >> plot( x, y )

  33. more than 1 plot degrees = [1: 4: 360] radians = degrees * pi/180 y = sin(radians) z = cos(radians) figure( 1 ) plot(degrees, y) figure( 2 ) plot (degrees, z) figure( 3 ) plot(degrees, y, degrees, z)

  34. ones • E = ones(2,3) % ones(m,n) gives an m*n matrix of 1's >> E = ones(2,3) E = 1 1 1 1 1 1

  35. diag • G = [-3 4 2] % define a row vector • H = diag(G) % use G as the diagonal >> G = [-3 4 2] G = -3 4 2 >> H = diag(G) H = -3 0 0 0 4 0 0 0 2 >>

  36. rand • I = rand(1,3) % rand(m,n) gives m*n matrix of uniformly distributed random numbers from 0 - .9999 >> I = rand(1,3) I = 0.8147 0.9058 0.1270

  37. >> A = [rand(1,3); rand(1,3)*10; rand(1,3)*100 ] A = 0.8235 0.6948 0.3171 9.5022 2.3445 4.3874 38.1558 76.5517 79.5200

  38. Create a 3 by 3 matrix with each element a random value between 0 and 9 • rand( 1 ) create a 1 by 1 matrix with random values(0, 1) • rand(3, 3)create a 3 by 3 matrix with random values(0, 1)

  39. Create a 3 by 3 matrix with each element a random value between 0 and 9 • rand( 1 ) create a 1 by 1 matrix with random values(0, 1) • rand(3, 3) create a 3 by 3 matrix with random values(0, 1) • rand(3,3)*10 create a 3 by 3 matrix with random values(0, 10)

  40. Square creations z = rand( 10 ) % gives a 10 x 10 matrix q = ones( 10 ) % gives a 10 x 10 matrix H = diag( [ 1 2 3 ] ) % gives a 3 x 3 matrix E = eye( 4 ) % gives a 4 x 4 matrix

  41. transposing - swap rows and columns A = [ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ] >> B = transpose(A) B = 16 5 9 4 2 11 7 14 3 10 6 15 13 8 12 1

  42. transposing a row makes it a column >> x = [0:6] x = 0 1 2 3 4 5 6 >> y = transpose(x) y = 0 1 2 3 4 5 6 >>

  43. transposing a column makes it a row y = 0 1 2 3 4 5 6 >> z = transpose(y) z = 0 1 2 3 4 5 6 >>

  44. transposing a 2 x 5 matrix >> A = [ 5 7 9 0 12 16 3 44 1 8 ] A = 5 7 9 0 12 16 3 44 1 8 >> B = transpose(A) B = 5 16 7 3 9 44 0 1 12 8 >> 2 x 5 5 x 2

  45. Individual Matrix elements • Let's start with the simple case of a vector and a single subscript. The vector is  v = [16 5 9 4 2 11 7 14] • The subscript can be a single value.  v(3)     % Extract the third element  ans =        9 • Colons work: v(1:4) ans = 16 5 9 4

  46. Now consider indexing into a matrix.  A = [ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 ] • indexing in matrices is done using two subscripts - one for the rows and one for the columns. A(2,4)   % Extract the element in row 2, column 4ans  = 8

  47. who and whos • who shows your variables • whos lists your variables and their characteristics clc and clear • clc clears the command window • clear erases all variables

  48. Scripts • anything you type in the command window can be written and run as a program:

  49. m files • type commands into MATLABs "notepad" • load/save as usual • two ways to run: • hit the PLAY button • Type the filename at the command prompt (without the .m)

More Related