ejeh
(Ernesto Espinosa)
November 6, 2015, 8:00pm
1
n = 1;
Slider[Dynamic[n], {0, 2 Pi}]
Show[
Plot[Sin , {x, 0, 2 Pi}, PlotStyle → Black],
Dynamic[Plot[Sin , {x, 0, n},
PlotStyle → Red]] (* Only this line *)
]
I want to run Dynamic exclusively in the second graph. I do not know where to put the instruction Dynamic in that line.
nma
(Nasser M. Abbasi)
November 6, 2015, 8:46pm
2
You can use DynamicModule
DynamicModule[{n = 1},
Row[{Slider[Dynamic[n], {0, 2 Pi}],
Dynamic@Show[Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black],Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]]}]
]
Or better use Manipulate
Manipulate[
Show[
Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black],Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]],
{{n, 1, "n"}, 0, 2 Pi, Appearance -> "Labeled"}
]
ejeh
(Ernesto Espinosa)
November 6, 2015, 10:02pm
3
Maybe I was not clear.
I just wish that the Dynamic affect the second Plot inside the Show.
In the examples provided, Dynamic affects both Plots.
I want to optimize the code, in case the first Plot doesn’t need Dynamic.
Thank you for your attention
nma
(Nasser M. Abbasi)
November 6, 2015, 11:06pm
4
But Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black] has no n in it. So it does not depend on dynamics of n changing at all.
If you show an example of what is it you are trying to optimize, it will help.
i.e. it is the same as if you wrote
DynamicModule[{n = 1, m = Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> Black]},
Row[{Slider[Dynamic[n], {0, 2 Pi}],
Dynamic@Show[m,Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]]}]]
Dynamic can not be inside Show, since it is not graphics object, so has to be outside Show.
But again, if you show an example where it matters, it will help, since in your example, the first plot has no dynamics in it, so it will not affect the performance.
ejeh
(Ernesto Espinosa)
November 7, 2015, 7:26pm
5
n = 1;
Animator[Dynamic[n], {0.01, 2 Pi}]
Dynamic@Show[Plot[Sin[1/x^2], {x, -2 Pi , 2 Pi}, PlotStyle → Black],
Plot[Sin , {x, 0, n}, PlotStyle → Red]
]
I send a concrete example.
When I run the Animator, the first graph distorts his output and returns to normal when I stop the Animator.
This is uncomfortable for a presentation.
If I can not use Dynamic in Show, and I will see how I solve this dilemma.
It would be very convenient to have the flexibility to choose which function to use Dynamic separate, in execution.
Thanks again. Best regards
nma
(Nasser M. Abbasi)
November 7, 2015, 8:37pm
6
You can eliminate the update issue by evaluating the static plot outside Dynamics as follows
n = 1;
m = Plot[Sin[1/x^2], {x, -2 Pi, 2 Pi}, PlotStyle -> Black];
Animator[Dynamic[n], {0.01, 2 Pi}]
Dynamic@Show[m, Plot[Sin[x], {x, 0, n}, PlotStyle -> Red]]
Side note: Show picks plot attributes (PlotRange, etc…) from the first plot. If this is not what you really want, You can override this, or change the order of the plots inside Show.
ejeh
(Ernesto Espinosa)
November 8, 2015, 2:45am
7
I want to thank Nasser M. Abbassi for this dialogue with me.
It made me reflect on small mistakes commited by me.
Thanks Nasser.