# Manipulate Re-evaluation

GROUPS:
 Tim Fernandez-Hart 2 Votes Hi Guys,I'm new to Mathematica and I am having trouble understanding manipulate. I have some code which should explore the relationships between the number of trials, the sample space and the sample size. The code below runs with the initialsed values but doesn't re-evaluate if I move any of the sliders, Where am I going wrong?Many thanks in advance,Tim Manipulate[  Module[{populationSize = 20, sampleSize = 6, data = {}, rawData,     dataR, dataL, totalData, trials = 5},      drawSample :=     AppendTo[data, RandomSample[Range[populationSize], sampleSize]];   Do[drawSample, {trials}];      rawData = Flatten[data]; dataR = Sort[rawData, Greater];   dataL = Sort[rawData]; totalData = Join[dataL, dataR];  BarChart[Drop[totalData, {1, Length[totalData], 2}]]  ], {populationSize, 1, 50, 1}, {{sampleSize, 6}, 1, populationSize,   1}, {{trials, 5}, 1, 10}]
5 years ago
7 Replies
 Bianca Eifert 2 Votes Hi Tim,I think the problem is that you try to localize the Manipulate variables (populationSize, sampleSize, populationSize) again inside the Module. It works just fine if you simply omit these variables from the Module to avoid this conflict.Note that I had to add the initial value of populationSize to the Manipulate control. Setting the TrackedSymbols option stops the Manipulate from evaluating multiple times (just try out what happens without it).Bianca Manipulate[Module[{data = {}, rawData, dataR, dataL, totalData, drawSample}, drawSample := AppendTo[data, RandomSample[Range[populationSize], sampleSize]]; Do[drawSample, {trials}]; rawData = Flatten[data]; dataR = Sort[rawData, Greater]; dataL = Sort[rawData]; totalData = Join[dataL, dataR]; BarChart[Drop[totalData, {1, Length[totalData], 2}]]],{{populationSize, 20}, 1, 50, 1},{{sampleSize, 6}, 1, populationSize, 1},{{trials, 5}, 1, 10},TrackedSymbols :> {populationSize, sampleSize, trials}]
5 years ago
 Nasser M. Abbasi 1 Vote fyi, the above gives an errorRandomSample::smplen: "RandomSample cannot generate a sample of length 15, which is greater than the length of the sample set {1,2,3,4,5,6,7,8,9,10,11}. If you want a choice of possibly repeated elements from the set, use RandomChoice"when moving the populationSize slides less than 15.
5 years ago
 William Rummler 4 Votes Hi Tim,Bianca is right and Nasser correctly points out an additional issue. One way to avoid the latter is to make sure that updates to populationSize also update sampleSize if necessary. Here's how you might do that: Manipulate[    Module[{data = {}, rawData, dataR, dataL, totalData},   drawSample :=     AppendTo[data, RandomSample[Range[populationSize], sampleSize]];   Do[drawSample, {trials}];   rawData = Flatten[data];   dataR = Sort[rawData, Greater];   dataL = Sort[rawData];  totalData = Join[dataL, dataR];  BarChart[Drop[totalData, {1, Length[totalData], 2}]]], {{populationSize, 20},  Manipulator[    Append[#,     Function[{val, expr},      expr = val;      sampleSize = Min[val, sampleSize],      {HoldRest}]],    {1, 50, 1}] &}, {{sampleSize, 6}, 1, populationSize, 1}, {{trials, 5}, 1, 10}]What we've done above is essentially reconstruct the default control, except that we added a customized update function for populationSize that also updates sampleSize if necessary. The details here are relatively intricate. I encourage you to study the Details subsection of ref/Manipulate carefully, and there's probably nothing better for getting acquainted with the nuances and power of Dynamic / Manipulate functionality than a certain set of four tutorials found in the Mathematica Documentation Center, under "Learning Resources" at the following Guide page: http://reference.wolfram.com/mathematica/guide/DynamicInteractivityLanguage.htmlHope this helps!
5 years ago
 Hans Milton 2 Votes Hi Tim,This is another way of skinning this particular cat. Variables are localized directly in the Manipulate, as controls. Manipulate[  (* Computations pre output *)  If[populationSize < sampleSize, populationSize = sampleSize];  data = {};   Do[    AppendTo[data, RandomSample[Range[populationSize], sampleSize]], {trials}   ];   rawData = Flatten[data];   dataR = Sort[rawData, Greater]; dataL = Sort[rawData]; totalData = Join[dataL, dataR]; (* The actual output *)  BarChart[Drop[totalData, {1, Length[totalData], 2}]], (* Controls *) {{populationSize, 20}, 1, 50, 1, Appearance -> "Labeled"}, {{sampleSize, 6}, 1, populationSize, 1, Appearance -> "Labeled"}, {{trials, 5}, 1, 10, 1, Appearance -> "Labeled"}, (* Local variables as controls *) {data, ControlType -> None}, {rawData, ControlType -> None}, {dataR, ControlType -> None}, {dataL, ControlType -> None}, {totalData, ControlType -> None}, (* Manipulate option *) TrackedSymbols -> {populationSize, sampleSize, trials} ]