Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}]
share|improve this question
    
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? –  rcollyer Aug 15 '12 at 19:31
4  
You could try using the option SaveDefinitions -> True in the Manipulate. –  Heike Aug 15 '12 at 19:31
    
This worked thanks. –  RKennedy Aug 16 '12 at 7:34
add comment

1 Answer

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})]
share|improve this answer
    
This also worked, Thanks! What is the difference between putting the initialization inside the manipulate vs the savedefinitions->true? –  RKennedy Aug 16 '12 at 7:36
    
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. –  RKennedy Aug 16 '12 at 7:48
    
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. –  kale Aug 16 '12 at 14:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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