Unformatted text preview: Engineering 101 Engineering 101
Lecture 21 11/15/07
MATLAB Intro Quote of the Day Quote of the Day
The longer we dwell on our misfortunes, the greater is their power to harm us.
Voltaire About that exam 3 About that exam 3 Question Question 1 EXAM 3 OMG EXAM 3 OMG Question 1:
Enter something: dx rl wo o he hello world hello worldxdxrlwoo Xhe EXAM 3 OMG EXAM 3 OMG Question 1:
Enter something: gy og d lk wa walk doggy walk doggygyog dlkwa EXAM 3 OMG EXAM 3 OMG Question 1:
Enter something: rx ne un r ow wi willow runner willow runnerxrxneun rowXwi EXAM 3 OMG EXAM 3 OMG Question 1:
Enter something: it ra y si silly rabbit silly rabbititXray Xsi Question 2 Question 2 What do you think this does? vector <vector <int> > generateTable(ifstream & in) { vector <int> row; vector <vector <int> > table; string inString; int count, i; Question 3 getline(in, inString); while (!in.fail()) { row.clear(); for (i = 0, count = 0; i < inString.size(); i++) { if (inString.at(i) == ' ') { if (count !=0) row.push_back(count); count = 0; } else count++; } if (count !=0) row.push_back(count); table.push_back(row); getline(in, inString); } return table; } Question 3
double findMaxAvg(vector <vector <int> > table) { double currentMaxAvg = 0; double colCount, rowSum; for (int i = 0; i < table.size(); i++) { rowSum = colCount = 0; for (int j = 0; j < table.at(i).size(); j++) { rowSum += table.at(i).at(j); colCount++; } if (currentMaxAvg < (rowSum / colCount)) currentMaxAvg = rowSum / colCount; } return currentMaxAvg; } It’s not that bad, this could be you… It’s not that bad, this could be you… Consider a General Statics 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. OPEN 24 HOURS Consider a General Statics Consider a General Statics Problem
T1 T5 T4 T6 T3 OPEN 24 HOURS T2 Each strut has a tension along the strut. A negative tension would mean that the strut is in compression. Consider a General Statics Consider a General Statics Problem
T1 T5 T6 T4 W/2 T3 W/2 OPEN 24 HOURS T2 In addition the sign exerts a force on the joints to which it is attached. Consider a General Statics Consider a General Statics Problem
W1 T1 T5 W 2 T6 T4 W/2 T3 W/2 OPEN 24 HOURS T2 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. Consider a General Statics Consider a General Statics Problem
W1 T1 T5 W 2 T6 T4 W/2 T3 W/2 OPEN 24 HOURS T2 At each of the joints the forces have to exactly balance. If the forces didn’t balance there would be acceleration Consider a General Statics Consider a General Statics Problem
W1 T1 T5 W 2 y x T6 T4 W/2 T3 W/2 OPEN 24 HOURS T2 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. Consider a General Statics Problem
T1 + W1x = 0 W1y = 0
W1 T1 T5 W 2 y x T6 T4 W/2 T3 W/2 OPEN 24 HOURS T2 Consider the joints one at a time: Consider a General Statics Problem Consider a General Statics Problem
W1 T1 T5 W 2 y x 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 T4 W/2 Consider a General Statics Problem Consider a General Statics Problem
W1 T1 T5 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 T4 W/2 T3 T2 W2 y + T5 / 2 = 0 W/2 OPEN 24 HOURS Consider a General Statics Problem Consider a General Statics Problem
W1 T1 T5 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 T4 W/2 T3 T2 W2 y + T5 / 2 = 0 T3 − T4 = 0 − W / 2 + T6 = 0 W/2 OPEN 24 HOURS Consider a General Statics Problem Consider a General Statics Problem
W1 T1 T5 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 T4 W/2 T3 T2 W2 y + T5 / 2 = 0 T3 − T4 = 0 − W / 2 + T6 = 0 − T3 − T2 / 2 = 0 − W / 2 + T2 / 2 = 0 W/2 OPEN 24 HOURS Convert to 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 T2 / 2 = W / 2 T1 + W1x = 0 W1y = 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 0 0 0 T3 − T4 = 0 T6 = W / 2 Convert to Convert to a Matrix
1 1/√2 0 1/√2 00 00 00 00 00 00 00 00 0 0 0 0 0 0 0 0 0 0 0 1/√2 0 1/√2 00 00 00 00 00 00 00 00 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 / 2 − T6 − 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 T1 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 0 0 0 Convert to a Matrix Convert to a Matrix
1 1/√2 0 1/√2 00 00 00 00 00 00 00 00 0 0 0 0 0 0 0 0 0 0 0 1/√2 0 1/√2 1 1/√2 0 1/√2 00 00 00 00 00 00 0 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 0 W2 x + T4 + T5 / 2 = 0 W2 y + T5 / 2 = 0
0 0 0 1 0 0 0 0 0 0 T1 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Convert to a Matrix Convert to a Matrix
1 1/√2 0 1/√2 00 00 10 00 00 00 00 00 0 0 0 0 0 0 0 0 0 0 0 1/√2 0 1/√2 1 1/√2 0 1/√2 00 00 00 00 00 00 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 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 T1 + W1x = 0 W1y = 0
T1 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 0 0 0 Convert to a Matrix Convert to a Matrix
1 1/√2 0 1/√2 00 00 10 00 0 1/√2 0 1/√2 00 00 0 0 0 0 0 0 1 0 0 0 0 1/√2 0 1/√2 1 1/√2 0 1/√2 00 00 00 00 00 00 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 0 0 1 0 0 0 0 0 0 0 − T3 − T2 / 2 = 0 T2 / 2 = W / 2
0 0 0 1 0 0 0 0 0 0 T1 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 W/2 0 0 Convert to a Matrix Convert to a Matrix
1 1/√2 0 1/√2 00 00 10 00 0 1/√2 0 1/√2 00 00 0 0 0 0 0 0 1 0 1 0 0 1/√2 0 1/√2 1 1/√2 0 1/√2 00 00 00 00 1 0 00 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 W/2 0 W/2 Convert to a Matrix Convert to a Matrix
But this matrix is neither upper or lower triangular. How can we solve the problem?
1 1/√2 0 1/√2 00 00 10 00 0 1/√2 0 1/√2 00 00 0 0 0 0 0 0 1 0 1 0 0 1/√2 0 1/√2 1 1/√2 0 1/√2 00 00 00 00 1 0 00 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 0 0 0 0 W/2 0 W/2 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 2 3 4 Exercise 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 2 3 4 Exercise Exercise Which Matrix and Vector pair represent these equations where: 1 x1 [M] y1 = v y2 2 x3 Exercise Exercise 3 Which Matrix and Vector pair represent these equations where: 1 x1 [M] y1 = v y2 2 x3 Exercise Exercise 3 How can we make our matrix lower diagonal? We know the order of equations does not matter. 125 a 7 374 b = 2 1 0 3 c 0 Making the Matrix Making the Matrix Lower Diagonal How can we make our matrix lower diagonal? We know the order of equations does not matter. 125 a 7 374 b = 2 1 0 3 c 0 Making the Matrix Making the Matrix Lower Diagonal We can swap the equations to try to make the matrix diagonal 4 5 3 a b = c 2 7 0 Making the Matrix Making the Matrix Lower Diagonal 3 7 1 2 1 0 Convert to a Matrix Convert to a Matrix
We can pick the order of rows to get as close to diagonal as possible
1 1/√2 0 1/√2 00 00 10 00 0 1/√2 0 1/√2 00 0 0 0 0 0 0 1 0 1 0 1/√2 0 1/√2 1 1/√2 0 1/√2 00 00 00 00 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 T2 T3 T4 T5 = T6 W1x W1y W2x 0 0 0 0 0 0 0 W/2 0 0 0 0 0 0 1 0 0 0 0 W2y W/2 Convert to a Matrix Convert to a Matrix
Which others would we swap?
1 1/√2 0 1/√2 00 00 10 00 0 1/√2 0 1/√2 00 00 0 0 0 0 0 0 1 0 1 0 0 1/√2 0 1/√2 1 1/√2 00 00 00 00 00 1 0 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 0 0 W/2 0 0 0 W/2 0 0 Convert to a Matrix Convert to a Matrix
We can get pretty close, but not quite.
0 1/√2 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 1 0 0 1 0 0 0 0 0 0 00 00 0 1/√2 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 W/2 0 0 0 W/2 0 0 0 0 We Need Another Trick 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 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 We Need Another Trick 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 Convert to a Matrix Convert to a Matrix
Select two rows that have a nonzero element in the column.
0 1/√2 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 1 0 0 1 0 0 0 0 0 0 00 00 0 1/√2 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 W/2 0 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Add one to the other.
0 1/√2 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 1 0 0 1 0 0 0 0 0 0 000 000 0 1/√2 0 1 0 0 0 1/√2 0 001 000 000 1 1/√2 0 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 W/2 W/2 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Select another two rows. Subtract one from the other
0 1/√2 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 1 0 0 1 0 0 0 0 0 0 00 00 0 1/√2 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 W/2 W/2 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Subtract one from the other.
0 1/√2 0 1/√2 1 2/√2 00 1 1/√2 00 10 00 00 00 1 0 0 1 0 0 0 0 0 0 00 00 0 0 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 W/2 W/2 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Now we need to swap to make sure there are no zeros on the diagonal.
0 1/√2 0 1/√2 1 2/√2 00 1 1/√2 00 10 00 00 00 1 0 0 1 0 0 0 0 0 0 00 00 0 0 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 0 W/2 W/2 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Now we need to swap to make sure there are no zeros on the diagonal.
1 2/√2 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 0 0 1 1 0 0 0 0 0 0 0 0 00 00 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y W/2 W/2 0 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Add twice
1 2/√2 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 0 0 1 1 0 0 0 0 0 0 0 0 00 00 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y W/2 W/2 0 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Add twice.
1 0 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 0 0 1 1 0 0 0 0 0 0 0 0 00 00 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 3W/2 W/2 0 0 0 W/2 0 0 0 0 Convert to a Matrix Convert to a Matrix
Now we can use our Lower Diagonal solve algorithm from last lecture!
1 0 0 1/√2 0 1/√2 00 1 1/√2 00 10 00 00 00 0 0 1 1 0 0 0 0 0 0 0 0 00 00 1 0 0 1/√2 00 00 00 1 1/√2 0 1/√2 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 T2 T3 T4 T5 = T6 W1x W1y W2x W2y 3W/2 W/2 0 0 0 W/2 0 0 0 0 Gaussian Elimination Gaussian Elimination This process is known as Gaussian elimination. It can fairly easily be implemented for any general matrix. Gaussian Elimination Gaussian Elimination Start with the row i=N1. Make 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. Here 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 i1, repeat. News Flash News Flash UM vs Ohio State http://www.youtube.com/watch?v=AZFmDHI_JcI Tracking fingers with the wiiMote http://www.youtube.com/watch?v=0awjPUkBXOU
MATLAB and Matrices 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 executing the oneline program: s = M\v C++ vs. MATLAB 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 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. 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. In MATLAB doubles include imaginary and complex values MATLAB is a Weakly Typed MATLAB is a Weakly Typed Language To Create a New Variable 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 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 ] Creating Matrices 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 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 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 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 ]; Shortcut Expressions for Matrix 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] Next Lecture Next Lecture Matrices in MATLAB ...
View
Full Document
 Fall '07
 JeffRingenberg
 Volt, Nuclear magnetic resonance, 0 W, Terminator 2: Judgment Day, Terminator, 0 0 0 W, 2 0 W

Click to edit the document details