Class Note for MATH 215 at UA
Popular in Course
Popular in Department
This 4 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Arizona taught by a professor in Fall. Since its upload, it has received 13 views.
Reviews for Class Note for MATH 215 at UA
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: 02/06/15
An Introduction to Matlab Part 3 This lecture assumes that you have already worked through part 1 and part 2 You should be able to use many basic Matlab commands and use Matlab as a calculator on scalar variables You should be able to create and execute a script le This lecture covers 0 Creating row column vectors 0 Vector operations 0 Applying functions to vectors 0 Component wise arithmetic Creating vectors This section goes through creating row and column vectors by typing each element by using patterns and by using a few built in functions 1 Typing in 39Lveetors earpl39ict y Here we learn how to type vectors a l C Open Iatlab If you already have it open type clear all in the Command Window You may do everything here in a pt le or on the Command Window 1 would suggest simply using the Command Window for now so you donquott have to rerun you le everytime we make a change Let us create a row vector consisting of the elements 135 and 7 To do this we use the bracket notation H We assign this vector to the variable u u 1357 Instead of using commas try simply inserting a space between each element Type a 1 3 5 7 Note that they are the same thing Also note that this is a row vector Matlab prints out the numbers in a row Let us create a column vector consisting of the same elements 1 3 5 and 7 For a row vector we put either spaces or commas in between elements For a column vector we put a semicolon between each element Note that a semicolon is also used to supress output but Matlab is smart enough to know that a semicolon within brackets I means to change rows Type 390 1f57 Note that Matlab displays the elements of v as a column 2 Typing in 39Lveetors using a pattern We create vectors which are large and correspond to a distinct pattern a l Suppose we want to create a row vector u that contains elements 1 through 100 ie u 1 2 99100 How can we do this Well you can type in each element but that seems awfully illy Matlab has a nice way to do this Type u 11100 This notation means it goes from 1 by 1 to 100 Create the vector u 13 5 7911 using this notation How do you think you would create the vector u 15 12 9 6 3 0 Well it goes from 15 by 73 to 0 So we type a 15 40 We an also use negative numbers Create a vector that goes from 715 to 0 by 3 Note that using the notation above a 1530 created a row vector Let us say we want a column vector instead The easiest way to do this is to use the transpose operator which is the single quote Type a 1 5130 v W What happened Well u is created as a row vector then 1 is the transpose of u which is a column vector We could have simply done this on one line also by typing 1 15zJz0l Try to create a column vector that goes from 2 to 16 by 2 3 Creating a zero vector one vector or random vector Here we discuss how to create a vector of all zeros all ones or of completely random entries between 0 and 1 a l To create a vt or of all zeros we use the zeros function It takes two for now arguments These represent the size of the vector you wish to create Type u zeros 51 You get a column vector with 5 zeros Try to create a row vector with 10 entries To create a vector of all ones we use the ones function It has the same structure as the zeros function v ones16 You can also create a vector with random entries these random entries are randomly drawn from betweenO and 1 using the rdnd function u rdnd 41 Vector operations This section goes through basic vector arithmetic and how to create new vectors using parts of old vectors 1 Vector addition and scalar nmlt39ipl39icdtion We learn how to add vectors and scale vectors a l Type clear all Create three row vectors u v and w by doing the following it 1 3 5 7 v Ml2 2 4 6 8 10 Add vectors a and v by typing or U W39L ote that your result is another vector We could assign it a varible by typing r um Now add v and v7 What happens We get an error because they are not the same size Recall that we can only add vectors of the same size Create a new vector v1 that is the transpose of v again by typing v1 v 7 Try adding v1 and v What happens An error again Why Because row vectors and column vectors cannot be added together How do you suppose we multiply a vector by a scalar Well exactly how you would expect Let us multiplyw by 2 by typing 2w Find 7m and e21 by doing the same 2 New vectors from old vectors Here we ll learn how to incorporate an old vector as part of a new vector or how to extract part of an old vector a l 39W quotll use the same u v and w from above so recreate the vectors if need be Let us say that I only want to look at the 2 element component of vector 1 How might I do this Well vectors are indexed from 1 until their size 4 in this case and are accessed using parenthesis So typing v2 returns the 2 element of 1 Use this idea to nd the 15 and 4 components of 1 Let us say that I want more than one element of 1 Say I want to get a new vector that is the same as 1 but without the 2 element So Ivant a new vector call it v1 that is size 3 an contains the 15 3Ml and 4 components of v I could type 3901 M1 W3 W4 but for very large vectors this will not be feasible Another way I could do this is by typing d 0 1 1 Ll1 3 4 which says take only the parts with index in 1 J 4 This is still a bit di icult for large vectors Luckily we can use the colon notation like when we de ned it 15 0 So I can instead type 1 v1 34 Create a new vector 39r that goes from 3 by 3 to 99 From this vector create another vector 7 1 that contains the rst 3 elements of 7 the 15 element the 18 element and the last 4 elements elements 30 through 33 We could also take elements out of order For example using the 7 de ned previously lets say we wanted a new vector r1 that was the last element 33rd the 10 element then elements 5 4 3 and 2 You would do this exactly as expected 1 39vJJ 10 512 Now let us consider the problem of enlarging a vector rather than taking part of a previous vector Let us create the vector t 012468 by taking 1 0 124 and adding 6 and 8 to the end There are two wz V39s to do this i Simply de ne it 1 then t5 6 and t6 8 This takes previously unde ned value of t and de nes them thus enlarging it Try this De ne a new vector that is exactly what you want 1 with a 6 and 8 tacked on t 068 Use this idea to create a new vector at 72 0 1 2 4 10 by adding 72 to the beginning of v and 10 to the end You can also combine two vectors by doing something like 5 at Note that we have used row vectors but all of the above operations hold for column vectors as well The difference is that notation such as 5 at will not make sense if v and t are arbitrary column vectors Instead we d want to use 5 v t to make sure the rows from it appear after the rows from 1 ii Applying functions to vectors As most of you have seen in your classes there are many useful functions that apply to vectors For example one might want to know the norm of a vector or the dimension of a vector In this section we use a handful of these functions These are by no means all the useful functions but just a small subset 1 Functions on a single veeto39r Here we discuss functions that tak a single vector as the argument or apply to single vectors a Type clear all and de ne a 101 12 001 6 1 z 1 z 10 and d 2 z 2 z 20 b Recall the norm of a vector 1 11 In is de ned as 7 2 Hz 7 x11quot39 The Matlab function for the norm is oddly enough norm To nd the norm of c type nonMe II sing norm we can create a unit vector simply by dividing a given vector by its norm Create a vectorv that is a unit vector in the same direction as d c To nd the length or size dimension of a vector we can use the functions length or length returns one argument while returns two Type n m k lengthe Notice the difference between the two functions 2 Functions on multiple vectors Here we discuss functions that take multiple vectors as arguments a We will be using the same vectors as previously so rede ne a 125 and d if necessary b Let us start with the dot product Recall for two vectors of the same size 0 d is the sum of the product of each individual component As you may gue s the dot product function is dot Try doted What about dotoe What happens c If two vectors are in R3 then recall that we can de ne the cross product which gives a third vector perpendicular to both vectors Try epo55ob What happens if you try e39ro55ed 3 There are numerous other functions We ll just list some here Try them out and type help Funet39ionNome for more help a Find minimummaximum m39ine mow e b Calculate absolute value ob51 0 1 c Calculate the sum of the entries sumo d Calculate the product of the entries p39rool e Component Wise arithmetic We can also manipulate vectors using component wise arithmetic The reason this is in bold print and gets its own section is because this is a very hard concept for beginning Matlab users but one that is essential to fully understand Matlab Let us say that I want to square the entries of the vector d How can I do this Well try ol 2 What happens It doesn t work why not Because I cannot multiply at times itself because that type of vector multiplication makes no sense Matlab has a way to take care of this We add a period before the operation Ti 0 2 What happened Matlab applied the operation 2 to each part of d That s what the meant I sing the same idea we can multiply each entry of d by the corresponding entry of c using 0 e Again this means multiply each component of d to the corresponding component of a Note that this only makes sense if c and d are the same size Create a new vector that contains the entries of 6 divided by the entries of d
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'