5
$\begingroup$

I want to show a multi-variable Manipulate plot with the current and last setting together but not using Bookmarks as I want to show both plots. Is there a way to capture the previous variable values and plot the line for the old values. My attempt does not update the last values.

Manipulate[Plot[{Sin[a x + b], Sin[lasta x + lastb]} , {x, -2 Pi, 2 Pi}, 
    PlotLabel -> Style["ai ="<>ToString[a]<>", bi ="<>ToString[b]<>
    "\n a(i-1) ="<>ToString[lasta]<>", b(i-1) ="<>ToString[lastb], 20]],
    {a, 0, 5} , {b, 0, \[Pi]}, SynchronousUpdating -> False, 
    Initialization :> {lasta = a; lastb = b;}]

This is what I want to achieve ...

This is what I want

Thanks to @belisarius's code here is the persistence view (see code in comments). Could reduce number of lines by using last n added using [[-n;;]] in the plot. Could also add a Reset button see "Reset" Button for Manipulate as Button["Reset", r = {{-5, 0}}]

Persistence Plot

$\endgroup$

3 Answers 3

5
$\begingroup$
Manipulate[
 Plot[{Sin[a x + b], Sin[lasta x + lastb]}, {x, -2 Pi, 2 Pi}, 
  PlotLabel -> 
   Style["ai =" <> ToString[a] <> ", bi =" <> ToString[b] <> 
     "\n a(i-1) =" <> ToString[lasta] <> ", b(i-1) =" <> 
     ToString[lastb], 20]],
 {a, 0, 5, TrackingFunction -> {(lasta = a; lastb = b); &, a = #; &, a = #; &}},
 {b, 0, N[Pi], TrackingFunction -> {(lasta = a; lastb = b); &, b = #; &, b = #; &}},
 {{lasta, 0}, None}, {{lastb, 0}, None}]

ScreenGIF

$\endgroup$
1
  • $\begingroup$ I selected this answer because it kept the last values rather than @belisarius as the continuous update lost the previous setting. If the number of variables was higher, this method would get unwieldy. $\endgroup$
    – ex-kiwi
    Commented Dec 10, 2015 at 14:08
2
$\begingroup$

This can be implemented directly using Dynamics and its second argument. TrackingFunction is new in 10.0, but all what it does is give access to the second argument of dynamics. Since it is not clear if you want the plot to show the last values as the slider is moving, or just when one finished moving the slider, there are two versions. The first one does the same thing as the solution above using tracking function, but using direct dynamics and it updates only at the end of the slider motion. The second one, updates as the slider is moving.

Manipulate[
 Plot[{Sin[a x + b], Sin[lasta x + lastb]}, {x, -2 Pi, 2 Pi}, 
  PlotLabel -> Evaluate@style[a, b, lasta, lastb]],

 Grid[{
   {"a ", Manipulator[
     Dynamic[a, {(lasta = a; a = #) &, (a = #) &, (a = #) &}], {0, 5, .1},
     ImageSize -> Small], Dynamic@a},
   {"b ", Manipulator[
     Dynamic[b, {(lastb = b; b = #) &, (b = #) &, (b = #) &}], {0, Pi, .1}, 
     ImageSize -> Small], Dynamic@b}
   }],
 {{a, 0}, None},
 {{lasta, 0}, None},
 {{b, 0}, None},
 {{lastb, 0}, None},
 TrackedSymbols :> {a, b},
 Initialization :>
  (
   style[a_, b_, lasta_, lastb_] :=
    Style["ai =" <> ToString[a] <> ", bi =" <> ToString[b] <> "\n a(i-1) =" <>
       ToString[lasta] <> ", b(i-1) =" <> ToString[lastb], 20]
   )
 ]

enter image description here

Manipulate[
 Plot[{Sin[a x + b], Sin[lasta x + lastb]}, {x, -2 Pi, 2 Pi}, 
  PlotLabel -> Evaluate@style[a, b, lasta, lastb]],

 Grid[{
   {"a ", Manipulator[
     Dynamic[a, {(lasta = a; a = #) &}], {0, 5, .1}, ImageSize -> Small], 
    Dynamic@a},
   {"b ", Manipulator[Dynamic[b, {(lastb = b; b = #) &}], {0, Pi, .1}, 
     ImageSize -> Small],
    Dynamic@b}
   }],
 {{a, 0}, None},
 {{lasta, 0}, None},
 {{b, 0}, None},
 {{lastb, 0}, None},
 TrackedSymbols :> {a, b},
 Initialization :>
  (
   style[a_, b_, lasta_, lastb_] :=
    Style["ai =" <> ToString[a] <> ", bi =" <> ToString[b] <> "\n a(i-1) =" <>
       ToString[lasta] <> ", b(i-1) =" <> ToString[lastb], 20]
   )
 ]

enter image description here

$\endgroup$
1
$\begingroup$
Manipulate[
 (AppendTo[r, {a, b}];
  Plot[
   {Sin[r[[-1]].{x, 1}],
    Sin[r[[-2]].{x, 1}]},
   {x, -2 Pi, 2 Pi}, PlotLabel -> Length@r]), {a, 0, 5}, {b, 0, π},
 TrackedSymbols :> {a, b}, Initialization :> (r = {a, b};)]

Mathematica graphics

$\endgroup$
1
  • 1
    $\begingroup$ I think the init. should be Initialization :> (r = {{a, b}}). Also you could extend this to full persistence by: Manipulate[(AppendTo[r, {a, b}]; Plot[Evaluate@Table[Sin[r[[i]].{x, 1}] , {i, Length[r]}], {x, -2 Pi, 2 Pi}, PlotLabel -> Length@r, PlotStyle -> Evaluate[ Table[Lighter[Blue, (Length[r] - j)/Length[r]], {j, Length[r]} ]]]), {a, -5, 5}, {b, 0, \[Pi]}, TrackedSymbols :> {a, b}, Initialization :> (r = {{a, b}};)] $\endgroup$
    – ex-kiwi
    Commented Dec 10, 2015 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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