15 - Intro to MATLAB - Engineering 101 Quote of the Day The...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day The longer we dwell on our misfortunes, the greater is their power to harm us. - Voltaire Intro to MATLAB Consider a General Statics Problem A sign is suspended away from a building by a network of struts and joints. All angles are 45 or 90 degrees. Consider a General Statics Problem T1 T5 T4 T2 T3 OPEN 24 HOURS Each strut has a tension along the strut. A negative tension would mean that the strut is in compression. T6 OPEN 24 HOURS Consider a General Statics Problem T1 T5 T4 W/2 T2 T3 W/2 OPEN 24 HOURS In addition the sign exerts a force on the joints to which it is attached. W1 Consider a General Statics Problem T1 T5 W2 T4 W/2 T2 T3 W/2 OPEN 24 HOURS In addition the sign exerts a force on the joints to which it is attached. And the wall exerts forces via the joints attached to the wall. T6 T6 1 Consider a General Statics Problem W1 T1 T5 W2 T4 W/2 T2 T3 W/2 OPEN 24 HOURS At each of the joints the forces have to exactly balance. If the forces didn't balance there would be acceleration W1 Consider a General Statics Problem T1 T5 W2 y x T4 W/2 T2 T3 W/2 OPEN 24 HOURS A general note: The notation differs from the book in that the T's always denote tensions. You can convert between the two notations by reversing all signs of T's in the x direction and the sign of T5 in the y direction. T6 T6 Consider a General Statics Problem T1 + W1x = 0 W1 Consider a General Statics Problem W1 T1 T5 W2 T4 W/2 y x W1y = 0 T1 T5 T6 T3 W/2 OPEN 24 HOURS W/2 T2 Consider the joints one at a time: T1 + W1x = 0 - T1 - T5 / 2 + T2 / 2 = 0 W1y = 0 - T5 / 2 - T6 - T2 / 2 = 0 T2 T3 W/2 OPEN 24 HOURS T6 W2 y T4 x Consider a General Statics Problem W1 T1 T5 W2 y x T4 W/2 Consider a General Statics Problem W1 T1 T5 W2 T4 W/2 y x T1 + W1x = 0 - T1 - T5 / 2 + T2 / 2 = 0 W1y = 0 - T5 / 2 - T6 - T2 / 2 = 0 W2 x + T4 + T5 / 2 = 0 T6 T3 W/2 OPEN 24 HOURS T2 W2 y + T5 / 2 = 0 T1 + W1x = 0 - T1 - T5 / 2 + T2 / 2 = 0 W1y = 0 - T5 / 2 - T6 - T2 / 2 = 0 W2 x + T4 + T5 / 2 = 0 T6 T3 W/2 OPEN 24 HOURS T2 W2 y + T5 / 2 = 0 T3 - T4 = 0 - W / 2 + T6 = 0 2 Consider a General Statics Problem W1 T1 T5 W2 y x T4 W/2 T1 + W1x = 0 - T1 - T5 / 2 + T2 / 2 = 0 W1y = 0 - T5 / 2 - T6 - T2 / 2 = 0 W2 x + T4 + T5 / 2 = 0 T6 T3 W/2 OPEN 24 HOURS T2 W2 y + T5 / 2 = 0 T3 - T4 = 0 - W / 2 + T6 = 0 - T3 - T2 / 2 = 0 - W / 2 + T2 / 2 = 0 Convert to a Matrix 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - T1 - T5 / 2 + T2 / 2 = 0 W2 x + T4 + T5 / 2 = 0 - T5 / 2 - T6 - T2 / 2 = 0 W2 y + T5 / 2 = 0 - T3 - T2 / 2 = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 T1 0 T2 0 T3 0 T4 0 T5 = 0 T6 0 W1x 0 W1y 0 W2x 0 W2y 0 T2 / 2 = W / 2 T1 + W1x = 0 W1y = 0 T3 - T4 = 0 T6 = W / 2 Convert to a Matrix -1 1/2 1/ 0 -1/2 1/ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1/2 1/ 0 -1/2 1/ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - T1 - T5 / 2 + T2 / 2 = 0 - T5 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 T2 0 T3 0 T4 0 T5 = 0 T6 0 W1x 0 W1y W/2 W2x 0 W2y 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 3 Convert to a Matrix -1 1/2 1/ 0 -1/2 1/ 0 0 0 0 1 0 0 0 0 -1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 0 -1/2 1/ 0 -1/2 1/ 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 T3 - T4 = 0 T6 = W / 2 T1 0 T2 0 T3 0 T4 0 T5 = 0 T6 0 W1x 0 W1y W/2 W2x 0 W2y W/2 Convert to a Matrix But this matrix is neither upper or lower triangular. How can we solve the problem? -1 1/2 1/ 0 -1/2 1/ 0 0 0 0 1 0 0 0 0 -1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 0 -1/2 1/ 0 -1/2 1/ 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 T1 0 T2 0 T3 0 T4 0 T5 = 0 T6 0 W1x 0 W1y W/2 W2x 0 W2y W/2 Exercise Consider the following diagram of a reactor The input stream is composed of 50kmol/hr of each X and Y. The output stream is 100% X The recycle stream is 100% Y The reactor turns 50% of the Y to X in each pass. 1 Exercise Which Matrix and Vector pair represent these equations where: 1 x1 [M] y1 = v y2 2 x3 2 3 3 4 Making the Matrix Lower Diagonal How can we make our matrix lower diagonal? We know the order of equations does not matter. 1 2 5 a 7 3 7 4 b = 2 -1 0 3 c 0 Making the Matrix Lower Diagonal How can we make our matrix lower diagonal? We know the order of equations does not matter. 1 2 5 a 7 3 7 4 b = 2 -1 0 3 c 0 4 Making the Matrix Lower Diagonal We can swap the equations to try to make the matrix diagonal 3 1 -1 7 2 0 4 5 3 a 2 b = 7 c 0 Convert to a Matrix We can pick the order of rows to get as close to diagonal as possible -1 1/2 1/ 0 -1/2 1/ 0 0 0 0 1 0 0 0 0 -1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 -1 0 1 0 -1/2 1/ 0 -1/2 1/ 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 T1 0 T2 0 T3 0 T4 0 T5 = 0 T6 0 W1x 0 W1y W/2 W2x 0 0 0 0 0 0 1 0 0 0 0 W2y W/2 Convert to a Matrix Which others would we swap? -1 1/2 1/ 0 -1/2 1/ 0 0 0 0 1 0 0 0 0 -1/2 1/ 0 1/2 1/ 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 0 -1/2 1/ 0 -1/2 1/ 1 1/2 1/ 0 0 0 0 0 0 0 0 0 0 -1 0 0 1/2 1/ 0 -1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 0 T3 0 T4 W/2 T5 = 0 T6 0 W1x 0 W1y W/2 W2x 0 W2y 0 Convert to a Matrix We can get pretty close, but not quite. 0 -1/2 1/ 0 1/2 0 -1/2 1/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1/2 1/ -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 W/2 T3 0 T4 0 T5 = 0 T6 W/2 W1x 0 W1y 0 W2x 0 W2y 0 We Need Another Trick We know if we have two equations: 2x + 3y + z = 5 x +y =2 We can multiply one by a number and add them to get a new equation: 2x + 3y + z = 5 -2( x + y = 2) We Need Another Trick We know if we have two equations: 2x + 3y + z = 5 x +y =2 We can multiply one by a number and add them to get a new equation: 2x + 3y + z = 5 -2x - 2y = -4 5 Convert to a Matrix We Need Another Trick We know if we have two equations: 2x + 3y + z = 5 x +y =2 We can multiply one by a number and add them to get a new equation: 2x + 3y + z = 5 -2x - 2y = -4 0 +y +z=1 Select two rows that have a non-zero element in the column. 0 -1/2 1/ 0 1/2 1/ 0 -1/2 1/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1/2 1/ -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 W/2 T3 0 T4 0 T5 = 0 T6 W/2 W1x 0 W1y 0 W2x 0 W2y 0 Convert to a Matrix Add one to the other. 0 -1/2 1/ 0 1/2 1/ 0 -1/2 1/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1/2 1/ -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 W/2 T3 W/2 T4 0 T5 = 0 T6 W/2 W1x 0 W1y 0 W2x 0 W2y 0 Convert to a Matrix Select another two rows. Subtract one from the other 0 -1/2 1/ 0 1/2 1/ 0 -1/2 1/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1/2 1/ -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 W/2 T3 W/2 T4 0 T5 = 0 T6 W/2 W1x 0 W1y 0 W2x 0 W2y 0 Convert to a Matrix Subtract one from the other. 0 -1/2 1/ 0 1/2 1/ 1 -2/2 2/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 W/2 T3 W/2 T4 0 T5 = 0 T6 W/2 W1x 0 W1y 0 W2x 0 W2y 0 Convert to a Matrix Now we need to swap to make sure there are no zeros on the diagonal. 0 -1/2 1/ 0 1/2 1/ 1 -2/2 2/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 0 T2 W/2 T3 W/2 T4 0 T5 = 0 T6 W/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Now we can use our Lower Diagonal solve algorithm from last lecture! 1 0 0 1/2 1/ 0 -1/2 1/ 0 0 -1 1/2 1/ 0 0 1 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1/2 1/ 0 0 0 0 0 0 1 1/2 1/ 0 1/2 1/ 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 T1 3W/2 T2 W/2 T3 0 T4 0 T5 = 0 T6 W/2 W1x 0 W1y 0 W2x 0 W2y 0 Gaussian Elimination This process is known as Gaussian elimination. It can fairly easily be implemented for any general matrix. See pages 275-279 in the text 275- Gaussian Elimination PseudoPseudo-code for the example on page 278 (except this makes a lower triangular matrix) Start with the row i=N-1. i=NMake sure the element on the diagonal A[ i ][ i ] is not zero. If it is zero swap with a row in which A[ j ][ i ] is non-zero. nonHere j < i. Divide all elements in row i by A[ i ][ i ]. Subtract A[ j ][ i ] * row [ i ] from each row j < i. Now every other row has a 0 value in column [ i ]. Do the same for row i-1, repeat. i- 7 MATLAB and Matrices While we can implement this in C++, MATLAB provides functionality to solve just these kinds of equations. If we set up a matrix M and a vector v such that M*s = v, we can find s in MATLAB by v, executing the one-line program: ones = M\v M\ C++ vs. MATLAB Compiled Fast Predefined Libraries Custom Design Data Types Graphics Difficult Interpreted Slow Even More Libraries Matrix Based Easy Graphics Functionality To MATLAB the Whole World is a Collection of Arrays To MATLAB all data is some kind of array. Even individual data objects are arrays with 1 row and 1 column. Vectors are arrays with only 1 dimension. Matrices may have 2 or more dimensions. Variable names must start with a letter which can be followed by letters, numbers and the _ character. MATLAB is a Weakly Typed Language Unlike C++ you do not have to declare variable types before each data object is used. The data type is determined by what is assigned to the variable. Most data objects are either double or char. char. In MATLAB doubles include imaginary and complex values To Create a New Variable Simply assign some value to the variable and MATLAB creates it. Brackets denote matrices. var = 45.8; complex = 0.5 0.5 * i; list = [ 1.0 2.0 3.0 ]; x = 2.0; y = 3.0; Creating Matrices Matrices are automatically assumed to be in row order separated by commas or spaces. Rows can be separated by semicolons (;) or new lines. a = [1.0, 3.0, 5.0; 2.0, 4.0, 6.0] b = [1.0 3.0 5.0 2.0 4.0 6.0 ] 8 Creating Matrices The single quote after a matrix definition causes it to transpose v=[0 1 2 3] w=v' Is equivalent to the statement w = [ 0; 1; 2; 3] or w=[ 0 1 2 3] Creating Matrices The number of elements in every row must be the same and the number of elements in every column must be the same. b = [ 10 15 20; 6 9 ] error! Creating Matrices An element of a matrix may be referenced using parentheses. Note that, unlike C++, rows and columns in MATLAB are numbered starting at 1. If c has not been previously created c(2, 3) = 5; Is equivalent to the statement c=[ 0 0 0 0 0 5 ]; Creating Matrices You may also use algebraic operations or references to data in the definition of an array a = [ 0 1+1 1+3*4 ]; b = [ a(2) a(1) a ]; Is equivalent to a = [ 0 2 13 ]; b = [ 2 0 0 2 13 ]; Which matrix is created by the following MATLAB code? a1 = [1; 2; 4; 8] a1 a2 a4 A = = = = a1' [a1(2) a1(3) a1(3)*2 a1(3)*4] [6, 5, 4, 3] [a1; a2 a4(3)-1, a4(3), a4(2), a4(1) a1(3), 2, a1(1), 0] Shortcut Expressions for Matrix Initialization If you want to create a vector in which the terms are regularly spaced you can use the colon operator. first:increment:last x = 1:2:8; Is equivalent to x = [ 1 3 5 7 ]; you can leave out the increment if it is 1 For example y=1:4 is the same as y=[1 2 3 4] 1 A = 1 2 4 6 1 2 3 4 2 4 2 5 2 4 4 2 4 8 1 4 4 8 5 1 8 16 0 3 8 16 6 0 2 A = 1 2 4 8 1 4 2 4 2 4 8 16 2 8 3 2 3 4 5 6 4 16 4 1 4 2 1 0 8 32 5 0 3 A = 4 A = 9 Next Lecture Matrices in MATLAB 10 ...
View Full Document

This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.

Ask a homework question - tutors are online