3
$\begingroup$

In the minimum working example below I make a function in the top graph and then by pressing the button I use it in a calculation (much, much simpler than my actual calculation) and then plot the result in the bottom graph. The bottom graph also contains the function made in the top graph.

DynamicModule[{pts, f, g},
 pts = Table[{x, 1}, {x, 0, 3 π, π/2.}];
 f = Interpolation[pts];
 Column[{
   LocatorPane[
    Dynamic[pts,
     (
       pts = #;
       pts[[1, 1]] = 0;
       pts[[-1, 1]] = 3 π;
       f = Interpolation[pts]
       ) &
     ],
    Dynamic@Plot[f[x], {x, 0, 3 π}, PlotRange -> {All, {0, 2}}]
    ],
   Button["Calculate", g[x_] := Evaluate[f[x]^2], ImageSize -> 1 72],
   Dynamic[Plot[g[x] + f[x], {x, 0, 3 π}]]
   }]
 ]

Mathematica graphics

The problem is that the bottom graph changes when I change the top graph. I only want it to change after I have pressed the button. How do I do that?

I made one try by using the second argument to dynamic. If the second argument to dynamic is None then the dynamic should stop being interactive. The modified code is as follows

DynamicModule[{pts, f, g, action = None},
 pts = Table[{x, 1}, {x, 0, 3 π, π/2.}];
 f = Interpolation[pts];
 Column[{
   LocatorPane[
    Dynamic[pts,
     (
       pts = #;
       pts[[1, 1]] = 0;
       pts[[-1, 1]] = 3 π;
       f = Interpolation[pts];
       action = None
       ) &
     ],
    Dynamic@Plot[f[x], {x, 0, 3 π}, PlotRange -> {All, {0, 2}}]
    ],
   Button["Calculate", g[x_] := Evaluate[f[x]^2]; action = Automatic, 
    ImageSize -> 1 72],
   Dynamic[Plot[g[x] + f[x], {x, 0, 3 π}], action]
   }]
 ]

However this does not work. Why? Thanks for any ideas.

$\endgroup$
4
$\begingroup$

No need to change the original code. You have to add TrackedSymbols :> {g} to your second Dynamic

DynamicModule[{pts, f, g}, 
 pts = Table[{x, 1}, {x, 0, 3 π, π/2.}];
 f = Interpolation[pts];
 Column[{LocatorPane[Dynamic[pts, (pts = #;
       pts[[1, 1]] = 0;
       pts[[-1, 1]] = 3 π;
       f = Interpolation[pts]) &], 
    Dynamic@Plot[f[x], {x, 0, 3 π}, PlotRange -> {All, {0, 2}}]], 
   Button["Calculate", g[x_] := Evaluate[f[x]^2], ImageSize -> 1 72], 
   Dynamic[Plot[g[x] + f[x], {x, 0, 3 π}], 
    TrackedSymbols :> {g}]}]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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