9
$\begingroup$

I want to do something like this:

expr := x^e;
Manipulate[Plot[expr, {x, 0, 4}], {e, 1, 3}]

But the plot shows up empty.

Yes, it works if I do:

expr[x_,e_] := x^e;
Manipulate[Plot[expr[x,e], {x, 0, 4}], {e, 1, 3}]

But I don't want to do that. Why? Because the function I'm actually working with has about a dozen variables. I want to be able define my function just as as expression (that I use in many places throughout my notebook, in addition to this particular Manipulate) so that I don't have to bother with remembering the order in which I defined the dozen or so variables that comprise the function definition. This way I can substitute values into my function using ReplaceAll as I see fit.

My approach has been working fine as I continue to shape this function, but using Manipulate becomes very cumbersome. I want to manipulate several of these variables without having to worry about typing a whole lot of code. Is there a way I can keep the definition of my function as a simple Set or SetDelayed (without the pattern matching) but change something about my Manipulate expression to make the Manipulate function actually manipulate symbols that are not do not explicitly occur within the Manipulate expression? Thanks!

$\endgroup$
4
  • $\begingroup$ Related: (10604), (25087) $\endgroup$
    – Mr.Wizard
    Aug 29, 2013 at 9:22
  • $\begingroup$ None of these answers were satisfactory to me, so I kept on poking and did find a better way to do it. I'd like to add a new answer, but this question won't let me since it's marked as a duplicate. Can someone help by un-marking this question as a duplicate. The additional answer I'd like to post (that I ended up using) is more relevant to this question than it is to the other "duplicate" question, so I'd rather not post my answer on the other question. I will explain all of this in my answer. $\endgroup$
    – Sean
    Aug 29, 2013 at 13:59
  • $\begingroup$ Sean, I shall reopen this question. In the future if you want to make sure I (or another moderator) sees your request you can flag your own post "other" and ask. (use the link below the tags). $\endgroup$
    – Mr.Wizard
    Aug 29, 2013 at 19:04
  • 2
    $\begingroup$ Your solution is not different in mechanism from the existing answers and far more complicated than it needs to be. I have re-closed this question. $\endgroup$
    – Mr.Wizard
    Aug 31, 2013 at 1:01

3 Answers 3

14
$\begingroup$

The control variable must appear in the Manipulate expression to be tracked. In this case it is e

Make expr a true function which is done by passing it all the arguments it needs, and then call it from inside Manipulate expression:

expr[e_, x_] := x^e;
Manipulate[Plot[expr[e, x], {x, 0, 4}],
 {e, 1, 3}]

Mathematica graphics

Now you can use your function outside of Manipulate, and have it used by Manipulate as well, which is what your goal was.

Second option

But if you really insist in making e appear outside and still use the expression from inside Manipulate, then use the LocalizeVariables -> False option

expr := x^e;
Manipulate[
 e;
 Plot[expr, {x, 0, 4}],
 {e, 1, 3},
 LocalizeVariables -> False
 ]

Mathematica graphics

But notice that e was added in the Manipulate expression, just so that it is tracked.

$\endgroup$
2
  • $\begingroup$ For completeness, one should also add Initialization :> (e = 1) so that the Manipulate appears correctly when first initiated (this is only required if e has some nonstandard specification, e.g. {{e,1}, 10^-3, 3}). Also, TrackedSymbols :> {e} makes the explicit inclusion of e within the Manipulate body unnecessary. $\endgroup$ Oct 30, 2017 at 10:22
  • $\begingroup$ @IstvánZachar true. But I was using the same setup as shown in the original post. $\endgroup$
    – Nasser
    Oct 30, 2017 at 18:32
6
$\begingroup$

The reason why this does not work is because the variables in a Manipulate do not actually have the name you see displayed - they have scoped ones. For instance, Manipulate[Hold[x] // Print, {x, 1, 3}]; prints Hold[FE'x$$9]. You can use ReplaceAll to transfer from the actual name of the variables to the manipulate placeholder:

expr := x^e;
Manipulate[Plot[expr /. e -> eprime, {x, 0, 4}], {eprime, 1, 3}]

enter image description here

$\endgroup$
5
$\begingroup$

Here's what I ended up using:

expr := x^e;
Manipulate[Plot[#, {x, 0, 4}], {e, 1, 3}] &@expr

Which works great and doesn't require extra function arguments or replacement rules.

$\endgroup$
2
  • 2
    $\begingroup$ Your solution could be slimmed down a bit: expr := x^e; Manipulate[Plot[exprp, {x, 0, 4}], {e, 1, 3}] /. exprp -> expr. No need to apply, just inject. $\endgroup$
    – VF1
    Aug 29, 2013 at 23:00
  • $\begingroup$ This was exactly what I was looking for. Thank you so much for posting this! $\endgroup$ Aug 25, 2022 at 17:54

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