Working with Data Sets

Lists

All lists (i.e. sets) in Mathematica are stored in curly brackets: {1, 4, 6, 9, 12, -1, 0, 8} is a list with eight elements, the first being 1, the second being 4, and the third being 6, etc...

A = {1,4,6,9,12,-1,0,8}

We can get at individual elements of the list A, as defined above, by using index notation.  Since A is the variable which is the placeholder for the list, we can get at the first entry of A by using the command A[[1]].

A[[1]]

A[[5]]

We can also get other useful information about the list A, for instance, we can determine the number of elements in the list by the following command:

Length[A]

A[[8]]

A[[9]]

We can also sum the numbers in the list:

Total[A]

1+4+6+9+12-1+0+8

You can determine if a specific value belongs to a list as well by using the MemberQ command:

MemberQ[A,-1]

MemberQ[A,7]

We can also create subsets of A in an ordered fashion by selection every nth entry.  The first command below says: list the elements of A, starting at element 1, ending at the last element (-1), and go by 2’s.

A[[1;;-1;;2]]

{A[[1]],A[[3]],A[[5]],A[[7]]}

We could start at the second to last element of A, go back by 3’s to the first element, this would be accomplished by the command:

A[[-2;;1;;-3]]

Take some time to make sure you understand how the above command works!

Lists of lists

So the wording of this section sounds a little strange, but if you think about it, if you have a set of points in the xy-plane, each ordered pair is a list of two elements, and a collection of these ordered pairs is itself a list.  We are going to start with the list A from the previous section, and join to it another list B with the same number of elements...  Once we have this done, we use the Thread command to join each element of A with its corresponding element of B.

Length[A]

B = {-1,2,0,-2,3,6,7,1}

Length[B]

A

AB = Thread[{A,B}]

So after the Thread command, we have a new list, which we called AB, whose elements are themselves lists...

AB[[3]]

So by using the command AB[[3]], we arrive at the element {6,0}.  If we wanted to get the first entry in this, we would use the command:

AB[[3]][[1]]

AB[[3]][[2]]

The command AB[[3]][[1]] says: take the third entry from the list AB, and from that, take the first entry.  Ask yourself what the command AB[[5]][[2]]would give you, then run it!
We do not have to use two separate lists to create a list of ordered points, we can manually do this:

AB[[5]][[2]]

PtList = {{0,0},{1,-1},{2,-5},{3,1},{4,-6},{6,10},{7,-2},{8,0},{12,1}};

Length[PtList]

One of the concepts we will be studying in the future is the measure of “slope” of a function, which is change in y-coordinate over the change in the x-coordinate.  I.e. given two points ((Subscript[x, 1]),(Subscript[y, 1])), ((Subscript[x, 2]),(Subscript[y, 2])), the slope is defined to be:

({{({{m = (((Subscript[y, 2])-(Subscript[y, 1]))/((Subscript[x, 2])-(Subscript[x, 1])))}})}})

For each point in PtList above, the second entry is the y-coordinate, and the first entry is the x-coordinate.  We can compute successive slopes using the following Table command:

PtList[[2]]

PtList[[2]][[2]]

Table[(PtList[[i+1]][[2]]-PtList[[i]][[2]])/(PtList[[i+1]][[1]]-PtList[[i]][[1]]), {i,1,8}]

Note that there are only 8 entries in the slope list, because each entry uses 2 successive points.  The first set of parentheses is the change in y-coordinates, and the second is the change in x-coordinates.

Plotting lists of points (lists of lists)

Mathematica has a ListPlot command, so we do a simple example first:

ListPlot[PtList]

The ListPlot command has a ton of options, so we can make our data plots look pretty cool!  A list of plot markers can be found here:

https://reference.wolfram.com/language/guide/ShapesIconsAndRelatedCharacters.html

ListPlot[PtList, PlotLegends->"data", PlotStyle->{Red}, PlotMarkers->{"▽",16}]

Just like with graphs of functions, we can plot multiple lists at the same time!

ListPlot[{AB,PtList}, PlotLegends->{"AB","Pts"}, PlotStyle->{Red,Blue}, PlotMarkers->{{"▽",16},{"✶",12}}]

Combining Data and Functions

Some of what we will be doing this semester is using data given to construct functions which either (a) exactly model the data, or (b) approximate the data as much as possible.  Imagine that we have the following data:

DataFunc = {{-2.00,-6.5},{-1.80,-5.86},{-1.60,-5.22},{-1.40,-4.58},{-1.20,-3.94},{-1.00,-3.30},{-0.80,-2.66},{-0.60,-2.02},{-0.40,-1.38},{-0.20,-0.74},{0.00,-0.10},{0.20,0.54},{0.40,1.18},{0.60,1.82},{0.80,2.46},{1.00,3.10},{1.20,3.74},{1.40,4.38},{1.60,5.02},{1.80,5.66},{2.00,6.30},{2.20,6.94},{2.40,7.58},{2.60,8.22},{2.80,8.86},{3.00,9.50}};

Length[DataFunc]

If we plot this, it appears that it all lies on a line....  We are going to call it something (DPlot) so that we can use it later!

DPlot = ListPlot[DataFunc, PlotStyle->Blue, PlotMarkers->{"▽",16}, PlotLegends->{"Pts",LegendMarkers->Automatic}]

So we can try to compute the slope through each successive pair of points.  Pay special attention to the last part of the Table command, instead of counting the number of elements in the DataFunc variable, we can just use the Length command.

Table[(DataFunc[[i+1]][[2]]-DataFunc[[i]][[2]])/(DataFunc[[i+1]][[1]]-DataFunc[[i]][[1]]), {i,1,Length[DataFunc]-1}]

Notice that the slope appears to be constant for each successive pair of points!  So we can compute the equation of the line through all those points by using the slope-intercept form of the line: y=mx+b.  Since each point is of the form (x,y), we can plug them into the equation. (remember, m=3.2).

DataFunc[[1]]

Solve[DataFunc[[1]][[2]]==3.2*DataFunc[[1]][[1]]+b,b]

Solve[DataFunc[[10]][[2]]==3.2*DataFunc[[10]][[1]]+b,b]

Note that we get the same value for b regardless of which point we choose.  Also, the data listed in DataFunc actually has the point (0,-0.1) in it!  Thus we know we did things correctly!  Now we are going to graph this line and plot it along with the data.  Pay attention to the domain for x in the command below.

LinePlot = Plot[3.2*x-0.1,{x,-2.2,3.2}, PlotStyle->{Red, Thickness[0.004]},PlotLegends->{"Line",LegendMarkers->Automatic}];

Show[LinePlot,DPlot]