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.

The following code produces the error message when exported to CDF in the title of this question. Although I have seen similar issues on SE, the problem is manifest by the "blinking" of the plot output's cell bracket. My goal is to export this code to CDF but am stymied now.

Manipulate[If[s = 20, s = 0];
 y1[t_] := y[t, 2];
 Plot[y1[t], {t, 0, 20}, PlotRange -> {0, 2}, 
  Epilog -> {PointSize[0.02], Red, Point[{s, y1[s]}]}],
 {{s, 0, "GO"}, 0, 20, .01, ControlType -> Trigger, AnimationRate -> 4, 
  SaveDefinitions -> True},

 Initialization -> (y[t_, x0_] := 1/(1 + (1/x0 - 1) Exp[-0.2 t]))
 ]
share|improve this question
    
Move the definition of y1[t_]=... inside Initialization, i.e., Initialization :> {y[t_, x0_] := 1/(1 + (1/x0 - 1) Exp[-0.2 t]), y1[t_] = y[t, 2]} (after fixing the typo as eldo noted -- If[s==20,...] instead of If[s=20,...]) –  kguler Jun 6 at 15:42
    
@kguler - good point to move the two functions to Initialization. But even then you have to replace their "t" by "x" (or any other unused symbol). Also, the bracket after SaveDefinitions is in the wrong place. It has to close the Trigger-definition. –  eldo Jun 6 at 16:16
    
@eldo, good points.. –  kguler Jun 6 at 16:23
    
@kguler - thanks :) I would (for more time-consuming plots) also recomend to precede the first argument of Plot with Evaluate. Strangely, this can become necessary when you use Initialization: [(mathematica.stackexchange.com/questions/41699/… –  eldo Jun 6 at 16:29
    
@eldo - Why SaveDefinitions -> False at all? The default for SaveDefinitions is False. I used SaveDefinitions->True in my code b/c I had additional functions to plot (for other values of x0) along with the corresponding Epilogs. –  Stephen Jun 6 at 16:30

2 Answers 2

up vote 3 down vote accepted

This works fine with me:

Manipulate[
 If[s == 20, s = 0];
 y1[x_] := y[x, 2];
 y[x_, x0_] := 1/(1 + (1/x0 - 1) Exp[-0.2 x]);

 Plot[y1[t], {t, 0, 20}, PlotRange -> {0, 2}, 
  Epilog -> {PointSize[0.02], Red, Point[{s, y1[s]}]}],

 {{s, 0, "GO"}, 0, 20, .01, ControlType -> Trigger, AnimationRate -> 4},

 SaveDefinitions -> True,
 TrackedSymbols :> {s}
 ]

To try it, copy my code (you had some typos in yours). TrackedSymbols (if used in the right way) kills the blinking.

share|improve this answer

First, making an assignment to a symbol with = or := usually triggers an update; this is because if the value or definition (down values) of a tracked symbol is changed, the system marks the code within a Dynamic that depends on the symbol for an update. The whole body of the Manipulate is put inside a Dynamic, so that the definition y1[t_] := y[t, 2] causes an update if y1 is a tracked symbol, which it will be by default. This leads to an infinite loop because each time the body is executed, the tracked symbol y1 is defined.

There are two (or three) basic ways to go: (1) Remove the dependency of the code on (or do not define y1 in the bode), or (2) do not track y1. Below shows (1), since @eldo has already shown (2). I've also done a few other things. I've isolated the code segment that depends on the symbol s by wrapping it in Dynamic. This means that when s changes, only the point is redrawn and the Plot is not recomputed. This can be a great trick for improving the responsiveness of a Manipulate. Next, I used Mod instead of If. I've localized the symbols y and y1, which would be my preference unless there is a need for them to be global. The declaration

{{y, y}, ControlType -> None}

initializes y to be y and makes it a local variable of the DynamicModule created by Manipulate; in contrast, the declaration

{y, ControlType -> None}

initializes y to be 0, the default. This causes an error when y[t_, x0_] :=... is executed. Finally, I put both function definitions, y and y1, in the Initialization option. This takes the definitions out of the body of Manipulate and prevents the infinite loop.

Manipulate[
 Plot[y1[Pause[0.002]; t], {t, 0, 20},
  PlotRange -> {0, 2}, 
  Epilog -> {PointSize[0.02], Red, 
    Dynamic @ With[{s = Mod[s, 20]}, Point[{s, y1[s]}]]}],

 {{s, 0, "GO"}, 0, 20, .01, ControlType -> Trigger, AnimationRate -> 4},
 {{y, y}, ControlType -> None}, {{y1, y1}, ControlType -> None},

 Initialization :> (
   y1[t_] := y[t, 2];
   y[t_, x0_] := 1/(1 + (1/x0 - 1) Exp[-0.2 t])
   )]
share|improve this answer
    
+1 - should be part of the help text. Isolating the "s" the way you did it seems to solve an old problem of mine. However, instead of writing this somehow obscure ControlType -> None, why don't you simply change the "t" in the two Initialization-functions to "somesymbol"? I just tried your code with this change and that works. –  eldo Jun 6 at 20:04
    
@eldo What difference does changing the t make? It seems to work both ways, so why not leave it as the OP had it? [Also, thanks! +1 to you, too.] –  Michael E2 Jun 6 at 20:18
    
@Michael E2 - I tried your code w/o {{y, y}, ControlType -> None} and it exported to CDF just fine. So I don't understand why you used that code in the first place –  Stephen Jun 6 at 21:05
    
@Stephen. To make y into a local symbol. If you wish y to be global, leave out the {{y, y},...}. If this is the only code in the CDF, then you won't notice any difference either way. Later if you add another Manipulate that uses the symbol y or y1, then it probably will make difference. There are reasons for doing it one way or the other that depend on the application. I pointed out how to localize a function definition in Manipulate because I have yet to find it in the documentation. –  Michael E2 Jun 6 at 21:39
    
I added another function to plot y2[t] and in Epilog - {s, y2[s]} where I defined y2 in the Initialization as y2[t_] := y[t, 0.1]; Now the output cell blinks, although export to CDF works fine. –  Stephen Jun 6 at 22:01

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.