0
$\begingroup$

I have this Manipulate:

Manipulate[Plot[Sin[a x], {x, 0, 2 Pi}], {a, 0, 3}]

Sin[a x]

Now, I realize I had mistyped the expression and it's actually x^2 instead. I go back and fix it, but when I evaluate the cell again, the value of a reverts to default/zero.

Mathematica graphics

I've tried multiple things from various Q&A: adding SaveDefinitions -> True, saving parameter to a global variable within the Manipulate block, using bookmarks, but none of them achieved the effect of having the Manipulate start up with a previously-set control value.

Interesting, I find that saving the parameter to a global variable, and then initialize the parameter with the same global variable solves the problem, i.e., Manipulate can remember the manipulated parameter from a previous run. If I add the option SaveDefinitions -> True, the value is also saved between sessions.

Below is an example: a remains at the same value even when the expression in Manipulate was changed and the cell was evaluated.

Mathematica graphics

Mathematica graphics

I went to the chat room to ask people if doing this is OK. I was told that I should not do this. What kind of potential issues would I have with this method? And if it's bad, is there another method to achieve the same result, i.e., preventing Manipulate from resetting control values after each evaluation?

$\endgroup$
1
  • 1
    $\begingroup$ @seismatica For a simple Manipulate like this one I find your approach rather clever. $\endgroup$
    – eldo
    Jun 25, 2014 at 12:50

1 Answer 1

2
$\begingroup$

Why don't you use a lokal variable?

Define a local variable first, then do your plot:

In[1]:= startingA = 0;

In[2]:= Manipulate[
           startingA = a;
           Plot[Sin[a x^2], {x, 0, 2 Pi}]
           , {{a, startingA}, 0, 3}]

(Note: The vaiable has NOT to be in the same evaluation cell as Manipulate[]!)

The first time a starts at the defined value zero (it could be any number inside the given range).

enter image description here

Change a:

enter image description here

Now you can change the function in the Plot and eveluate Manipulate[] again. a start at your last a/startingA:

Manipulate[
 startingA = a;
 Plot[Sin[a x], {x, 0, 2 Pi}]
 , {{a, startingA}, 0, 3}]

enter image description here

Animated GIF with the complete process: enter image description here

Giving the Range in Manipulate as

{{a, startingA}, 0, 3}

says Manipulate should change the value "a" in the range between 0 and 3 with the starting value "startingA". One could give the range also as

{{a, startingA}, 0, 3, 1}

In this case "1" defines the stepsize.

$\endgroup$
6
  • $\begingroup$ Could you help me understand what {{a,startingA},0,2} is supposed to me? Is this the same thing as initializing a with startingA. I've never seen a range written like this before. $\endgroup$
    – seismatica
    Jun 26, 2014 at 2:20
  • 1
    $\begingroup$ @seismatica It says Manipulate[] to change a between 0 and 2 starting at startingA. With initializing it first to zero, to first startpoint for a is zero. With startingA=a; in Manipulate[] you're saving your current a to startingA in every step. (And startingA is stored outside Manipulate[].) So next time you evaluate Manipulate[] again, startingA has still the "old value of a" and will start at this point with changing a. ... I may should edit this in my answer. $\endgroup$
    – Phab
    Jun 26, 2014 at 6:39
  • $\begingroup$ Thank you so much for your explanation and effort to clarify your answer. This fix is working well for me. BTW how did you make that gif? $\endgroup$
    – seismatica
    Jun 26, 2014 at 17:36
  • 1
    $\begingroup$ @seismatica Making animated GIFs is easy using LICEcap (link). $\endgroup$
    – Phab
    Jun 27, 2014 at 6:50
  • $\begingroup$ you rock! ugh 15 char! $\endgroup$
    – seismatica
    Jun 27, 2014 at 7:27

Your Answer

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

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