4
$\begingroup$

I am new to Mathematica. Once I evaluate the cell containing the code, my Manipulate works as desired; however, if I open the file and do not evaluate the cell, the Manipulate goes haywire. Can anyone help please? The code below is all in the same cell.

Sequence1[n_] := { 1 / n, (-1)^n / n, 1 / Sqrt[n] };
Function1[x_] := { ( x - 2 )^2, 2 - x^2, 2^x };

Manipulate[

 Grid[ { { "Function" , "Sequence of Outputs" },

    { data = 
     List[Transpose[{Table[ Sequence1[n][[sss]], {n, 1, t}], 
        Table[ Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}]}]]; 
    ListPlot[data, PlotRange -> {{-1, 1}, {0, 5}}, 
     AspectRatio -> 1/1, ImageSize -> Full], 

    ListPlot[Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}], 
     PlotRange -> Automatic, ImageSize -> Full]},

   { "Close-Up" , "Tabular Values" }, 

   { data = 
     List[Transpose[{Table[ Sequence1[n][[sss]], {n, 1, t}], 
        Table[ Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}]}]]; 
    ListPlot[data, PlotRange -> {{-.1, .1}, {0, 5}}, 
     AspectRatio -> 1/1, ImageSize -> Full]  ,
     If[ t < 11, 
     TableForm[ 
      Transpose[ {Range[1, t], 
        Table[ Sequence1[n][[sss]], {n, 1, t} ], 
        Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}]}]], 
     TableForm[
      Transpose[ {Range[t - 10, t], 
        Table[Sequence1[n][[sss]], {n, t - 10, t} ], 
        Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, t - 10, 
          t}]}]]]}},

  ItemSize -> {{Scaled[.4], Scaled[.4]}}, Frame -> All, 
  Spacings -> {2, 2}],

 {{sss, 3, "sequence"}, {1 -> " 1 / n ", 2 -> " (-1)^n / n ", 
   3 -> "1 / Sqrt[n] "}, 
  SetterBar}, {{hhh, 3, "f(x)="}, {1 -> "(x-2)^2", 2 -> "2-x^2", 
   3 -> "2^x"}, SetterBar}, {t, 1, 100, 1}]
$\endgroup$
3
  • $\begingroup$ I'm afraid I don't understand. Which code are you talking about: the two functions Function1 and Sequence1, or the code for the Manipulate? $\endgroup$
    – rcollyer
    Aug 15, 2012 at 19:31
  • 4
    $\begingroup$ You could try using the option SaveDefinitions -> True in the Manipulate. $\endgroup$
    – Heike
    Aug 15, 2012 at 19:31
  • $\begingroup$ This worked thanks. $\endgroup$
    – RKennedy
    Aug 16, 2012 at 7:34

1 Answer 1

5
$\begingroup$

Add the Initialization option to your Manipulate as shown below:

Manipulate[
 Grid[{{"Function", 
    "Sequence of Outputs"}, {data = 
     List[Transpose[{Table[Sequence1[n][[sss]], {n, 1, t}], 
        Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}]}]];
    ListPlot[data, PlotRange -> {{-1, 1}, {0, 5}}, AspectRatio -> 1/1,
      ImageSize -> Full], 
    ListPlot[Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}], 
     PlotRange -> Automatic, ImageSize -> Full]}, {"Close-Up", 
    "Tabular Values"}, {data = 
     List[Transpose[{Table[Sequence1[n][[sss]], {n, 1, t}], 
        Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}]}]];
    ListPlot[data, PlotRange -> {{-.1, .1}, {0, 5}}, 
     AspectRatio -> 1/1, ImageSize -> Full], 
    If[t < 11, 
     TableForm[
      Transpose[{Range[1, t], Table[Sequence1[n][[sss]], {n, 1, t}], 
        Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, 1, t}]}]], 
     TableForm[
      Transpose[{Range[t - 10, t], 
        Table[Sequence1[n][[sss]], {n, t - 10, t}], 
        Table[Function1[Sequence1[n][[sss]]][[hhh]], {n, t - 10, 
          t}]}]]]}}, ItemSize -> {{Scaled[.4], Scaled[.4]}}, 
  Frame -> All, 
  Spacings -> {2, 2}], {{sss, 3, "sequence"}, {1 -> " 1 / n ", 
   2 -> " (-1)^n / n ", 3 -> "1 / Sqrt[n] "}, 
  SetterBar}, {{hhh, 3, "f(x)="}, {1 -> "(x-2)^2", 2 -> "2-x^2", 
   3 -> "2^x"}, SetterBar}, {t, 1, 100, 1}, 
 Initialization :> (Sequence1[n_] := {1/n, (-1)^n/n, 1/Sqrt[n]};
   Function1[x_] := {(x - 2)^2, 2 - x^2, 2^x})]
$\endgroup$
3
  • $\begingroup$ This also worked, Thanks! What is the difference between putting the initialization inside the manipulate vs the savedefinitions->true? $\endgroup$
    – RKennedy
    Aug 16, 2012 at 7:36
  • $\begingroup$ I spoke too soon. Only the initialization code worked. Thanks kalewallace. I was experimenting in the same file, so the savedef code suggestion worked since the initialization code worked which I ran first. $\endgroup$
    – RKennedy
    Aug 16, 2012 at 7:48
  • $\begingroup$ The SaveDefinitions option "remembers" the value of the variable or the function before the notebook is closed, saving it within the notebook to where it is available when the notebook is reopened (even after the kernel is stopped). The Initialization option re-evaluates the expression whenever the Dynamic expression is displayed. This is useful if your Manipulate involves large amounts of data, such as importing data or SQL servers. Both ways work fine for small functions like in your case. $\endgroup$
    – kale
    Aug 16, 2012 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.