1 / 38

Lab 3

Lab 3. MBAC 311. Lab Setup. Within your private mbac611 folder create a lab3 folder. Copy the file lab2_vars to your lab3 folder. Start Mathematica Enter SetDirectory [“h:mbac311/lab3”] into the Mathematica notebook and press the evaluation key. Loading The Variables.

onawa
Télécharger la présentation

Lab 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 3 MBAC 311

  2. Lab Setup Within your private mbac611folder create a lab3 folder. Copy the file lab2_vars to your lab3 folder. Start Mathematica Enter SetDirectory[“h:mbac311/lab3”] into the Mathematica notebook and press the evaluation key.

  3. Loading The Variables In our last lab we saved the fares and header variables values to disk so that they could be used in this lab. To restore these values enter the following expression into the Notebook: <<lab2_vars

  4. The output should appear as follows: Notice that it only displays the header list. This is because we saved the header variable last. When restoring values Mathematica displays the last value saved. To verify that the fares variable has been restored you can just type fares into the Notebook. Try it now. You should see the following output:

  5. Part Function Shortcut List manipulation is very common in Mathematica. As such the Part function is needed frequently. Mathematica provides a shortcut for the Part function. Instead of entering Part[list,i,j] you can enter list[[i,j]].

  6. Enter the following expression into the Notebook: fares[[1,1]] The output should be: Note that this is the same output you would get if you entered the expression Part[fares,1,1]. Try it. Essentially this shortcut allows us to omit the word Part and only specify the arguments. Enter the following expression into the Notebook: fares[[1,3;;25]] The output should be: This expression displays the 3rd through 25th element of the first sublist in fares.

  7. Other Examples You can use this shortcut anywhere you would normally use the Part function. Enter the following into the Notebook: Total[fares[[1,3;;25]]] You should see the following output:

  8. List Element Assignment We can use the shorthand to assign elements to various positions in the list. As you may recall the last element in all of the station lists is just white space that we can’t really use. Lets assign the last element in each list to a zero. Enter the following expression: fares[[1,26]]=0

  9. Set it to zero. The 26th (last) element of the first list. Select first element of fares (which is a list) To verify the change was made enter the following expression: fares[[1]] You should see the following output: Notice the last element of the list is now zero (replacing the white space)

  10. Lab Assignment 1 Note the zero at the end of each list 1) Write the expression to set the last (26th) element of the second list of fares to zero. 2) Write the expression to set the last (26th) element of the third list of fares to zero. To see if you correctly implemented the previous expressions enter the following expression: fares[[1;;3]] The resulting should look like the following:

  11. Do Loop We could conceivably change the 26th member of every list in the manner of the previous exercise. However a much better way to accomplish the task is using the Do function. The Do function allows us to implement what is called a loop. A loop allows us to execute the same function several times, each time on a different member of the list.

  12. Enter the following expression: Do[fares[[i, 26]] = 0, {i, 1, 20}] You won’t see any ouput resulting from the execution of this expression. However, now enter the following expression: fares[[1;;20]] You should see the following output: Notice that the last (26th) element of each list now contains a zero.

  13. The Do Function The Do function takes two arguments. The first is called the body the second is called the iterator. The arguments are separated by a comma. Second argument: The iterator specifies the variable and the variable’s min and max values. The variable i will, in turn, be assigned to each value starting at 1 and ending at 20. Each time the variable i is assigned a new value the body will be executed. First argument: The body identifies the expression that you want to be executed a specific number of times. Notice that the body references the variable iwhich we have not previously defined.

  14. The Do function first sets the iterator variable i to 1 then executes the body. The variable i is then assigned to 2 and the body is executed. This continues until i is finally is assigned the value 20 at which the body is executed one last time and the Do function is complete. Essentially our Do loop is executing the following expressions (the body) by changing the value of i: fares[[1,26]]=0 fares[[2,26]]=0 fares[[3,26]]=0 fares[[4,26]]=0 fares[[5,26]]=0 fares[[6,26]]=0 fares[[7,26]]=0 fares[[8,26]]=0 fares[[9,26]]=0 . . . fares[[19,26]]=0 fares[[20,26]]=0

  15. Lab Assignment 2 Enter the following expression: Length[fares] As you will recall the Length function returns the number of elements in a list. In this case it returns the number 465. So there are 465 lists contained in the fares list – we need to change the 26th element of each of these to zero. 1) Write the Do function that changes the 26th element of all lists within fares to zero. You can start the iterator at 1. Hint: The max value specified in the iterator can either be a number or another expression.

  16. To verify that the Do function was correct enter the following expression: fares[[450;;465]] As you will recall this expression displays elements 450 to 465 of the fares list. The output should be: Notice that the last element (26th) element of each list is zero

  17. Total Function In our last lab we looked at the Total function. This allowed us to add up members of a list. Enter the following expression:fares[[1]] This displays the first list in fares. We would like to add up the Metrocard usage values which are in elements 3 through 25. We won’t include element 26 which we set to zero.

  18. Enter the following expression:Total[fares[[1,3;;25]]] This expression adds up elements 3 through 25 of the first list in fares. The output is: Since we may wish to eventually use this value lets store it into the 26th element of the first list (which is currently zero). Enter the following expression: fares[[1,26]]= Total[fares[[1,3;;25]]] The verify that the list was changed enter the following expression: fares[[1]] The output should be: Note that the 26th element is now 160130

  19. Lab Assignment 3 Modify the previous expression such that the 26th element of the second list is assigned the sum of elements 3 through 25 of the second list. Modify the previous expression such that the 26th element of the thirdlist is assigned the sum of elements 3 through 25 of the thirdlist.

  20. Enter the following expression to verify that you correctly modified the fares list: fares[[1;;3]] The following output should appear: Note that the last (26th) element of each list contains the sum of elements 3 through 25.

  21. Lab Assignment 4 More specifically we modified the two 1 values that appear in the expression. When we wanted to change the second list we changed the 1 to 2. When we wanted to change the third list we changed the 1 to 3. 1) Write the Do function to change every list in fares. Every list in fares should have its 26th entry contain the sum of its 3rd through 25th elements. Hint: the variable i can appear in more than one place in the body of the Do function. In the last assignment we modified the following expression:

  22. To verify that your expression correctly transformed the fares list enter the following expression: fares[[450;;465]] Note that the last column (26th) of each list contains the sum of the 3rd through 25th elements.

  23. Modifying the Header We should document the change we made to all the lists by modifying the header. Remember the header has all the field names. Enter the following expression: header[[26]]="TOTAL" This expression assigns the word “TOTAL” to the 26th element of the header list. We placed the word in quotes as we don’t want Mathematica to try to interpret the word TOTAL as a variable.

  24. Viewing the fields By adding the total value to the end of each list we have really modified the last field of the data set. This field consists of the column formed from the 26th entry of each list in fares. Enter the following expression: fares[[All,1]]

  25. You should see the following output: You may recognize this as a list of station IDs. Each station has a unique Station ID that is the first field of each station record. So we are really viewing the station ID field represented as a list. The expression we entered extracted the first element from All records.

  26. Extracting Fields (Columns) The name of the list we want to extract a field (column) from. The word All tells Mathematica we are interested in a column formed by extracting from all member lists. The field (column) number we wish to extract data from.

  27. Lab Assignment 5 Write the expression needed to extract the station name field data (2nd column) from fares. Write the expression needed to extract the field containing the station totals data (26th column) from fares.

  28. The output for the first expression (displaying the 2nd field) should begin with the following (the output is large): The output for the second expression (displaying the 26th field) should begin with the following (the output is large):

  29. Summing Columns We would like to take the sum of each column (field) – excluding the first two columns which contain station identification information. Enter the following expression into the Notebook:Total[fares[[All,3]]] You should see the following output: Note that this value is over 14 Million. So in a single week over 14 million people used this type of metrocard.

  30. We can check the type of metrocard being used by just checking the third entry in our header list. Enter the following into the Notebook: header[[3]] The field name FF should appear. As you may recall from Lab 1, FF is an abbreviation for Full Fare (see below).

  31. Camel Case Variable Names Rather than calculate the field (column) totals every time we need them lets just store them in a new list named fieldTotal. Note that the variable name fieldTotal has a lowercase f and uppercase T. This method of naming variable is called camel case. It is frequently used when naming variables. When naming a variable using this method the first letter of the first word is made lowercase. However, the first letter of subsequent words is capitalized - e.g. camelCaseExample.

  32. Creating An Initialized List Our method of assigning values to a list requires that the list entry already exist. For example to assign to the 26th element of one list we used fares[[1,26]]=exprwhere we replaced expr with an expression. However, if the 26th element didn’t already exist Mathematica would generate an error.

  33. Enter the following expression into the Notebook: fares[[1,27]]=0 The following output will appear: Note that the error states that Part 27 doesn’t exit. For our column totals we aren’t interested in the 27th column of fares but we will be interested in assigning to parts of another list named fieldTotal. Since we have 26 columns (in fares) we need the fieldTotal list to contain 26 elements. We will initialize those 26 elements to zero. There are a number of ways in Mathematica to create an initialized (contain a specific value) list. We will use the Table function.

  34. Enter the following expression into the Notebook: Table[0,{26}] This expression tells Mathematica that we want a 26 element list that contains only zeros. The following output should appear: However, we want to store this list in the variable named fieldTotal. Enter the following expression into the Notebook: fieldTotal=% The symbol % in Mathematicais a shorthand for the last output. Since our last output was the 26 element zero-initialized list, it is assigned to the variable fieldTotal. To verify that the assignment took place enter the following expression into the Notebook: fieldTotal The output should be:

  35. Assigning to fieldTotal The first two columns of fares contain identification information so we will leave the first two columns of fieldTotal as zero. Enter the following expression into the Notebook: fieldTotal[[3]]=Total[fares[[All,3]]] This expression assigns the third element of fieldTotal to the total of the third column of fares. Clearly this assignment looks amenable to the looping function we applied to total the rows.

  36. Lab Assignment 6 Hint: This should 3. Hint: This should 26. Write the Do function to total columns 3 through 26 of fares. The result of each column total should be stored in the appropriate element of fieldTotal. The following example (from earlier in this lab) may be helpful.

  37. You won’t see any output after the Do function completes but we can view the changes by entering the following expression: fieldTotal You should see the following output: Note that the 26th (last) element of the fieldTotal list contains the total number of metrocard uses for the week of 6/30/12 (week of data collection). Therefore a rough estimate of the total amount of revenue generated during this week can be calculated in Mathematica by entering the following expression: fieldTotal[[26]]*2.25 The output should be: This is over 69 Million Dollars. Of course this is an estimate because some of the metrocards offer reduced fare.

  38. Saving and Submitting The Lab Save your variable values by entering the following expression: Save["lab3_vars",{fares,header, fieldTotal}] Save your Notebook using the File pull-down menu. Name the Notebook lab3. Make sure it is saved in your private mbac311/lab3folder. Close Mathematica. Submit lab3 to Moodle.

More Related