Lecture 14 M O F for Engineering Applications Part 2 BJ Furman 28NOV2011 The Plan for Today Matlab Octave FreeMat M O F for engineering applications part 2 Recap M O F key concepts Element by element operations dot operator Function examples 2D graphs File IO Resources for more information Learning Objectives Distinguish between matrix and array arithmetic and use them appropriately Explain the differences between script files and functions Explain the basic elements of creating a 2D graph Explain how to read from and write to files Last Lecture Overview of Matlab Octave Useful commands The array as a fundamental element creating arrays indexing elements vectors extracting sub arrays using indexing and the colon operator special matrices colon operator linspace logspace ones zeros diag eye Introduction to plotting in M O F via script file comparison to Ch and Excel Arrays Vectors and Matrices Array Vector A collection of data elements arranged in rows and columns A one dimensional array single row or single column of data elements A 1 9 B 1 9 row or column column vector Matrix A two dimensional array more than one row and or column of data elements C 1 3 4 6 7 9 C 1 4 7 2 5 8 3 6 9 Colon Operator vs linspace What will the following do D 0 3 10 base increment limit F linspace 0 10 11 start end n Observations about the two methods both methods produce vectors with equally spaced elements colon operator method allows you to specify the first element and the interval spacing between elements but not the number of elements If just start end then spacing is 1 linspace method allows you to specify the first and last elements of the vector but not the spacing between elements Review of length and size What will the following do length ones 1 3 length returns the number of elements of the vector size zeros 2 3 size returns the size of the dimensions of its argument Review of Array Manipulation Given G 2 4 6 8 10 12 what is G 2 3 G 2 G 4 G 1 1 0 G 1 0 Observations Array indexing begins with 1 contrast with C means all of the elements in that dimension Extract elements by indexing Extract sub arrays using vectors as the indexing arguments More Array Manipulation Suppose H 1 9 what will this produce How could we form into a 3x3 matrix Reshape function J I 3 1 1 Reverse the order of the columns of I how I I Reverse the order of the rows of I I reshape H 3 3 How to transpose the rows to be the columns K I 3 1 1 Reverse the order of all the elements of I how Pseudocode Index I in reverse order Reshape L reshape I 9 1 1 3 3 Matrix and Array Arithmetic Arithmetic operators add sub mult right div left div expon algebr transpose addition and subtraction are done element by element same for matrix and array arithmetic Unless one is a scalar the operands must be of the same size scalar matrix or array matrix or array matrix or array matrix or arrays must be of the same size For the other operators need to distinguish between matrix and array operation n columns B Matrix arithmetic operations m n per rules of linear algebra A n rows n p rows and columns must conform For example A x B must have column and row agreement Array arithmetic operations element by element Denote with dot operator array transpose Matrix and Array Arithmetic Examples Scalar and matrix operands If L ones 1 5 and M ones 1 4 N 2 L N 1 Non scalar operations If O 1 5 O M Error using Matrix dimensions must agree L O Error using 1x5 1x5 does not work Inner matrix dimensions must agree L O 15 Same as sum L O 1x5 5x1 works Inner matrix dimensions agree Results in a 1x1 Array Operations Element by element array operation Ex Given a set of distances and times calculate average speeds and maximum of averages How would you do this in C Pseudocode Calculate avg speeds speed i distance i time i for i 1 to 4 Determine maximum speed M O F vectorize distances 120 213 87 35 in miles times 2 3 8 0 9 0 6 in hours speeds distances times note dot divide element by element max speed max speeds To get the maximum speed and its index max speed i max speeds Circuit Analysis Equations i2 i1 V i3 Matrix division 0i1 0i2 i3 R1 V 0i1 R2 R3 i2 0i3 V Recall the circuit analysis i1 i2 i3 0 R3 Matrix operations R1 R2 R1 10k R2 R3 5k V 10V Matrix solution 1 0 0 1 Ri V R 1Ri R 1V i R V 1 0 R2 R3 R 1 i1 0 R1 i2 V 0 i3 V i V use left division to solve for i i R V Think of it like inverting R and multiplying on the left side of V If we had iR V instead we d use right division to solve for i i R V Think of it like inverting R and multiplying on the right side of V i VR 1 Circuit Analysis Solution Circuit analysis solution Build R build V solve for i Build R all at once eq1 1 1 1 eq2 0 0 10e3 eq3 0 10e3 0 R eq1 eq2 eq3 Build V R 1 1 1 0 0 10e3 0 10e3 0 or build by rows and combine R1 10k R2 R3 5k V 10V V 0 10 10 Solve I R V I R V note transposed 1 0 0 1 0 R2 R3 R 1 i1 0 R1 i2 V 0 i3 V i V Dot Product Example Another example of element by element operations v1 dot product of two vectors v1 3i 2 j 5k v1 v2 v1 v2 cos v2 v2 2i 4 j 10k i j k are unit vectors for a cartesian coordinate system their coefficien ts are called measure numbers what is v1 v2 v1 v2 3 2 2 4 5 10 6 8 50 52 Dot Product Function Development Define the problem Inputs v1 v2 three element row vectors Outputs Create a function that will take two vectors as arguments and will return their vector dot product z the dot product Algorithm Multiply v1 and v2 element by element Sum the element by element products Try it in Matlab Octave Return the sum v1 3 2 5 sum v1 v 2 v 2 2 4 10 Dot Product Function in M O F Write the function function z dot prod v1 v2 dot prod v1 v2 computes the vector dot product between vectors v1 and v2 Function dot prod v1 v2 computes and returns the vector dot product between vectors v1 and v2 z sum v1 v2 Test it out A 1 2 3 B 4 5 6 what should A dot B result in A dot B dot …
View Full Document