# Manipulate using a variable defined outside of the manipulate function [duplicate]

This question already has an answer here:

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!

-

## marked as duplicate by Mr.Wizard♦Aug 31 '13 at 1:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Related: (10604), (25087) –  Mr.Wizard Aug 29 '13 at 9:22
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. –  Sean Madsen Aug 29 '13 at 13:59
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). –  Mr.Wizard Aug 29 '13 at 19:04
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. –  Mr.Wizard Aug 31 '13 at 1:01

## 3 Answers

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}]


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
]


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

-

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'x9]. 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}]


-

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.

-
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. –  VF1 Aug 29 '13 at 23:00