Re: Dynamic Show in Manipulate
- To: mathgroup at smc.vnet.net
- Subject: [mg94165] Re: [mg94041] Dynamic Show in Manipulate
- From: John Fultz <jfultz at wolfram.com>
- Date: Sat, 6 Dec 2008 06:15:05 -0500 (EST)
- Reply-to: jfultz at wolfram.com
What you want is a static graphic with changing elements. Fundamentally, we'll want to apply the approach documented in the advanced Manipulate tutorial of using Dynamic inside of Manipulate to control the scope of re-evaluation. See tutorial/AdvancedManipulateFunctionality in the docs for more information on this. Unfortunately, the implementation in your example is a bit non-obvious. Probably the easiest way to do this in your example is to inject the dynamic part into the Epilog option of the static graphic. However, we also have to make sure that the calculation of pl2 doesn't trigger a complete reevaluation of the body of the Manipulate. The easiest way, perhaps, is to remove pl2 entirely. So this example shows the plot embedded in an Epilog, using Dynamic to prevent the need to evaluate the entire Manipulate body. I've also removed the TrackedSymbols and PerformanceGoal options which aren't necessary for this example to perform efficiently. Needs["VectorFieldPlots`"] pl1 = VectorFieldPlot[{1, Cos[x]}, {x, -5, 5}, {y, -5, 5}]; Manipulate[ Show[pl1, Epilog -> Dynamic[First[Plot[Cos[x], {x, -5, -5 + k}]]], PlotRange -> {-5, 5}], {{k, 1, "k"}, 1, 10}] If you'd prefer to pull the Plot command out of the Manipulate to clean the code up, it can be done using With and Hold. With[{pl2 = Hold[First[Plot[Cos[x], {x, -5, -5 + k}]]]}, ReleaseHold[Manipulate[ Show[pl1, Epilog -> Dynamic[pl2], PlotRange -> {-5, 5}], {{k, 1, "k"}, 1, 10}]]] One unfortunate disadvantage of this approach is that it wouldn't work very well if pl1 used Epilog. If that's a problem for you, let me know. Other methods are possible, but they're more complex. On the plus side, thinking about your example has given me some ideas about how this could be made much simpler in future releases of Mathematica, so your question is certainly appreciated! Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc. On Wed, 3 Dec 2008 05:42:08 -0500 (EST), Michael Hsieh wrote: > Hello! > > I have a seemingly simple problem here, but somehow can't figure out a > solution: > > I compute a Plot of a Differential Equation, with variable x-ranges, > and want to show the graphic with a VectorFieldPlot as background. > > Unfortunately, within Manipulate, the static VectorFieldPlot and the > whole Differential Equation are reevaluated anytime I move the slide bar. > This makes the whole movie very very slow. Is there a way to > "tell" Show that the VectorFieldPlot is just a background and doesn't need > to be reevaluated every time? I tried Dynamic Settings, and Background > Settings, > but nothing really works: > > Below a code snippet to give you a better idea of what I mean: > > -- Code (copypaste-able directly into M6 nb) > > Needs["VectorFieldPlots`"] > pl1 = VectorFieldPlot[{1, Cos[x]}, {x, -5, 5}, {y, -5, 5}]; > > Manipulate[ > pl2 = Plot[Cos[x], {x, -5, -5 + k}, > PlotRange -> {-5, 5}, > PerformanceGoal -> "Speed" > ]; > Show[pl1, pl2, PlotRange -> {-5, 5} > ] > , > {{k, 1, "k"}, 1, 10}, TrackedSymbols :> {k}] > > -- End of Code > > As you can see, this simple example with Cos(x) is already > quite slow...when using more graphics options (eg colors) > and more complicated functions, the movie just becomes > much too slow, especially when considering that the actual > effort isn't that big. I even tried to pre-calculate an array of > pictures and just print them out in the Manipulate eg Array[[k]], > but even this is slow. > > Does anyone have an idea how to speed this up? Thanks in advance! > > Regards > > Michael Hsieh