Mathematica Lists

by
K. D.  Stroyan
University of Iowa

copyright 2007 by Keith Stroyan - All rights reserved.

Review: Closed and Open Mathematica Cells

The NoteBook MmaNtBkCells introduces you to use of cells.

To open a closed cell, use your mouse to position the cursor over the dark arrowhead and click the button on the mouse twice rapidly.  (This is called "double clicking.")  When you are done looking at a section, you can position the cursor at the far right bracket again and double click to close that section.

Open the cell containing "Notebook Overview" now.  Then close this cell.

Notebook Overview: What's expected?

This NoteBook introduces you to List computations in Mathematica.  Mathematica works especially well with lists and operations on lists.

The only requirement of the NoteBook is for you to work through the Exercise that parallels the text work in the preview section of Chapter 1 of the main text.  The remaining computations are only to give you a quick tour of some of the graphic computations in Mathematica.

References to the Text

This NoteBook goes with Chapter 1 of the text.

Lists

The ablility to define and use lists is one of the most powerful features of  Mathematica  and we will take advantage of this ability frequently.

Basic Lists

Enter the cells below to define a list of exact numbers and see their decimal approximations.

list={0,(π/6),(π/4),(π/3),(π/2),((2 π)/3),((3 π)/4),((5 π)/6),π}

N[list]

If we want to find the Cosine of every number in the list, all we need to do is enter Cos[list] and Mathematica will find the Cosine of all the numbers above.

Cos[list]

We can also define lists of points for Mathematica to plot.  Enter the cell below to define a list of data points.

data={{0,0},{(π/6),(1/2)},{(π/4),((Sqrt[2])/2)},{(π/3),((Sqrt[3])/2)},{(π/2),1}}

Now we can use the ListPlot command to graph the above points.  The command has an option, PlotJoined -> True, that tells Mathematica to connect the points it graphs.

ListPlot[data,Joined->True]

Operations on Lists

Lists of Lists and Transpose

A list can also be a list of lists.  Here is an example of points on the curve  y = (x^3):

Clear[f,xList,yList,pairs];
f[x_]:=(x^3);
xList={-1,0,1,1.5,2};
yList=f[xList];
Print["The x list = ",xList];
Print["The y list = ",yList];
pairOfLists={xList,yList};
Print["The pair of lists = "];
Print[pairOfLists];

A list of pairs is not the same thing as a pair of lists.  <Enter> the next cell to see why.

listOfPairs=Transpose[pairOfLists];
Print["The pair of lists = "];
Print[pairOfLists];
Print["The list of pairs = "];
Print[listOfPairs];

Lists can also be printed in table or matrix form:

Print["The pair of lists = "];
Print[TableForm[pairOfLists]];
Print["The list of pairs = "];
Print[TableForm[listOfPairs]];

The list of pairs is needed to make a plot:

ListPlot[listOfPairs]

ListPlot[pairOfLists]

Longer lists

Let's add a datum to our x list and add the corresponding point to the y list:

AppendTo[xList,3];
AppendTo[yList,f[3]];
AppendTo[listOfPairs,{3,f[3]}];
Print["New longer x list = ",xList];
Print["New longer x list = ",xList];
Print["New longer pair list = "];
Print[TableForm[listOfPairs]];

ListPlot[listOfPairs,Joined->True]

Practice Exercise

Use ListPlot to make a plot of the points  (x,y)  where  y = f[x] = (x^2)  and  x  takes the values {-1,0,1,1.5,2}.  You need to enter correct commands where we have typed ??.  See the samples above.

Clear[xList,yList,f];

f[x_] := ??;

xList = {??,??,??};

yList = ??

listOfPairs = ??

ListPlot[ listOfPairs ];