mbac 611. within your private mbac611 folder create a lab5 folder. copy the moodle file lab3_vars...

22
Lab 5 MBAC 611

Upload: iris-ramsey

Post on 26-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • MBAC 611
  • Slide 2
  • Within your private mbac611 folder create a lab5 folder. Copy the Moodle file lab3_vars to your lab5 folder. Start Mathematica Enter SetDirectory[h:mbac611/lab5] into the Mathematica notebook and press the evaluation key.
  • Slide 3
  • In an earlier lab we saved the fares, header and fieldTotal variables values to disk so that they could be used in this lab. To restore these values enter the following expression into the Notebook: header[[3;;25]]] You should see the following output: This graph is only slightly better it graphs the correct data but the axis is unreadable because the metrocard type names are running into eachother.
  • Slide 7
  • Select elements 3 through 25 of the fieldTotal list. ChartLabels is an option of the BarChart function that allows you to label the elements of the chart. This symbol is the Mathematica transformation symbol. It is formed by typing the characters -> without an intervening space. It essentially used to temporarily make an assignment. In this case we are temporarily assigning to ChartLabels. We are assigning the 3 rd through 25 th header list elements to ChartLabels.
  • Slide 8
  • We can fix the readability of the bar graph by adding the BarOrigin option. Enter the following expression: BarChart[fieldTotal[[3;;25]],ChartLabels->header[[3;;25]], BarOrigin->Left] The BarOrigin option allows you to specify the origin of the bars. Other possible BarOrigin assignments include Right, Top and Bottom.
  • Slide 9
  • The following output should appear: This looks better. We can even improve on it by enlarging the graph. To enlarge the graph simply click once on the graph and then click and hold the mouse button down on one of the resize boxes. Then move the mouse. Try enlarging the graph using the bottom resize box on the right-hand side.
  • Slide 10
  • This graph is much clearer. We can see that Full Fare (FF) metrocards are the most popular, with 30-Day-Unlimited (30-D UNL) and 7-Day- Unlimited (7-D UNL) coming in second and third.
  • Slide 11
  • Mathematica has many additional options that can be added to the BarChart function. From the Lab5 Moodle folder download the metrocard.jpg image and place a copy in your private mbac611/lab5 folder. Enter the following expression: metroCardImage=Import["metrocard.jpg"]
  • Slide 12
  • Enter the following expression: BarChart[fieldTotal[[3;;25]],ChartLabels->header[[3;;25]], BarOrigin->Left, ChartElements->metroCardImage] Note the additional option (ChartElements). You should see the following output:
  • Slide 13
  • Create a bar chart based on the first (1 st ) station (record) in the fares list. Remember that the station metrocard data is in fields 3-25 (Hint: fares[[1,3;;25]]) The labels for the bar chart are the same as our previous chart Set the BarOrigin option should to Left. Add a chart title by adding the PlotLabel option. PlotLabel should be assigned the second (2 nd ) element in the first (1 st )record the station name is stored there. It is not necessary to use the metrocard image for the bars, but are free to use it if you wish.
  • Slide 14
  • The output should appear as follows (note the station name):
  • Slide 15
  • We can view the bar charts for each station by executing the BarChart function on every member of the fares list. However, a much better way of viewing the charts is to use the Manipulate function. The Manipulate function allows us to change arguments to functions graphically, using a sliding bar.
  • Slide 16
  • Enter the following expression: Manipulate[BarChart[fares[[n,3;;25]], ChartLabels->header[[3;;25]], BarOrigin->Left, PlotLabel->fares[[n,2]]], {n,1,Length[fares]}] The following output should appear: Note the slider. Click and hold the mouse button on the slider. Then move the slider to the right. The station bar charts will change. Note that you can still resize the chart if it is too small. header[[3;;25]], BarOrigin->Left, PlotLabel->fares[[n,2]] ], {n,1,Length[fares]} ] Lets take a cl">
  • Manipulate[ BarChart[ fares[[n,3;;25]], ChartLabels->header[[3;;25]], BarOrigin->Left, PlotLabel->fares[[n,2]] ], {n,1,Length[fares]} ] Lets take a closer look at the function code. Note that spaces between arguments, options or functions are ignored by Mathematica so it is fine to add space for clarity. The first argument of the Manipulate function is the function we are going to visualize. In this case it is the BarChart function. Note that value we are going to be changing with the slider is replaced with the variable n. The second argument is the iterator. This is very similar to the iterator that appeared in the Do function. The variable n should, depending on the slider, take on values between 1 and the length of the fares list which is the number of stations (465).
  • Slide 20
  • We can fix the button increment and decrement problem by telling the Manipulate function to change the value of n by 1. This is done by adding an increment value (one in this case) as the fourth member of the iterator - {n,1,Length[fares],1} Notice that our variable (n) appears next to the slider. This indicates that the user is controlling this variable with the slider. However, n is not particularly meaningful. Change the variable from n to station. Remember, if you change it in the iterator you also need to change it in the BarChart function call. Make the two changes described above and execute the Manipulate function call again.
  • Slide 21
  • Your output should look similar to the following: Note the variable name n has been replaced by the variable name station. When you press the and buttons the increment and decrement step should now be 1.
  • Slide 22
  • Save a copy of your notebook in your lab5 folder. Submit the notebook file to Moodle.