Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
@seismatica For a simple Manipulate like this one I find your approach rather clever. –  eldo Jun 25 at 12:50

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  seismatica Jun 26 at 2:20
1  
@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. –  Phab Jun 26 at 6:39
    
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? –  seismatica Jun 26 at 17:36
1  
@seismatica Making animated GIFs is easy using LICEcap (link). –  Phab Jun 27 at 6:50
    
you rock! ugh 15 char! –  seismatica Jun 27 at 7:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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