Popular in Course
verified elite notetaker
Popular in Engineering and Tech
This 53 page Reader was uploaded by Madeline Dippel on Thursday April 3, 2014. The Reader belongs to a course at University of California Santa Barbara taught by a professor in Fall. Since its upload, it has received 191 views.
Reviews for Eng 3
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 04/03/14
Engineering 3 Professor Linda Petzold Spring 2014 Lecture 1 Topics covered Identity Matrix Office Hours Professor Petzold Mon 13 5107 Harold Frank Hall Ta Mike Mon 1012 1526Phe1ps Ta Ben 122 1525 Phelps Taoseph 35 1525 Phelps How can I access Matlab Instructional Computing Labs You can log onto collaborative computers in Phelps SSMS and LSCF using your UCSB net ID ECI Labs There is an E1 lab in Harold Frank Hall 1140 and a CAD lab in the engineering pavilion Auhill student center These labs have longer hours than the collaborative labs Get an ECI account this allows you to access matlab remotely from your computer instructions are on the syllabus on gauchospace Buy Matlab student Version is approximately 100 Course Structure There will be weekly modules posted on gauchospace presenting information to be covered in lecture Following each module there will be a quiz online 2 chances highest score is taken Homework is due at 8AM on Tuesdays usually There will be a forum discussion for each module which can be used as a resource for help Grade Breakdown Lecture and attendance 5 Quizzes 5 Homework 25 Midterm 25 April 23 Final 40 Friday Iune 13th 123 in IC SSMS labs No makeup exams Work done individually aka no cheating How to study Practice Practice Practice programming is a skill clear variablename clears a specific variable clc clears all commans clears entire workspace essentially in the command window help can give a short definition of a command in the command window doc opens up a document explaining a command in matlab the up arrow key goes to the last command entered time saver Remember capitalization matters To save something as a separate file type in edit filemnamem a semicolon at the end of a statement prevents that statement from being displayed For example if you want to create a column vector A 21 you can put a at the end of the statement to prevent the column vector from being shown in the command window this is good when you just want to establish variables How to create a matrix in matlab Q3 Q 3 E j gt Users gt madelinedippel gt Documents gt MATLAB Curr I39 I Na A fj assi forlo 1 matr ocaeStru 123 M J3 p E C Qtwggwg 1 39Iwu quot 3 i39 Cw V ltStudent Versiongt Command Window 3 JO1p3 o 4 Student License for use in conjunction with courses offered at a degreegranting institution Professional and commercial use prohibited EDUgtgt A1 2 4 A 1 3 2 4 EDUgtgt B1234 B 1 3 2 4 EDUgtgt C1234 C hlJlll fa EDUgtgt As can be seen in the above examples brackets surround the elements of the matrix such as when A1 23 4 In this matrix A 1 and 2 are elements of the first row of the matrix and the semicolon separating 12 and 34 tells matlab to start a new row The elements in the row of the matrix in A are separated by a space but in Matlab they can also be separated by commas as shown in B HOME quotP R7 39 E 1 gt Users gt madelinedippel r Documents gt MATLAB Curr Q NaA t assi forlo J matr Details A Wor F3 Name A IIA ans IE3 c Iocalestru PLOTS APPS E M W E g 3 L Qg u A 4 ltStudent Versiongt Command Window 9 EDUgtgt A1 23 4 ionpa O 1 2 3 4 EDUgtgt sizeA ans 2 2 EDUgtgt A ans 1 3 2 4 EDUgtgt transposeA ans 1 3 2 4 EDUgtgt A22 ans 4 f3 EDUgtgt Shown above are examples of several Matrix functions sizeA displays the size of matrix A A the apostrophe after the matrix name gives the transpose of the matrix transposeA does the same operation but if complex numbers are involved it won39t ip the signs and give the conjugate of the transposed matrix like the operation A A22 displays the value for the element in the second row and second column of A H HOME I4 13 E 1 gt Users gt madelinedippel gt Documents gt MATLAB Curr IVI PLOTS APPS EH M a 4 L E Q ui l tw x mi 1 A V ltStudent Versiongt Command Window 3 5 N EDUgtgt A 2 1 assi forlo ans J matr 2 4 EDUgtgt A2 ans 3 4 EDUgtgt A222 A 1 2 3 2 EDUgtgt H18 Dt I2 A H Wor 39 1 2 3 4 5 6 7 8 Name A A EDUgtgt H128 ans 3 H c H 1 3 5 7 l localestru fa EDUgtgt Additionally A2 displays the second column of A The colon sign means all essentially So what this expression says to matlab is take all rows and column 2 of A A2 reversely displays the second row of A A222 assigns the value 2 to the element of the second row second column of A As you can see above the new matrix with the new value is displayed H18 here once again means all so this expression displays all s 18 H128 in this case the sequence will display all numbers 18 in increments of two So it starts at 1 then 12 etc Matrix Multiplication When can you do it Let39s say you want to perform AB A times B A is an nxm matrix and B is a cxd matrix in this case n and c represent the number of rows in each matrix and m and d represent the number of columns Now to multiply matrices you multiply the rows of A by the columns of B which means that the number of columns in A must equal the number of rows of B aka m must equal c The resulting matrix after performing matrix multiplication would then have n rows and d columns ie it would be a nxd matrix In simpler terms nxmcxdgtnxd JOHP O I HOME Curr G i NaA assi 39 forlo J matr PLOTS APPS LG M W E I U L Q w l H m cm J 394 WE E 1 gt Users gt madelinedippel r Documents gt MATLAB V ltStudent Versiongt Command Window 9 EDUgtgt Al1 23 4 A 1 2 3 4 EDUgtgt 32 4 1 5 E Iocalestru Matrix Multiplication in Matlab B 2 4 1 5 EDUgtgt AB ans 4 14 10 32 EDUgtgt C1 4 5 6 7 8 C 1 4 5 6 7 8 EDUgtgt AmC Error using Inner matrix dimensions must agree f EDUgtgt As can be seen from the example above since the there were more columns in C than Rows in A matlab could not multiply A and C That39s why an error message appeared However since A and B are both 2x2s matlab could multiply them Matlab took the first column of B and multiplied it by the first row of A to get the first element of the first row in the product The second column of B times the second row of A gave the second element in the first row etc etc If you need extra help on matrix multiplication see http wwwkhanacademyorg math algebra2 algebra matricesmatrixmultiplicationvmatrixmultiplication part 1 The Identity Matrix This is a special kind of a matrix where the diagonal of the matrix is all ones and the rest of the matrix is composed of zeros The identity Matrix times any matrix A equals the original matrix A In matlab a 3x3 identity matrix is written as Ieye3 remember an identity matrix must always be a square matrix to keep its form Examples shown below J01P O H HOME I E 1 gt Users gt madelinedippel gt Documents D MATLAB ltStudent Versiongt Command Window v Curr E i Na A J assi forlo quot matr Details A PLOTS APPS EDUgtgt Ieye3 I 1 0 0 0 1 0 0 0 1 EDUgtgt A1 2 3 5 7 3 A 1 2 3 5 7 3 EDUgtgt AI 3l395 1 2 3 5 7 3 J55 EDUgtgt 3 gm ff 3 3 5 E QSear39cI1 Documentation 3 T 39 4 X OlP3 b E Engineering 3 Lecture 2 Topics Covered Comments If Statements Relational Operators Logical Operators Loops AB is matrix multiplication however AB is element wise multiplication Basically AB tells matlab to multiply all the elements of A by their respective elements of B Similarly A 2 Instead of squaring A squares all the elements of A So Aquot2 is matrix multiplication and not the same thing as A 2 See below picture for examples in 4 393 JOHP O HOME PLOTS APPS LL53 M W J E J L E OI l Kt H1 mt t 4 I5 13 E 1 gt Users gt madelinedippel gt Documents gt MATLAB Curr G ltStudent Versiongt Command Window E N37 EDUgtgt A1 11 1 lquot 5539 EDUgtgt 32 22 2 forlo EDUgtgt cAB matr EDUgtgt C C 2 2 2 2 EDUgtgt Bquot2 BHS 4 4 4 4 EDUgtgt whereas EDUgtgt Bquot2 EH15 DmI A 8 8 39 8 8 Wor v Name A g EDUgtgt A I ans 3 c H I localestru in Matlab 1 Parentheses 2 Power A 3 Multiplication Division 4 Addition Subtraction By these rules in Matlab 13quot42 20123 Try it for yourself pi314159 i andj imaginary numbers square root of 1 eps 0 ans o 22204equot 16 this is the smallest number that Matlab can count by inf infinity NaN not a number good way to tell if your program is wrong exp1 27183 expx equotx disp This function displays an answer Comments These are created using the sign at the beginning of the comment Comments aren39t actual code so matlab will skip over them as if they don39t exist They are useful tools to help other people understand what you39re doing with yourcode IfquotSta 1ts These are statements that involve if and sometimes terms such as else if They follow a logical order sort of like if statements in English It39s easiest then to follow the logic of examples and practice doing them individually Some examples HOME PLOTS APPS Lu W re E3 L Q u31r quot91 pquot z quotF R7 39I E 1 gt Users gt madelinedippel gt Documents gt MATLAB V 0 CurrentFoder GW ltStudentVersiongt Command Window 9 E NameA 5 4 39 w la55399 m Student License for use in conjunction with courses offered at a lf 39l p m degree granting institution Professional and commercial use prohibited J matrixprobm EDUgtgt g10 EDUgtgt if glt20 disp 39hello Details Workspace Name A 9 Q Iocalestruct end hello EDUgtgt sIn this statment glt20 so the program displays the string hello fa EDUgtgt isa string is a list of characters such as a word and is enclosed by apostrophes HOME PLOTS 4 E 1 gt Users gt madelinedippel I Documents 5 MATLAB ltStudent Versiongt Command Window v Current Folder 6 i Name A Q assignm forloopm Q matrixprobm Details A Workspace Cv Name A 9 9 H locaeStruct APPS EDUgtgt g1e EDUgtgt if glt5 disp39small39 elseif glt20 disp39medium39 else disp39large39 end medium L73 my 1 jg g E QSearch Documentatu 2 EDUgtgt 95in this more complicated elseif statement ggt5 and so Matlab skips the first part 133 EDUgtgt ssand goes to the second to see if that statement is true Since glt20 the program then finishes and displays the string 39medium39 OH 4 I o1p3 o Relational Operators 0 lt is less than 0 lt is less than or equal to 0 gt is greater than 0 gt is greater than or equal to is equal to not to be confused with which only assigns a variable a value is not equal to If a statement is true Matlab will display a 1 and if a statement is false it will display a0 Ex Comparing Matrices H0 ME PLOTS E 1 gt Users gt madelinedippel b Documents b MATLAB ltStudent Versiongt Command Window 2 Current Folder 6 i Name D assignm E forloopm E matrixprobm Details A Workspace 6 Name A 3A 33 EC 3 9 3 ocaeStruct gt I39 39lt APPS EDUgtgt A1 2 3 EDUgtgt B3 2 1 EDUgtgt CAltB EDUgtgt C C EDUgtgt 39sfor the first elements of A and B the statement is true which is why C1 135 EDUgtgt 9sHowever for the others the statement is false which is why C0 1 0 0 2 Q My 1 3 E Q5earch Documentation v mupa o E Logical Operators is not amp is and is or ex 1lt2 amp3lt2 is not true 0 since one of the statements is not true ex 1lt2 3lt2 is true 1 since at least one of the statements is true ex 1lt2 amp 3lt2 is true 1 since the not sign makes the answer the opposite of whatever the answer would normally be for the statement it affects Loops Basic for loops These are used when you know the number of iterations you want 2 HOME PLOTS APPS E M E 4 E q5C6139CquotlDCICL 39Il2quotldiIJquotI E II E E E 1 gt Users gt madelinedippel gt Documents gt MATLAB V P CurrentFolder 9 ltStudentVersiongt Commandwindow g E E 39quotT EDUgtgt for i110 5quot JaSsIgnm quot foroopm end matrixprobm 1 2 3 4 5 6 7 8 9 10 Details A 39 EDUgtgt for i126 Workspace 3 di5p 1 NameA V end ERA 1 EB EC 1 3 E9 1 E s 5 E Iocalestruct J fa EDUgtgt For the first loop there are 10 iterations from 110 For the second there are 3 from 16 For more information on what the means see first lecture notes Engineering 3 Lecture 3 4714 For Loops Continued Possible Exam question if you write disps what is Matlab going to display disps or disp s Similar formatting is used for questions on quizzes and midterms Pro gram s039 for k139I0 ssk end What s 039 on in the above Value of S Value of 0 39 39 of 1 3 6 10 15 21 28 36 45 55 d ofl gt D0O10U1gtUJJ 0 The loop starts at sO then adds k to s 10 times with k increasing each time The end result of this is the number 55 Example Problem 1 work it out in your head sum0 for j 1 3 sum sum jquot2 end Question what is the Value of the sum when the loop is complete b 14 add 01 then 14 then 59 59l4 Example Problem 2 for a1150 disp hi end how many times is hi displayed a 50 hint try and think out the answer with a smaller sequences Example Problem3 for a1150 disp bonj our monde end How many times is bonj our monde displayed b 0 Nested for loop Example 1 for a10l05O for bO0ll disp hello end end How many times is hello displayed a55 Hint b starts at 0 11 iterations a starts at 10 5 iterations 1 15 55 Nested for loop visualization personally this wasn t that helpful but l ll include it for those leamers that might find it helpful In this example imagine someone walling taking two steps each time and then facing a specific direction face the door for i1 2 take one step forward end gt face the door for j 1 4 for i1 2 take one step forward end face left end Overall what you do is Take two steps and turn left four times face the door for kI 3 for j 1 4 for 139 1 2 take one step forward end face left end end Overall what you do is You do loop j and face left 3 times Then you do loop i and face left four times This pattern occurs every time you go through loop k possibly helpfulThink of loops like a hierarchy K the boss and everytime K runs k forces j and i to run as well Since j is above i everytime j runs i must run all the way through loop j Example of how a sum nested for loop actually works in Matlab shown below 2 HOME PLOTS APPS E M 0 Q5I1 139Cquot gtvcuv 2 II EV E E j gt Users gt madelinedippel gt Documents gt MATLAB 39 9 Current Folder I9 ltStudentVersiongt Command Window 9 E i NameA 239 1 assmm E33 iZT 13 9 foroopm for J21L 39 fl matrixprob m 39 sumsum1 end end SUITI 1 sum 2 sum 3 Details A sum Workspace 7 4 NameA V Eans l Ei 3 sum E91quot 3 3k 1 5 3 ocaeStruct 1 as S Esum 6 sum 6 fag EDUgtgt As you can see in the above diagram for every time the program runs through i it also runs through j For example since irepeats 3 times and j repeats itimes there are 6 iterations of the sum function making the total sum 6 Note Don t mess with Loop counters These counters prevent you from having in nite loops 2 HOME PLOTS APPS E 5 E QSea39c I DoCuquot uequotr 139 oi I E E E j I Users gt madelinedippel gt Documents gt MATLAB V 0 Current Folder 9 ltStudentVersiongt Command Window 9 E f quotquotf EDUgtgt n1e 5quot C1a55399quot39quot EDUgtgt for j1n foroopm nn1 J matrixprobm J end 1 gt Details 399 Workspace Name A E ans i E k E Iocalestruct n E s 3 sum mvogtw s 7 fa ln the above loop j goes from ln 110 and n is replaced by the value nl This results in dispay of jlgtjl0 As a side note when writing a program the of ends at the end of a program corresponds to of fors in for loop While Loops 2 HOME PLOTS APPS 0 um U 39 y ya E Q earch Documemanon E i 2 4i E E Ljl I Users gt madelinedippel gt Documents 5 MATLAB V 0 Current Folder 6 ltStudentVersiongt Command Window 3 E E a39 EDUgtgt so Equot 3599quot39 EDUgtgt k1 Qf quot Pquot EDUgtgt while klt5 J matnxprobm 55 kk1 end EDUgtgt disps 15 EDU dispk 6 J3 EDUgtgt Details A Workspace Cv V l 3 1 6 1 0 1 6 These are useful when you don t know how many times the program will run In the above example the program runs until the Value of k5 with k getting larger by 1 each time the program runs Engineering 3 Lecture 4 4914 Module 2 Iteration First it s important to leam about a very special type of iteration The formula XsqrtXl repeated an in nite number of times always converges to a special number know as the Golden Ratio This formula will converge to the same Golden Ratio even if the sqrt of Xl is a complex number It will work for ANY value of X 1 HOME PLOTS APPS E M 9 p E 3 E Q5cac L3rcwlquot39lequotliai cm g I 39E E gt Users gt madelinedippel D Documents D MATLAB V CurrentFoder 9 ltStudentVersiongt Commandwindow 3 E Namet fjasggnm EDUgtgt xsqrt1x fjforloopm matrixprobm x 16181 EDUgtgt xsqrt1x x 16180 EDUgtgt xsqrt1x x 16180 EDUgtgt xsqrt1x X Details A 16180 Workspace 3 Name Vi EDUgtgt x5qrt1x W ocaeStruct 1 I x 1 X 16180 EDUgtgt format long EDUgtgt format Long EDUgtgt xsqrt1x X 1 618034054422319 f5 EDUgtgt 9sThis number is the Golden Ratio al1 numbers in that form converge to this ratio Xkx called a xed point X sqrtlx Xlsqrtl42 number Golden ratio converges to as can be seen below JO1P3 b n HOME PLOTS 4 ti Q E j Users gt madelinedippel gt Documents gt MATLAB ltStudent Versiongt Command Window Current Folder 9 NameA assignm J forloopm matrixprobm gt Details Workspace Plotting O APPS EDUgtgt x6 EDUgtgt yinf EDUgtgt while absx ygt epsx yx xsqrt1x end X 2645751311064591 1 909385060972404 1 705691959578987 1 644898768793687 1 626314474138900 1 620590779357608 g 1 1 RR7RR7EQ Q I E mm 5 3 3 Q E CQ5earch Documentation 7 x0390 0254 Sets an axis in between 0 and 4 with increments of 0025 pI0txxxsqrtx plots the line XX Versus the curve XsqrtX1 d MATLAB WindpL Help 808 File Edit View Insert Tools Desktop Window Help ltStudent Versiongt Figure 1 4 4 s3 Fri 438 PM Q ioupa b a merge it s39s5239 isZ iv 3 DE mg As you can see in the picture of the plot above the intersection of these two lines is the golden ratio as expected OlP b HOME PLOTS APPS E M 5 4 gt QSearcl1Documentation n gt E E j Users gt madelinedippel gt Documents gt MATLAB V Current Folder 9 ltStudentVersiongt Command Window 3 NameA 1 assignm 4 314398832739892e05 forIoopm quot1matrixprobm EDUgtgt xxquot2 X 1861403728794734e11 EDUgtgt xxquot2 X 3464823841570940e22 EDUgtgt xxquot2 X 1 200500425311841e45 EDUgtgt xXquot2 Details A X Workspace 1441201271173911e90 EDUgtgt xx 2 X 2 077061104033297e180 EDUgtgt XXquot2 X Inf fV5 EDUgtgt I In nity in Matlab After awhile matlab cannot compute numbers any more and will simply display the Value inf when a number becomes too high Eng 3 Week 3 Topics Covered Fibonacci and Debugging Review An example of using tolerances Summing k1 gtinf Rquotn 1 rquotn Difference between rn 1 rquotn a finite amount of error To1001 Rlt1 Establishing a tolerance in a program will make the program stop once Matlab has calculated withing a certain range of error the tolerance Using Counters HOME PLOTS APPS E M Q E p x Q5i39cquoti Q 4rLxquotu339iii39 on i 7 II Newvariable Analyze Code Lli W g l B a nd Files ii ad lj Llilll E Preferences 1 ampCommunity Open Variable V Run and Time New New Open L lcompare Import Save Simulink Layout set path Help Addon5 v Script V V Data Workspace gclearworkspace V fgclear Commands V Library V V FILE VARIABLE cone SIMLLINK aux momazur RESOLRCES J Q d E E j gt Users madelinedippel Documents 9 MATLAB V 0 Current Folder Q ltStudent Versiongt Command Window v E NameA 5 l355399quot39m Student License for use in conjunction with courses offered at a H Jf rl 9p39m degree granting institution Professional and commercial use prohibited ff functIonSnm 3 functionSnm EDUgtgt n25 klhW1 p39 b1am EDUgtgt counter10 1hW1 p39 b1b39m EDUgtgt while countergtn J hwz p39 b1a39m countercounter 1 J hw2prob1bm end hW2 p39 b239m EDU dispcounter matrixprobm 4 fa sou Details A Workspace G1 Namet M Ir counter I W Iocalestruct 39 IV n 5 hquotCounter loop The program is done when c4 because the counter won39t satisfy the loop to run again function f triplex f3X end The function written above will display three times whatever input x you put into the function Ex HOME PLOTS APPS IE M 9 5 I L Q5rz c w in rim Ur E Newvariable Analyzecode mi quot l I a m nd Files L Q I 1 E Pref eeeee es 0 mmmm nity Open Variable V Run and Time New New Open Lgjcgmparg Import Save Snmulmk Layout sethth Help Addon5v Script v v Data Workspace clearworkspace v fgclearcommands v Library v V FILE Rl BLE con SMLLINK ENUIRONMENT RESOLRCES 4 L E E gt Users gt madelinedippel gt Documents gt MATLAB 39 9 Current Folder K9 ltStudent Versiongt Command Window 3 NtmeA 2 tlassignm EDUgtgt tr1plex3 3 Efibonaccim ans tforoopm f functionSnm 9 E highm E1 hwlprob1am 155 EDU Q hw1prob1bm 1 hw2prob1am hw2prob1bm lOglSIIC m 393 matrixp b m C tripexm 5 In m l Workspace 1 Name A Value Bi ans 9 EH f 3 Q Iocalestruct 1x1 srruct x 2 EH v 1 This function is coming from somewhere a separate file and you put x into that function For this function function f is the output Exlj x10 y3 w subtractyx Function wsubtract xy wx y end w7 Side Note Division of labor modularization why do we need a subtract function For large companies and large codes they assign different responsibilities to different groups To communicate information it39s a lot easier to share functions rather than variable names 1 a10 b3 wsubtractba w7 Fibonacci and Debugging Fibonacci practice on iteration and functions Let fn of pairs of rabbits At end of month n fn fn 1fn 2 In the Fibonacci sequence the numbers are called Fibonacci numbers This sequence is made up of added terms like F3f2f13 F4f3f25 F5f4f38 Etc A note of interest The limit as the Fibonacci Sequence approaches infinity fn1fn is equal to the Golden Ratio Example Question If rabbits died after 6 months what would the Fibonacci model look like then Fnfn 1fn 2 fn 7 Another type of population model Another type of population model important to programming is called a logistic map Logistic maps are used for various biological species and function like Let the nth population be Pn Then Pn1cPn dPnquot2 You then Rescale the sequence by xndC Pn And result in Xn1rxn1xn Wednesday Lecture 41614 Debugging The bar at the right of the editor in Matlab the Mlint bar is an indication of how many bugs you have in your code Green good Red syntax errors warnings Note Green does not mean your code will create the intended result it only tells you whether or not the code will compile smoothly Logic Errors these errors don39t prevent your program from running but will prevent the program from outputting the correct result Matlab language that bridges from you to the computer which is why you have to be very particular in what you type into a program Syntax error To prevent making syntax errors you need to make sure all your variables are assigned a value Also be sure to assign the output value when making a function Pre allocation Pre allocating is essentially assigning a size to an element f when it changes size following each iteration of a loop function Note Don39t mess with the loop index Example of a way to make a better function In the program k1n fkfk 1fk 2 kk1 kk1 is way complicated and unnecessary to the program Things to Remember Brackets for outputs in functions Parentheses for inputs in functions Use a between to things that are multiplied together 3yinvalid in math notation In Matlab it must be 3y When Debugging a Program follow the short and sweet debugging checklist 1 Check for syntax errors by going through red and yellow error bars on the Mlint 2 Check to see if the program actually makes sense in logical terms Eng3 Lecture Notes week 56 Monday Lecture Goal Think about matrices geometrically Rather than as just arrays of numbers it39s important to think of matrices as tools that change the geometry of lines and points Suppose we have a column vector x1x2 2 O This is the same thing at saying that the vector is 210 101 This means that a vector is essentially a linear combination Scalar multiplication of a vector Let a a scalar numerical value a x1 x2ax1 ax2 geometrically this means that you increase the magnitude of the vector ex if the vector is a line segment of length 1 the length of that vector will increase by a factor of length a Vector Addition a b c d ac db just add values across rows to find your new vector What matrices do to vectors all a12 a21 a22 x1 x2 a11x1 a12x2 a21x1a22x2 old x9 new x both are vectors in the same plane Basically Multiplying by matrix A maps the old vector x1 x2 in the plane into the new vector in the plane In this way you can think of matrix vector multiplication as a linear transformation Inverse of a Matrix invA gives you the inverse of matrix A in Matlab The inverse of a matrix exists if A is not singular so that A A 1IA 1A Remember I the identity matrix If A is a 2x2 matrix A1 can be found using a formula If you have a matrix a b c d then the inverse 1 determinant d b c a The determinant of a 2x2 matrix ad cd Determinant is a bit imprecise because of rounding errors with close numbers This is because as a matrix approaches singularity singular means that the determinant is about 0 the determinant will approach zero producing an inverse matrix that is numerically garbage Matrix condition and norms condA A A 1 gt given this formula uses norms its important to learn what norms are The norm of a vector magnitude of a vector a measure of length Thinking back to linear plotting x1x2 is like a plotted point The Euclidean norm is distance between x1 and x2 x1quot2x2quot2quot12 L1 norm Manhattan norm x1x2 The distance between x1 x2 absx1x2 think you can only walk on city blocks Norm of a matrix a measure of the size of a matrix A maX IIAXIIX The infinity norm is the opposite of the one norm Maximum absxi 1ltiltn It39s basically the maximum sum of all the rows of a matrix Ex if Aan mxn matrix A1 norm maximum from 1lt jltn i1 It39s the sum from i1 to m sum of all aij For any given j we39re taking the sum of all the elements in those columns Then we39re looking at all those column sums and finding the maximum value Now back to conditions The best conditioned matrix is the identity matrix It has a max row sum of 1 and a max column sum of 1 CondI1 the condition of the identity matrix is 1 this is the least singular a matrix can be CondA12 Cond A5 28445 condition numbers for random matrices A1 and A5 are within a reasonable range meaning that neither is verging on a determinant of zero On the other hand a different matrix A6 is verging on singular Cond A6 huge condition reaching towards infinity The condition number is a better indication of singularity than the determinant because it39s more numerically precise and doesn39t give you useless matrices Note It s possible to have matrices that aren39t singular but for practical purposes are singular Special matrices rotate things GOcos0 sin0 sin0 cos0 a counterclockwise rotation of angle theta G 1 must rotate clockwise by an angle theta to counteract G G 10G 0 G01GO2 GO1O2 Wednesday Lecture invA Matlab warning message your matrix is singular to working precision This means the matrix is garbage it39s not an invertible matrix Geometric Transformation on Matrices NOTE for a better visual representation see hand written notes in next section Re ection in y axis 1 O O 1 Re ection in x axis 1 00 1 Re ection about the line yx 0 11 0 Re ection about the line y x 0 1 1 O to figure out a transformation a good trick is to multiply it by x and y to see exactly what it does Example Exercise Find the 2x2 transformation matrix A to transform the vector 20 to 1 0 and which leaves the vector 02 unchanged Answer A12 OO 1 see program in module clc clears the command window avoids messiness involved in plotting close all closes all the current figures so that only the new figure you want to plot will be displayed Great to have at the start of a program clear all removes all variables from the workspace everything needs to be newly redefined Hold on allows you to plot multiple curves in the same viewing widow etc To examine buoy data in the module he plotted a sine graph as a reference point Modeling like this is very useful Then by hand he put in time data of the buoy And next the height data 12pts usual size of text on paper Line width etc in a similar point system xlabel label title is a string ylabel label title title title goes here How to import data Zimportdata data2007 Data2007filename PlotZ9 For files that are csvcomma separated values files you have to use Xcsvread FanGraphsLeaderboardNumericcsv Pause it waits suspends the command window Ex pause5 suspends it 5 seconds Printing things to the command window fprintf gives you more control over what you39re going to print fprintf happy birthday n fprintf my name is s n professor petzold a155 fprintf The sum is e n a Eng 3 Lecture Notes week 6 Linear Systems Goal learn to formulate problems in terms of linear systems learn how linear systems are solved on the computer by the computer Ax Anxm matrix X unknown a column vector b bgivencolumn vector Problem Alice buys 3 apples 12 bananas 1 cantaloupe for 236 3x12yz236 Bob buys 12 apples 2 cantaloupes for 526 12xOy2z526 Carol buys 2 bananas 3 cantaloupes for 277 Ox2y3z277 xapples ybananas zcantaloupes MatrixA 3 12 112 O 2 O 2 3 X X y z b 236 526 277 Axb To solve for x in Matlab use Ab this is like dividing b by A although division doesn39t actually exist with matrices Z HOME PLOTS APPS P Hui dz D 27 V QSearch Documentation E j 8 L 3 Find Files ii a Sew variabrl l Anayz c de LE Preferences T p T Community v 39 v R T quotW New 09939 COMPIYE quot P 59 E n wise P 3quot med Sirzulink Layout sgtpath Help ampA don5 v Iscrlpt V V I Data Wor pace QC rwor pace V I C rcomrnan s VI Ll rary I V I V I I FILE I VARIABLE I CODE I SIMULINK I I nesounces 4 I I E I gt Users gt madelinedippel gt Documents 5 MATLAB ltStudent Versiongt Command Window Joupa b 0 0500 0 8900 EDUgtgt Ab ans 02900 00500 08900 EDUgtgt Ab ans O 2900 0500 0 8900 O EDUgtgt A3 12 112 0 20 2 3 EDUgtgt b236526277 EDUgtgt Ab ans 0 2900 0 0500 0 8900 I35 EDUgtgt I Current Folder Workspace Example How many solutions does x have Z HOME PLOTS APPS 0 W J ltE g E QSear ci1Documentation E L E Find Files Ii l New variable 9 Anawze code LE Preferences P Community 0 V bl V R dT39 New New Open compgrg Import Save E pen ana e amp quotnan me Simulink Layout Esgtpath Help ampAddon5v Iscrlpt V V I Data Workspace Qclearworkspace V I Clearcomrnands VI Llbrary I V I V I FILE I VARIABLE I CODE I SIMULINK I I nesounces I I E I gt Users gt madelinedippel gt Documents 5 MATLAB ltStudent Versiongt Command Window EDUgtgt A1 22 4 EDUgtgt b12l EDUgtgt xAb warning Matrix is singular to working precision I 14 Joupa b X NaN NaN fa EDUgtgt Current Folder Workspace Answer an infinite number of solutions NaN means not a number and has to do with infinity Example problem The distance along the river from Rheinfall to Rheinau is 8 km The trip downstream from Rheinfall to Rheinau takes 20 minutes Vwater Vboat2O The trip upstream from Rheinau to Rheinfall takes 40 minutes Vboat Vwater40 Question how fast does the boat run on its own How fast does the river Rheine ow X1 boat speed kmh X2river speed kmh 20 min13 hour 8km 13 h 24kmh total speed 40 min 23 hour 8km23h 12kmh total speed x1 x224 x 1 X2 1 2 x 1 1 8km h x26km h Ex of solving this in matlab 5 HOME PLOTS APPS E M Q 51 jg rr Q52d39Cquot D uLquot39nquotiiai E E l D lEFind Files l naNewvariabe ljllnalyze Cfde E E Preferences PX fgcommunity New New Open t JComparg Import Save Qopenvanablev ampRunand me Simulink Layout SgtPah Help AddOnsv Script V V 39 Data Workspace Qclearworkspace V gCear Commands V Library V V 39 FILE VARIABLE CODE SIMULINK ENVIRONMENT naouaca l N 11 39 E j gt Users gt madelinedippel 9 Documents MATLAB V 0 ltStudentVersiongt Commandwindow 3 E EDUgtgt A1 11 11 5quot EDUgtgt b2412 EDUgtgt Ab ans Z 18 6 fr EDUgtgt Current Folder Workspace Problem A boy commutes to a university taking a path that is 5 miles from home X bike y walk zbus He uses three modes of transportation to try and get there It takes 48 hours to bike to the bike rack near his class He takes 01 hours to walk from the bike rack to class 48x1y0z5 It takes him 01 hours to reach the bus stop at school It takes him 05 hours to walk to class 0x 05y01z5 It takes him 01 hours to reach the bus stop by bus It takes him 008 hours to bike from the bus stop to the bike rack It takes him 01 hours to walk from the bike rack to class 008x01y01z5 Compute speed of each mode of transportation A48 1 00 05 01008 01 01 B5 5 5 Find x y z where x speed bike yspeed walking zspeed bus Solve with xyz with Ab Bike 10mph Walking 2mph Bus 40mph P1x B0 B1X How do we find B0 and B1 P1x1fx1 P1x2fx2 b0b1x2 As a matrix equation A X b 1 X1 1 X2 B0B1 fX1fX2 Now if you want the polynomial to go through three points P2xB0B1XB2Xquot2 Then you need three corresponding Fx points Fx1Fx2Fx3 With this information you can build a matrix just like before In the homework the function canon will give you a bunch of different x and y values It will give you and observation of where the cannon and where the explosion occurred Higher polynomials are trouble so instead you should try and fit a second order polynomial through all of the points P2xiBOB1xiB2xiquot2 M 1 x1 x1quot2 1 x2 x2quot2 1 x3 x3quot3 1 xn xnquot2 BO B1 B2 yy1 y2 yn this is essentially minimizing over the Betas the norm of AB y this means that this function is finding the polynomial closes to the points by measuring the size of the difference Lecture Wednesday 5714 How does the backslash operator work It solves a linear system through Gaussian Elimination Reduced Row Echelon Form 1 12 2 x1x22424 same as x1x224 2x12x224 Solve by multiplying the second equation by 12 Then you can add the equations together to cancel out x2 Then you get 2x136 and can solve for x118 Then you can solve for x26 by back substitution What you did above to eliminate variables was about what you want to do to solve a matrix by row reduction Matrix row reduction is essentially the process of adding rows together and multiplying rows by scalars in order to solve for a specific variable in the matrix Key ideas Goal reduce the matrix to an upper triangular matrix an upper triangular matrix has all zeros below the diagonal of the matrix by performing row operations that don39t change the solution 3x12x2x31 2x12x24x32 x112x2x3O Matrix 3 2 12 2 41 12 1x1x2x312O To reduce the matrix to upper triangular You can multiply the third row by two and add the second row to it to get a new bottom row of 0 02 with 2 as the solution to that row Next you can multiply the second row by 3 2 and add the first row to it to get 0 5 7 as the new row with 4 as the solution Now you have an upper triangular matrix and can then solve for all three variables x1x2x3 directly Check and see for yourself how exactly this geometry looks by writing it down on a piece of paper Matrix inverse v backslash operator AinvAI To compute matrix inverse A Ainverse with n columns I of n columns For this you have to solve n linear systems to get A inverse This takes way too long when you have large matrices and gives you a lot of rounding errors so it shouldn39t be used Lecture 51214 Probability 6 coin ips of heads and tails with three heads and two tails 10 combinations How can we figure this out using probability 5 possibilities slots for the first head 4 possible slots for the second head 3 possible slots for the third head gt60 possible slots this is wrong however because it counts the same things twice This is because you can rearrange each of the distinct heads three times Meaning there are 6 ways you can rearrange each of the three distinct heads between the three same slots You can then get the actual number of combinations by dividing 60 by the number of repeats 6 9 this gives you the correct answer of 10 separate combinations Formula for the above example we had 5 possibilities choose 3 instances 532 53 5 3 generic form nk nkn k In Matlab there is a function for this nchoosekNk There is also a function that allows you to compute the factorial factorialN Probability of Flipping Coins of ways to have 6 heads out of 10 ips 106 1064 210 possibilities Nk gt N sometimes called the binomial coefficient Ex xyquot2 xquot2 2xyyquot2 to the coefficients of the binomial 22 21 20 This means that probability can be used when performing binomial expansion The combination of al these binomial coefficients can be found in Pascal39s Triangle Probability of an event occurring For example when ipping a coin five times each time you ip it there are two possibilities That means that the total number of possibilities 2quot432 Going back to the first part the probability of three heads coming up 1032 Let Plim as N gtO0 nN n of times a particular kind of event happens like three heads in 5 ips N oftrials 5 ips Remember A probability p is always between 0 and 1 Example Suppose we ip a fair coin 10 times what is the probability that it comes up as heads all 10 times n1 only one combination of it coming up all heads N2quot1O number of total possibilities as before P12quot10 Example what is the probability of heads coming up six times in 10 ips 106 2quot10 the same as 10 choose 6 over all the possibilities 2quot10 These events are independent or mutually exclusive because the probability of an event occurring is not affected by the events that occurred before it For mutually exclusive events the probability is the sum of all the probabilities of the individual events Problem There are two bears one black one white 1 If you are told that at least one of the bears is male what is the probability that both are males 2 We are told the white bear is male what is the probability that both are males 1 13 The possibilities are MF FM and MM There is only one that is MM so the probability is 1 3 2 12 The possibilities are only MF and MM this time since a specific bear is known to be male Now there is only a 12 possibility that both are male Poker 52 card deck 4 suits hearts h clubsc diamondsd spadess In each suit there is an acea a kingk a queenq and a jackj then the numbers 10 gt2 The straight ush contains five cards in sequence note5432a also a sequence All cards must be in the same suit How many possible straight ushes are there 10 4 40 possible straight ushes 4 ways to choose a suit 10 ways to chose a sequence what are the number of possible distinct hands 52 distinct cards 5 cards in a hand 9 52 choose 5 distinct hands 2598960 Probability ofa straight ush 402598960 00015 chance Four of a kind Ex 5555Aetcetc How many ways can you generate four of a kind 13 choices for the repeated card 48 choices for the final unrepeated card 1348 ways to get four of a kind 0024 of getting a four of a kind Wednesday Lecture 51414 rand produces a random number in between 0 gt 1 Rand is uniformly distributed which means that the numbers rand produces really are completely random hist produces a histogram it39s like a bar chart where inside of each interval it tells you how many things fall into each number interval Ex a histogram of rand1000 will tell you how many numbers out of a thousand fall into each interval from 0 to 1 Poker Continued Full House three of a kind two of another kind 13 choices for the value for 3 of a kind can be of different suits 4 choose 3 of suits 4314 12 choices for the value of 2 of a kind 4 choose 2 of suits 4226 O14 A Flush not including a straight ush ofstraight ushes40 5 cards of the same suit 4 suits 13 possible cards for each suit gt 13 choose 5 Multiply it all together 413choose5 40 number of possible hands 0196 Straight A numerical sequence of cards AKQ10987654321A There are 10 ways to go down this list 10 possible values for straights You can also choose a suit 4 ways to choose a suit for each card 4quot5 ways to choose suits alone 039 probability of getting a straight Three of a Kind exactb Choose the Kind 13 choices Choose the suits 4 choices 4 3 possibilities Two more cards that can be anything 48 cards we can choose gt 48 choose 2 In all probability 211 chance of getting a three of a kind Examples of Probability Programs in Matlab 45 5 Sec gg may 3 8quot3quot J13 z5o gt5 e M 3 En quot 5 K FZ OF X O 5 lJh k bulb m Z Ones 3 Pfzco 4 cu bk C0mb nqqtong AL mLo ll choose 1 H i 7 7u771 3 dao 395 W 339lt 39 ca EWQ3quot N am own 3 mwagswwtt ltc Mcc3 quot 7541 p 0bQbiP UoF boggy 0JT 0 U elf quot 320 H 7 B06 T Saran Nd mmrucs YOK drum 393 rqndomh 1 ti PCCLU KT Q r 395 25 amp cP Ss39UQ O39Ylb1Qlm 30 3 Pfa jftn p V O T if L quotV391 rt mm Jt cicn schquot Or 0055 h fquot394H b L quot3396rlt39 3 563 cog quotquot quot ooodm ultuJltLUe gig cs x L 0lffvl W 5N Wye In Mm h h 2 3993 Ju 39uoJiv0 9quot d WW 1M1 oewuwtuz U 91 00613 Tova Q solar bu voam an mun EV on mam 939 39 3 K 0quot C rofV l p H Mm ODE d 9 3 X Mint Nugt9an N a 4 39 quotF L cmwons 3 5 0 fauna range 6 NC Mk ODE Mk main 7 d tuq 5 vector S39 Loco3 inzlzal Cadmons gt olt03 1 all ODE 23 W4 7 ODE13 Q Ftzmm ispanms I1zn lg I 9mbabiaF3 Dsh4zmm vial For u5Hmmc 0 2 03 dc f ro5agt39Mj Nb 4vhchon P 39 M A quotV399C quot NF JHRS s po gtqbil5quot3 Kr 4 s Sctic random UY1h39ab I5 Qmtffy omQ unluc gxw K00 C392ze lt K S saw Seam 7500 7 0V K 63 F E 3 mu x 1 Sum cm for an die n1 oc rugrlkfn S i 1l gtquot gtJ5 xx q C3 Y kc A0 U 3039quot X C 0 S o XC S f 5wag D539 hon pK Mv39prC1P Iu 11 0 Objcdd V 39lt it all csoscnobecs It 5 P 9f bq39os a on It on 39gtWus5FvI quotd quot39 39 f to ma 39 9ob on gol ng km 9 O k 9K P P cr 5 LL to1 quot Nchoou 7 b nwmqt coq 39 m on A wt L LUJ3 38 L23 p 339 3 lo L 5 Iquot c Kquot P P UH 0 K Ito NlO39 for quot3913910 Pu 39 1CquotOO LN 394 39539A 3quot Uquot39 r mo 16 VCl p Probabalgl 1 M 3 of cws5 gtm dnrQ gt I I I I K I n5 h Ppuu39r39 if of beds porsson Du rEluHO1 Y 11 91 k Zxtefl Le 10 0quot 5 E OHIZ 39 39 9rob 09 lo rues Pu ing h IhqrDg W q Car 911 mi Pvb Xq Lzlo I I 1 1 goeg Y 51 Vim D39mltrmh J Qgttakmb 056 bum Kat relak wmown Fondiors 4e M Jmuxdwcs J WC 3 5 X 4 Soln lo a d 3quotcmq uDuqHon 5 a Ivnchcn gum satasflizs 3 bot mg I mmm Fwhm wed 4o 3 Jmouave soln KIA PC X 9 la 2 gt d z 0 lt y dr 7 3 Eng 3 Week 8 Lecture Monday 519 Baseball Basic rules 9innings 3outs in each inning per team Probability of scoring a run in an inning is 02417 What is the probability of a 59 inning long streak with scoreless innings this means no scores from the other team Probability of no runs scored in an inning 1 2 5417 Probability of scoring 59 consecutive scoreless innings 1 025417quot59 In 1941 Ioe Demagio had a 56 game hitting hit had at least one hit in 56 consecutive games At the time his batting average was 357 357 1000 times he would get a hit He comes to bat about 4 times each game Assume that at each at bat each hitter only gets a hit or an out Conceptual waIkthrough of the problem Probability of getting at least one hit in 56 straight games in a 154 game season Count0 of seasons which set the record with a 56 game hitting streak Loop over the of trials of seasons Current streak Loop over 154 games Determine whether we get a hit in the current game Prob of hit in 1 AB 357 AB stands for At bat Prob of not getting a hit 1 357 Prob of not getting a hit in 4 ABs 1 357quot4 Prob of at least 1 hit in a game 1 1 357quot4 If get hit Current streak current streak1 else current streak0 end if current streak56 countcount1 break end Actual Code H C C 6 Editor UsersmadelinedippellDocumentsMATLABhittingstreakm cUneJa 0 EDITOR O A1al1a Ill D Q Ea nd Files Insert a fx V lt3 W ERun Section O lLEJCompare V Comment 39 fig 3O I gt GoTo V 5quot New Open Save Breakpoints Run Run and Adyan e Run and V V V G3Print V lndent N g nd V V Advance Time 4 FILE EDIT NAVIGATE BREAKPOINTS RUN 9 Curr l headstailsm l probsm l hittingstreakm l l r Q 1 function probhitting5treakbattingaverage trials I E 2 count0 E 3 for m1trials E3 4 currentstreak0 23 5 for j1154 6 atbatsrand41 7 hitscurrentgame0 for i14 if atbatsi1ltbattingaverage hitscurrentgamehitscurrentgame1 end end if hitscurrentgamegt0 currentstreakcurrentstreak 1 else currentstreak0 end if currentstreak56 countcount1 break end probcountt rials mawmmmmaammmmammmmmmm hittingstreak Ln 17 Col 12 Birthday Suppose that N3 364365 prob that the second person has a different birthday 363 365prob that the second person has a different birthday Prob that all three have different birthdays is 364 365363 365 What is the probability that at least two share the same birthday 1 364365363365 Eng 3 Week 9 Section 52814 ODES Example Set Up x 5x xO ux Vx V x 9 x 52x 12x Then substitute in u and V 1 V 52V 12u 2 vu A case involving Springs Sum of forcesma Spring system force on spring Fs kx xposition of spring kspring constant There can also be a damping force caused by friction Ftbx b damping coefficient x velocity of blockmass attached to spring somabx kx mx bx kx you can then rearrange this in the form mx bx kxO You can then change variables to solve ux Vx u V x V bmV kmu u V Pictures of this in action shown below Q MATLAB Window Help 8 8 8 File Edit View Insert Tools Desktop Window Help D a w5 SlA ltStudent Versiongt Figure 1 39f39iEEl G 9 gt3 15 1 If 5 Fl quotquotquot I I 1 U I I I L I M l I I If I I I II I I 1 5 quot 0 I 1 2 25 1 4 position 89 wed 234 PM Q Q MATLAB Window Help Q 9 gt3 88 Wed 241 PM Q E C 0 8 ltStudent Versiongt Figure 2 File Edit View Insert Tools Desktop Window Help D m a K 39w3 J E E39l39 IE EIIEII E E 3395 I I I I I I I I KE I PE El TotalEnergy I 3 I 1 l I l 1 1 I I 25 39I I I I II 2 II 39I II I I I 39I 15 l I I ll 1 x I 1 I I 39I 1 1 I I 1 i 5 l I l I 39 ri 39 I 05 I r A 1 It I l I IL I x I I J I x o I I I I I I r 39 4 1 2 3 4 5 5 7 3 9 Homework 8 Notes For the Pendulum question Go to math works file exchange to find the function for the pendulum Search peakseek and find it 1ocationpeakpeakseekP Wednesday Lecture 52814 0DEs Coffee Problem How long does it take coffee to get cold T temperature of the coffee in degrees F Taambient room temperature dTdt kT Ta kheat transfer coefficient t time T0180F T0200F extra hot Ex Function DE function TdotcoffeetemptT Tdot is the rate of change of the temperature of a cup of coffee at temperature T given a room temp of Ta global kcup Ta TO Tdot kcupT Ta end Main Program coffee main program global kcup Ta TO kcup is a positive constant depending on the cup s materials and shape kcupO25 Ta70 set temp of room in degrees Fahrenheit TO200 this is the extreme highest temp an espresso machine can produce set length of timespan in minutes tspanO 30 tout youtode23coffeetemptspan T0 plottoutyout youtend p Q MATLAB Window Help is I3 3 43 4 73El Wed 400 PM Q 55 6 0 8 ltStudent Versiongt Figure 1 File Edit View Insert Tools Desktop Window Help 339 U lI ks s siquot39quoti3 Eo TEL DE El 200 I80 I60 140 I20 IIJIJ I V v v39J39w m5 l V i 7 7 A 7 Temperaturev Time graph of Coffee Most k values for coffee cups aren39t made public However we can easily find this out through experimentation Suppose the coffee temp went from ZOOF to 175F in 1 minute Initial Conditions TO20O T1175 Create a function called coffeetempdiffk solution of the ODE for coffeetemp at a t 1 minute 175F We can then guess a k and then substitute in through guess and check to solve But we can also solve it using Matlab We want a k that makes the difference to O fzerofinds the k for which fis equal to zero coffeeinverse Dyoutend 175 difference equation Other than this the coffee function file remains almost the same as the function shown before The only difference is that the timespan is now 1 minute instead of thirty Eng 3 Week 10 SSMS 13011303 Location of the final Need picture ID Room assignments based on last name Topics for Final Iteration not recursion Debugging Plotting Linear matrix Probablilty ODEs How to write a 2nd order ODE into a 1rst order system Simplest Method to solve ODEs Numerically Euler39s Method dTdt kT Ta lim Ateo Ttquott TtAt dTdt Ttt Ttquott kT Ta Ttquott Tt kT Ta At Tn Tn 1 quottk Tn Ta This is a first order accurate method Some Useful ODE Solvers ODE 23 2 gt3Fd order polynomial ODE 45 4 gt 5th order approximations ODE 23s stiff system good for a process with two really different rates fast slow etc toutyout ode2 3 sirtspan yO In this homework the yout length of yout matrix of times 3 columns S I R plot the population using a constant line Write an ODE Solver X1 X1 X2 10X2 tquot22t tspan05 XO11 XO21 h step size001 Springs K stiffness of a spring eqequilibrium M mass on end of spring Force pulling on spring Fx kxxeq Fma mx kx xeq 9 x kxxeqm Letx1x Let x2x1 this means that x2 velocity x2 x1 kx1 xeqm how you rearrange a differential equation to suit a solver Needs initial position and velocity to determine the solution In the solver X1 x2 X2 kx1 xeqm X1OxeqA X20o Examples of spring problems Undamped Spring spring mass function xdotspringmasstx global k xeq m b Xd0tX2kmX1Xeqll must be a column vector end Undamped Spring main file main file for spring mass global k xeq m b k3 b10 xeq1 m1 xO15O tspan0 10 tx ode45springmasstspanxO figure1 p1ottx1tx2p1ots position and velocity V time figure2 p1otx1x2 p1ots velocity versus position Q MATLAB Window Help 393 9 A8 S4 29E Mon434PM Q E W 8 O 8 ltStudent Versiongt Figure 2 File Edit View Insert Tools Desktop Window Help 8 Baa5 m St E 3 IE so Velocity V Pogtion k 339 MATLAB Window Help quot3 0 434 29El Mon43S PM Q 55 8 0 8 ltStudent Versiongt Figure 1 File Edit View Insert Tools Desktop Window Help DEBe3 ks w15 3 i3 l3o39 1 DE TE quot5 I I I I I I I I Position Velocity v Time Damped Spring Motion function xdot springmassdampedtx global k xeq m b Xd0tX2 bmX2kmX1Xeqll must be a column vector end main file for spring mass damped global k xeq m b k3 b10 xeq1 m1 xO15O tspan0 10 tx ode45springmassdampedtspanx0 figure1 plottx1tx2plots position and velocity V time figure2 plotx1x2 plots velocity versus position MATLAB Window Help 9 9 gt8 Z 4 29E Mon 436 PM Q 8 O 6 ltStudent Versiongt Figure 1 File Edit View Insert Tools Desktop Window Help D a Fa R x i 3 I IZIv EL EIEI DE Position Velocity v Time d MATLAB Window Help 399 C9 gt3 4 29El Mon436PM 0 as C 0 9 ltStudent Versiongt Figure 2 File Edit View Insert Tools Desktop Window Help 8 D ai Pa R 39s iquotJ I IZiIv IE2 EIEI DE I I I I I 05 o 05 1 15 2 I I I I I 235 I 05 o Velocity V Pogtion Wednesday Lecture Lorenz equations X a yX Y ybxxz Z czxy Graphing Chaos 3D graph looks like a small butter y The Symbolic Toolbox used for solving problems symbolically syms x real diffcosx negative sinx solves the problem symbolically dsolvesolves a differential equation A baseball problem mo
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'