(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 9973, 298]*) (*NotebookOutlinePosition[ 10706, 323]*) (* CellTagsIndexPosition[ 10662, 319]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Sample Commands for Lab#3 ", "Section", FontSize->36], Cell[CellGroupData[{ Cell["\<\ Random Number Generators (for all labs) -- double-click on solid triangle to \ open this and later sections\ \>", "Subsection", FontSize->24], Cell["Random[] generates a random number uniformly from [0,1]:", "Text"], Cell[BoxData[ \(Random[]\)], "Input"], Cell["\<\ Random[Integer] generates 0 half of the time and 1 the other half of the \ time:\ \>", "Text"], Cell[BoxData[ \(Random[Integer]\)], "Input"], Cell[BoxData[ \(Random[Integer]\)], "Input"], Cell[BoxData[ \(Table[Random[Integer], {j, 1, 10}]\)], "Input"], Cell["\<\ Random[Integer,n] generates the integers 0,1,...,n, all with equal \ probability:\ \>", "Text"], Cell[BoxData[ \(Table[Random[Integer, 5], {j, 1, 20}]\)], "Input"], Cell["\<\ If you load the package <", "Text"], Cell[BoxData[ \(<< Statistics`ContinuousDistributions`\)], "Input"], Cell[BoxData[ \(Random[NormalDistribution[34, 2]]\)], "Input"], Cell[BoxData[ \(Table[Random[NormalDistribution[34, 2]], {j, 1, 10}]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["\<\ Simulated Annealing Algorithm (for either of last two projects)\ \>", "Subsection"], Cell["\<\ Here is the code I ran to do simulated annealing via Metropolis \ Monte Carlo on the double-well potential. You can use it as a model for your \ algorithm. Remember that you need to choose Tinit, Tfinal, and npts \ appropriate for your problem, as well, of course, as defining the function \ and moves appropriate for your problem.\ \>", "Text"], Cell[BoxData[{ \(Clear[x]; en[x_] = x^4 - x^2 + 0.1*x;\), "\[IndentingNewLine]", \(\(x = 0.0;\)\), "\[IndentingNewLine]", \(\(i = 0;\)\), "\[IndentingNewLine]", \(\(Tinit = 1;\)\), "\[IndentingNewLine]", \(\(Tfinal\ = \ 0.01;\)\), "\[IndentingNewLine]", \(\(npts = 500;\)\), "\[IndentingNewLine]", \(\(enlist = Table[0, {npts}];\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]logT = Log[Tinit] + i/npts*\((Log[Tfinal] - Log[Tinit])\); \[IndentingNewLine]T = Exp[logT]; \[IndentingNewLine]dx = Random[]*2 - 1; \[IndentingNewLine]xtry = x + dx; \[IndentingNewLine]If[ en[xtry] < en[x], \[IndentingNewLine]\ \ x = xtry, \[IndentingNewLine]\ \ p = Random[]; If[p < \[ExponentialE]\^\(\(-\((en[xtry] - en[x])\)\)/T\), \ \[IndentingNewLine]\ \ x = xtry, \[IndentingNewLine]\ \ x = x];\[IndentingNewLine]]; \[IndentingNewLine]enlist[\([i]\)] = en[x], \[IndentingNewLine]{i, 1, npts}];\)\), "\[IndentingNewLine]", \(ListPlot[enlist]\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Commands for Gambling", "Subsection", FontSize->24], Cell["\<\ Rather than a Do loop, you may find a While loop more useful, since there are \ various reasons for stopping the simulation ($20000 or more, $0 or less, 5000 \ hands played). The syntax for While is: While[condition, command1;command2;...;commandn] Here's an example. I generate random numbers until one is either above 0.95 \ or below 0.02. The ones that lie between I add up and print out, and then \ generate a new random number. Notice how I use && as \"AND\":\ \>", "Text"], Cell[BoxData[{ \(\(total = 0.0;\)\), "\[IndentingNewLine]", \(\(s = Random[];\)\), "\[IndentingNewLine]", \(\(Print[s];\)\), "\[IndentingNewLine]", \(\(While[ s < 0.95\ && \ s > 0.02, \[IndentingNewLine]total = total + s; \[IndentingNewLine]s = Random[]; \[IndentingNewLine]Print[ s]];\)\), "\[IndentingNewLine]", \(Print["\", total]\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Commands for Pollutant Spread", "Subsection"], Cell["\<\ You will want to store the list of current positions of the benzene \ particles. Here's an initialization of this list, so that it holds 4000 \ ordered pairs (0,0). Another way of thinking of this \"list of lists\" is as \ a 4000-by-2 matrix.\ \>", "Text"], Cell[BoxData[{ \(\(n = 4000;\)\), "\[IndentingNewLine]", \(\(xylist\ = \ Table[{0, 0}, {i, 1, n}];\)\)}], "Input"], Cell["\<\ The syntax for extracting or setting the (i,j) entry of a matrix \ (or list of lists) m is m[[i,j]]:\ \>", "Text"], Cell[BoxData[ \(m\ = \ {{2, 4}, {3, 5}, {7, 9}}\)], "Input"], Cell[BoxData[ \(m\ // \ MatrixForm\)], "Input"], Cell[BoxData[ \(m[\([1, 2]\)]\)], "Input"], Cell[BoxData[{ \(\(m[\([1, 2]\)] = 3;\)\), "\[IndentingNewLine]", \(m\ // \ MatrixForm\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Commands for Ising Model", "Subsection"], Cell["You can make a random initial guess as follows:", "Text"], Cell[BoxData[{ \(\(L = 6;\)\), "\[IndentingNewLine]", \(\(spinmat = Table[0, {i, 1, L}, {j, 1, L}];\)\), "\[IndentingNewLine]", \(\(Do[\[IndentingNewLine]Do[\[IndentingNewLine]s = You\ fill\ in\ here; \[IndentingNewLine]spinmat[\([i, j]\)] = s, \[IndentingNewLine]{j, 1, L}], \[IndentingNewLine]{i, 1, L}];\)\), "\[IndentingNewLine]", \(spinmat // MatrixForm\)}], "Input"], Cell["The Mod function can distinguish even from odd numbers:", "Text"], Cell[BoxData[ \(Mod[5, 2]\)], "Input"], Cell[BoxData[ \(Mod[4, 2]\)], "Input"], Cell["\<\ Here's a command you can run to view your spin matrix after annealing. To \ use it, type view[L,spinmat], where L is the dimension of the lattice and \ spinmat is the spin matrix output by annealing:\ \>", "Text"], Cell[BoxData[ \(view[num_, list_]\ := \[IndentingNewLine]Block[{y, l, i, j, onlist, offlist, x, pl1, pl2}, \[IndentingNewLine]onlist = {}; \ \[IndentingNewLine]offlist = {}; \[IndentingNewLine]Do[\[IndentingNewLine]Do[\ \[IndentingNewLine]y = \((\(-i\) + 1)\)*Sqrt[3]/2; \[IndentingNewLine]l = j - 1; \[IndentingNewLine]If[Mod[i, 2] \[Equal] 1, x = l, x = l - 1/2]; \[IndentingNewLine]If[ list[\([i, j]\)] \[Equal] 1, onlist = Append[onlist, {x, y}], offlist = Append[offlist, {x, y}]], \[IndentingNewLine]{i, 1, num}], \[IndentingNewLine]{j, 1, num}]; \[IndentingNewLine]pl1 = ListPlot[onlist, PlotStyle \[Rule] {RGBColor[1, 0, 0], PointSize[1.0/\((1.3*num)\)]}, PlotRange \[Rule] {{\(-1\), num - 1/2}, {\(-Sqrt[3]\)/2*\((num - 1/2)\), 0.5}}, DisplayFunction \[Rule] Identity]; \[IndentingNewLine]pl2 = ListPlot[offlist, PlotStyle \[Rule] {RGBColor[0, 0, 1], PointSize[1.0/\((1.3*num)\)]}, PlotRange \[Rule] {{\(-1\), num - 1/2}, {\(-Sqrt[3]\)/2*\((num - 1/2)\), 0.5}}, DisplayFunction \[Rule] Identity]; \[IndentingNewLine]Show[pl1, pl2, DisplayFunction \[Rule] \ $DisplayFunction]\[IndentingNewLine]]\)], "Input"], Cell[BoxData[ \(view[6, spinmat]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Commands for Traveling Salesperson", "Subsection"], Cell["You may want to use the Sum formula:", "Text"], Cell[BoxData[ \(Sum[n^2, {n, 1, 5}]\)], "Input"], Cell["\<\ Here is a command you can use to view your path. To run it, type \ routeview[ordering,x,y,ncities], where ordering is your vector of integers \ giving the ordering of cities to visit, x is your vector of x coordinates of \ the cities, y is the vector of y coordinates of the cities, and ncities is \ the number of cities.\ \>", "Text"], Cell[BoxData[ \(routeview[ordarg_, xarg_, yarg_, ncitiesarg_]\ := \[IndentingNewLine]Block[{}, \ \[IndentingNewLine]itin = Table[{xarg[\([ordarg[\([j]\)]]\)], yarg[\([ordarg[\([j]\)]]\)]}, {j, 1, ncitiesarg}]; \[IndentingNewLine]ListPlot[itin, PlotJoined \[Rule] True, PlotStyle \[Rule] PointSize[0.03]]\[IndentingNewLine]]\)], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"4.0 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 695}}, ScreenStyleEnvironment->"Presentation", WindowSize->{906, 599}, WindowMargins->{{44, Automatic}, {22, Automatic}}, StyleDefinitions -> "PastelColor.nb" ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1739, 51, 61, 1, 86, "Section"], Cell[CellGroupData[{ Cell[1825, 56, 153, 4, 95, "Subsection"], Cell[1981, 62, 72, 0, 45, "Text"], Cell[2056, 64, 41, 1, 54, "Input"], Cell[2100, 67, 104, 3, 45, "Text"], Cell[2207, 72, 48, 1, 54, "Input"], Cell[2258, 75, 48, 1, 54, "Input"], Cell[2309, 78, 67, 1, 54, "Input"], Cell[2379, 81, 105, 3, 45, "Text"], Cell[2487, 86, 70, 1, 54, "Input"], Cell[2560, 89, 211, 4, 70, "Text"], Cell[2774, 95, 71, 1, 54, "Input"], Cell[2848, 98, 66, 1, 54, "Input"], Cell[2917, 101, 85, 1, 54, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3039, 107, 93, 2, 40, "Subsection"], Cell[3135, 111, 357, 6, 95, "Text"], Cell[3495, 119, 1118, 21, 515, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[4650, 145, 59, 1, 41, "Subsection"], Cell[4712, 148, 494, 12, 245, "Text"], Cell[5209, 162, 424, 9, 215, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[5670, 176, 51, 0, 40, "Subsection"], Cell[5724, 178, 268, 5, 70, "Text"], Cell[5995, 185, 124, 2, 77, "Input"], Cell[6122, 189, 124, 3, 45, "Text"], Cell[6249, 194, 65, 1, 54, "Input"], Cell[6317, 197, 52, 1, 54, "Input"], Cell[6372, 200, 46, 1, 54, "Input"], Cell[6421, 203, 110, 2, 77, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[6568, 210, 46, 0, 40, "Subsection"], Cell[6617, 212, 63, 0, 45, "Text"], Cell[6683, 214, 437, 8, 238, "Input"], Cell[7123, 224, 71, 0, 45, "Text"], Cell[7197, 226, 42, 1, 54, "Input"], Cell[7242, 229, 42, 1, 54, "Input"], Cell[7287, 232, 224, 4, 70, "Text"], Cell[7514, 238, 1411, 25, 560, "Input"], Cell[8928, 265, 49, 1, 54, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[9014, 271, 56, 0, 40, "Subsection"], Cell[9073, 273, 52, 0, 45, "Text"], Cell[9128, 275, 52, 1, 54, "Input"], Cell[9183, 278, 346, 6, 95, "Text"], Cell[9532, 286, 413, 8, 146, "Input"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)