1 / 55

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

Engr/Math/Physics 25. Chp2 MATLAB Arrays: Part-1. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu. Learning Goals. Learn to Construct 1-Dimensional Row and Column Vectors Create MULTI-Dimensional ARRAYS and MATRICES

elmer
Download Presentation

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu

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. Engr/Math/Physics 25 Chp2 MATLABArrays: Part-1 Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu

  2. Learning Goals • Learn to Construct 1-Dimensional Row and Column Vectors • Create MULTI-Dimensional ARRAYS and MATRICES • Perform Arithmetic Operations on Vectors and Arrays/Matrices • Analyze Polynomial Functions

  3. Position Vector • The vector p can be specified by three components: x, y, and z, and can be written as: • Where i, j, k are UNIT-Vectors Which are|| To The CoOrd Axes • MATLAB can accommodate Vectors having more than 3 Elements • See MATH6 for more info on “n-Space”

  4. To create a ROW vector, separate the elements by COMMAS ROW & COLUMN Vectors • Use the Transpose operator (‘) to make a COLUMN Vector >>p = [3,7,9]' p = 3 7 9 >>p = [3,7,9] p = 3 7 9 >>g = [3;7;9] g = 3 7 9 • Create Col-Vector Directly with SEMICOLONS • The TRANSPOSE Operation Swaps Rows↔Columns

  5. Can create a NEW vector by ''appending'' one vector to another e.g.; to create the row vector u whose first three columns contain the values of r = [2,4,20] and whose 4th, 5th, & 6th columns contain the values of w = [9,-6,3] Typing u = [r,w] yields vector u = [2,4,20,9,-6,3] “Appending” Vectors >> r = [2,4,20]; w = [9,-6,3]; >> u = [r,w] u = 2 4 20 9 -6 3

  6. The colon operator (:) easily generates a large vector of regularly spaced elements. Typing >>x = [m:q:n] creates a vector x of values with a spacing q. The first value is m. The last value is n IF m - n is an integer multiple of q. IF NOT, the last value is LESS than n. Colon (:) Operator >> p = [0:2:8] p = 0 2 4 6 8 >> r = [0:2:7] r = 0 2 4 6

  7. To create a row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = [5:0.1:8]. If the increment q is OMITTED, it is taken as the DEFAULT Value of +1. Colon (:) Operator cont. >> s =[-11:-6] s = -11 -10 -9 -8 -7 -6 >> t = [-11:1:-6] t = -11 -10 -9 -8 -7 -6

  8. The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment The syntax is linspace(x1,x2,n), where x1 and x2 are the lower and upper limits and n is the number of points If n is omitted, then it Defaults to 100 Thus EQUIVALENT statements linspace Comand >> linspace(5,8,31) = >>[5:0.1:8]

  9. The logspace command creates an array of logarithmically spaced elements Its syntax is logspace(a,b,n), where n is the number of points between 10a and 10b For example, x = logspace(-1,1,4) produces the vector x = [0.1000, 0.4642, 2.1544, 10.000] logspace Comand >> x = logspace(-1,1,4); >> y = log10(x) y = -1.0000 -0.3333 0.3333 1.0000 • If n is omitted, the no. of pts defaults to 50

  10. Consider this command logspace Example >> y =logspace(-1,2,6) y = 0.1000 0.3981 1.5849 6.3096 25.1189 100.0000 • Calculate the Power of 10 increment • for k = 1→6, then the kth y-value • In this Case • In this case

  11. Consider this command logspace Example (alternative) >> y =logspace(-1,2,6) y = 0.1000 0.3981 1.5849 6.3096 25.1189 100.0000 • Take base-10 log of the above >> yLog10 = log10(y) yLog10 = -1.0000 -0.4000 0.2000 0.8000 1.4000 2.0000 • Calc Spacing Between Adjacent Elements with diff command >> yspc = diff(yLog10) yspc = 0.6000 0.6000 0.6000 0.6000 0.6000

  12. Keep in mind the precise meaning of these terms when using MATLAB The length command gives the number of elements in the vector The magnitude of a vector x having elements x1, x2, …, xn is a scalar, given by √(x12 + x22 + … + xn2), and is the same as the vector's geometric length The absolute value of a vector x is, in MATLAB, a vector whose elements are the arithmeticabsolute values of the elements of x Vector: Magnitude, Length, and Absolute Value

  13. Geometric Length Mag, Length, and Abs-Val • Thus the box diagonal >> a =[2,-4,5]; >> length(a) ans = 3 >> % the Magnitude M = norm(a) >> Mag_a = norm(a) Mag_a = 6.7082 >> abs(a) ans = 2 4 5 • By Pythagoras Find vector amagnitude

  14. 3D Vector Length Example % Bruce Mayer * ENGR36 % 25Aug09 * Lec03 % file = VectorLength_0908.m % NOTE: using “norm” command is much easier % % Find the length of a Vector AB with %% tail CoOrds = (0, 0, 0) %% tip CoOrds = (2, 1, -5) % % Do Pythagorus in steps vAB = [2 1 -5] % define vector sqs = vAB.*vAB% sq each CoOrd sum_sqs = sum(sqs) % Add all sqs LAB = sqrt(sum_sqs) % Take SQRT of the sum-of-sqs

  15. 3D Vector Length Example - Run vAB = 2 1 -5 sqs = 4 1 25 sum_sqs = 30 LAB = 5.4772 % Bruce Mayer * ENGR36 % 25Aug09 * Lec03 % file = VectorLength_0908.m % % Find the length of a Vector AB with %% tail CoOrds = (0, 0, 0) %% tip CoOrds = (2, 1, -5) % % Do Pythagorus in steps vAB = [2 1 -5] % define vector sqs = vAB.*vAB % sq each CoOrd sum_sqs = sum(sqs) % Add all sqs LAB = sqrt(sum_sqs) % Take SQRT of the sum-of-sqs

  16. A MATRIX has multiple ROWS and COLUMNS. For example, the matrix M: Matrices/Arrays • M contains • FOUR rows • THREE columns • VECTORS are SPECIAL CASES of matrices having ONE ROW or ONE COLUMN.

  17. For a small matrix you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing Making Matrices • The Command • >>A = [2,4,10;16,3,7]; • Generates Matrix • spaces or commas separate elements in different columns, whereas semicolons separate elements in different rows.

  18. Given Row Vectors: r = [1,3,5] and s = [7,9,11] Combine these vectors to form vector-tand Matrix-D Making Matrices from Vectors >> r = [1,3,5]; s = [7,9,11]; >> t = [r s] t = 1 3 5 7 9 11 >> D = [r;s] D = 1 3 5 7 9 11 • Note the difference between the results given by [r s](or [r,s]) and [r;s]

  19. Use COMMAS/SPACES combined with SEMICOLONS to Construct Matrices Directly Make Matrix >> D = [[1,3,5]; [7 9 11]] D = 1 3 5 7 9 11 • Note the use of • BOTH Commas and Spaces as COLUMN-Separators • The SEMICOLON as the ROW-Separator

  20. >> r1 = [1:5] r1 = 1 2 3 4 5 >> r2 = [6:10] r2 = 6 7 8 9 10 >> r3 = [11:15] r3 = 11 12 13 14 15 >> r4 = [16:20] r4 = 16 17 18 19 20 >> r5 = [21:25] r5 = 21 22 23 24 25 >> A = [r1;r2;r3;r4;r5] A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Make Matrix Example

  21. The COLON operator SELECTS individual elements, rows, columns, or ''subarrays'' of arrays. Some examples: v(:) represents all the row or column elements of the vector v v(2:5) represents the 2nd thru 5th elements; that is v(2), v(3), v(4), v(5). A(:,3) denotes all the row-elements in the third column of the matrix A. A(:,2:5) denotes all the row-elements in the 2nd thru 5th columns of A. A(2:3,1:3) denotes all elements in the 2nd & 3rd rows that are also in the 1st thru 3rd columns.   Array Addressing

  22. You can use array indices to extract a smaller array from another array. For example, if you first create the array B Array Extraction • to Produce SubMatrixC type C = B(2:3,1:3) Rows 2&3 Cols 1-3

  23. Array Extraction Example A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 >> C = A(3:5,2:5) C = 12 13 14 15 17 18 19 20 22 23 24 25

  24. Matrix Commands

  25. Matrix Commands cont

  26. Matrix Commands cont

  27. Given Matrix A Examples: Matrix Commands >> length(A) ans = 3 >> max(A) ans = 6 7 >> min(A) ans = -10 2 >> A = [[6,2]; [-10,5]; [3,7]]; >> size(A) ans = 3 2

  28. WorkSpace Browser • Allows DIRECT access to Variables for editing & changing

  29. Array Editor • Permits changing of a single Array Valuewithout retyping the entire specification

  30. 3D (or more-D) Arrays Consist of two-dimensional arrays that ar“layered” to produce a third dimension. Each “layer” is called a page. MultiDimensional (3) Arrays pg1 pg2 pg3 pg4 3D

  31. VECTOR  Parameter Possessing Magnitude And Direction, Which Add AccordingTo The Parallelogram Law Examples: Displacements, Velocities, Accelerations SCALAR  Parameter Possessing Magnitude But Not Direction Examples: Mass, Volume, Temperature Vector Classifications FIXED or BOUND Vectors Have Well Defined Points Of Application That CanNOT Be Changed Without Affecting An Analysis Vectors Digression

  32. FREE Vectors May Be Moved In Space Without Changing Their Effect On An Analysis SLIDING Vectors May Be Applied Anywhere Along Their Line Of Action Without Affecting the Analysis EQUAL Vectors Have The Same Magnitude and Direction NEGATIVE Vector Of a Given Vector Has The Same Magnitude but The Opposite Direction Vectors cont. Equal Vectors Negative Vectors

  33. Parallelogram Rule For Vector Addition Examine Top & Bottom ofThe Parallelogram Triangle Rule ForVector Addition C B C B Vector Addition • Vector Addition isCommutative • Vector Subtraction →Reverse Direction ofThe Subtrahend

  34. Addition Of Three Or MoreVectors Through RepeatedApplication Of The Triangle Rule The Polygon Rule ForThe Addition Of ThreeOr More Vectors Vector Addition Is Associative Vector Addition cont. • Multiplication by a Scalar • Scales the Vector LENGTH

  35. Vector Notation • In Print and Handwriting We Must Distinguish Between • VECTORS • SCALARS • These are Equivalent Vector Notations • Boldface Preferred for Math Processors • Over Arrow/Bar Used for Handwriting • Underline Preferred for Word Processor

  36. The SCALAR PRODUCT or DOT PRODUCT Between TwoVectors P and Q Is Defined As Dot Product of 2 Vectors • Scalar-Product Math Properties • ARE Commutative • ARE Distributive • Are NOT Associative • Undefined as (P•S) is NO LONGER a Vector

  37. Scalar Products With Cartesian Components Scalar Product – Cartesian Comps >> p = [13,-17,-7]; q = [-16,5,23]; >> pq = dot(p,q) pq = -454 >> qp = dot(q,p) qp = -454 >> r = [1 3 5 7]; t = [8,6,4,2]; >> rt = dot(r,t) rt = 60 • MATLAB Makes this Easy with the dot(p,q) Command

  38. TWISTING Power of a Force MOMENTof the Force Quantify Using VECTOR PRODUCT or CROSS PRODUCT Cross Product • Vector Product Of Two VectorsP and Q Is Defined as TheVector V Which Satisfies: • Line of Action of V Is Perpendicular To thePlane Containing P and Q. • |V| =|P|•|Q|•sinθ • Rt Hand Rule Determines Line of Action for V

  39. Recall Vector ADDITION Behaved As Algebraic Addition BOTH Commutative and Associative. The Vector PRODUCT Math-Properties do NOT Match Algebra. Vector Products Are NOT Commutative Are NOT Associative ARE Distributive Vector Product Math Properties

  40. Vector Products Of Cartesian Unit Vectors Vector Prod: Rectangular Comps • Vector Products In Terms Of Rectangular Coordinates • Summarize UsingDeterminateNotation

  41. 3D Vector (Cross) product Evaluation is Painful c.f. last Slide MATLAB makes it easy with the cross(p,q) command The two Vectors MUST be 3 Dimensional cross command >> p = [13,-17,-7]; >> q = [-16,5,23]; >> pxq = cross(p,q) pxq = -356 -187 -207 >> qxp = cross(q,p) qxp = 356 187 207

  42. Add/Subtract Element-by-Element Array Addition and Subtraction • Using MATLAB >> A = [6,-2;10,3]; >> B = [9,8;-12,14]; >> A+B ans = 15 6 -2 17 >> p = [17 -9 3]; >> q = [-11,-4,19]; >> p-q ans = 28 -5 -16

  43. Example 2.3-2 36’ x = W y = N 25’ r 59’ 55’ 20’ w −r -20’ v = w−r z = Down

  44. Example 2.3-2 cont x = W y = N r >> r = [55,36,25]; w = [-20,59,15]; >> b = r.*r b = 3025 1296 625 >> c = sum(b) c = 4946 >> dist1 = sqrt(c) dist1 = 70.3278 w -r v = w-r z = Down >> v = w-r v = -75 23 -10 >> dist2 = sqrt(sum(v.*v)) dist2 = 79.0822

  45. CAVEAT • 2D arrays are described as ROWS x COLUMNS • Elemement of D(2,5) • 2 → ROW-2 • 5 → COL-5 • ROW Vector → COMMAS • COL Vector → SemiColons >> r = [4,73,17] r = 4 73 17 >> c = [13;37;99] c = 13 37 99

  46. Caveat cont • Array operations often CONCATENATE or APPEND • This Can Occur unexpectedly • Use the clear command to start from scratch • concatenate • con·cat·e·nate ( P ) Pronunciation Key (kn-ktn-t, kn-) • tr.v. con·cat·e·nat·ed, con·cat·e·nat·ing, con·cat·e·nates • To connect or link in a series or chain. • Computer Science. To arrange (strings of characters) into a chained list. • adj. (-nt, -nt) • Connected or linked in a series.

  47. All Done for Today • Matrix Multiplication • Explained in Fine Detail in MATH6 • Some UseFul Links Next Time:More ArrayOperations • http://www.mai.liu.se/~halun/matrix/matrix.html • http://people.hofstra.edu/faculty/Stefan_Waner/RealWorld/tutorialsf1/frames3_2.html • http://calc101.com/webMathematica/matrix-algebra.jspMatrix Multiplication

  48. Engr/Math/Physics 25 Appendix-1 Vector Math Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu

  49. Using Rt-Angle Parallelogram Resolve F into Perpendicular Components Rectangular Force Components • Define Perpendicular UNIT Vectors Which Are Parallel To The Axes • Vectors May then Be Expressed As Products Of The Unit VectorsWith The SCALAR MAGNITUDESOf The Vector Components

  50. Find: Resultant of 3+ Forces Adding by Components • Plan: • Resolve Each Force Into Components • Add LIKE Components To Determine Resultant • Use Trig to Fully Describe Resultant

More Related