# Manipulate and Turning Expressions into Functions

I've been trying to use Manipulate to do interactive plotting, but I've been running into a few problems with saved expressions. I have an expression saved as "func" and I want to work with it and then plot it later. But I can't seem to get it to work. For example, this works fine

Manipulate[Plot[Sin[a x + b], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


while this does not

func = Sin[a x + b];
Manipulate[Plot[func, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


I assume that this has something to do with when expressions are evaluated and the difference between expressions and functions, but I'm new to Mathematica and and I'm not sure on on the difference between the two.

I have tried other things like the following. For example, this works

Manipulate[Plot[Function[{a, b, x}, Sin[a x + b]][a, b, x], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


and this works

func2 = Function[{a, b, x}, Sin[a x + b]]
Manipulate[Plot[func2[a, b, x], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


but this does not

func = Sin[a x + b];
func2 = Function[{a, b, x}, func];
Manipulate[Plot[func2[a, b, x], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


How could I fix this and why doesn't it work the way I have it written?

• What in the last code bit is it that you thing doesn't work? It seems to work perfectly. Have you tried clearing your kernel and making sure you don't have other things interfering? – jVincent Feb 21 '13 at 9:31
• Thanks for the catch. I revised the question. – jmbejara Feb 21 '13 at 9:46

A simple straightforward way of doing this is to use With to inject the literal expression into the Manipulate.

func = Sin[a x + b];
With[{fun = func},
Manipulate[Plot[fun, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]
]


You'd need to use Dynamic@With... if you want the manipulate to update when func changes.

Just to be different:

func = Sin[a x + b];

func // Manipulate[Plot[#, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}] & • Very elegant. Thanks! – jmbejara Feb 21 '13 at 10:51
• I don't know if this is worth a new question, but why doesn't Manipulate[ Evaluate@Plot[Evaluate@func, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}] work? Manipulate[Evaluate@func, {a, 1, 4}, {b, 0, 10}] does work as expected. – Ajasja Feb 21 '13 at 13:59
• @Ajasja Imagine: what would Plot[func, {x, 0, 6}] evaluate to before values for a or b were provided? How would the result of that be useful in a Manipulate? – Mr.Wizard Feb 21 '13 at 14:11
• Hmm, to an empty plot? I think I get it. – Ajasja Feb 21 '13 at 14:24

One thing is to define the function using set delayed (rather than set). For instance

 func[x_, a_, b_] := Sin[a x + b];
Manipulate[Plot[func[x, a, b], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


gives an interactive plot that allows $a$ to control the frequency and $b$ to control the phase of the sinusoid.

• Thanks for the answer. The problem that I am running into, however, is that I have an expression that I would like to plot saved somewhere that is not already a function proper. So, for example, I have the following, both of which do not work. Clear["*"] someExpression = Sin[a x + b]; func[x_, a_, b_] := someExpression Manipulate[Plot[func[x, a, b], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}] Clear[func] func[x_, a_, b_] := Function[{x, a, b}, someExpression][x, a, b] Manipulate[Plot[func[x, a, b], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}] – jmbejara Feb 21 '13 at 9:57

Another solution is to use formal params in your function and replace them during your manipulate:

func = Sin[\[Alpha] x + \[Beta]];
Manipulate[Plot[func /. {\[Alpha] -> a, \[Beta] -> b}, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]

• +1 for another method. When you say "formal" I think of formal symbols e.g. \[FormalA], which might be a superior method as they cannot have values accidentally assigned. – Mr.Wizard Feb 21 '13 at 11:31
• So, to be sure I understand, \[FormalA] can never be assigned a value and so it works. \[Alpha] and the like, however, are not write protected in the same way that a and b are not protected, but still works. What is different, then, about \[Alpha] and \[Beta] that makes this work? Are they something in between regular a and \[FormalA]? – jmbejara Feb 21 '13 at 11:51
• @jmbejara No, \[Alpha] and \[Beta] are just like an other symbols. What makes this work has nothing to do with "formal symbols" but instead it is because a and b appear explicitly in the body of Manipulate. – Mr.Wizard Feb 21 '13 at 13:49

...and why doesn't it work the way I have it written?

Because Manipulate localizes a and b, so that the internal symbol does not match the symbol in your func expression. Similarly, Function localizes its variables, too.

A workaround for your last example is to Evaluate the function body:

func = Sin[a x + b];
func2 = Function[{a, b, x}, Evaluate@func];
Manipulate[Plot[func2[a, b, x], {x, 0, 6}], {a, 1, 4}, {b, 0, 10}]


but you can skip func2 altogether:

func = Sin[a x + b];
Manipulate[Plot[Function[{a, b}, Evaluate@func][a, b], {x, 0, 6}],
{a, 1, 4}, {b, 0, 10}]


Plot does not localize x, so you don't need to include it in the arguments.

To add a little bit of variety:

func = Sin[a x + b];
viewExpression[exp_] := Manipulate[Plot[exp, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}];
viewExpression@func This is very similar to Mr.W's answer, but uses the automatic scoping of named patterns with SetDelayed.

From the documentation:

When you apply a rule such as f[x_]->rhs, or use a definition such as
f[x_]:=rhs, Mathematica implicitly has to substitute for x everywhere
in the expression rhs. It effectively does this using the /. operator.
As a result, such substitution does not respect scoping constructs.
However, when the insides of a scoping construct are modified by the
substitution, the other variables in the scoping construct are renamed.

• I'm not seeing how this is helpful in this application. Can you give an example? – Mr.Wizard Feb 27 '13 at 17:30
• @Mr.Wizard Really? Well, thank you for pointing this out. This serves to inject an expression inside Manipulate[Plot[...]] just like with or a Function argument and is as such a direct answer. The example is the last line. – Ajasja Feb 27 '13 at 21:17
• @Mr.Wizard It's also a useful reminder that Manipulate can be returned just like any other expression. (which I tended to forget when I was learning MMA). Using a pattern declaration one can also better control evaluation as I learned here: mathematica.stackexchange.com/questions/5951/…) – Ajasja Feb 27 '13 at 21:27
• I suspect that I am confused by your wording. I read "automatic scoping of named patterns with SetDelayed" to refer to the automatic renaming of parameters (pattern names) in nested scoping constructs, e.g. With[{body = x^2}, Function[x, body]]. I am neither seeing that behavior here nor understanding how it would help; to the contrary it would seem that this renaming, if it happened, would prevent proper function of the Manipulate. If this is not what you refer to what do you mean by "automatic scoping"? – Mr.Wizard Feb 28 '13 at 4:41
• @Mr.Wizard I was refering to When you apply a rule such as f[x_]->rhs, or use a definition such as f[x_]:=rhs, Mathematica implicitly has to substitute for x everywhere in the expression rhs. It effectively does this using the /. operator. As a result, such substitution does not respect scoping constructs. However, when the insides of a scoping construct are modified by the substitution, the other variables in the scoping construct are renamed` from here reference.wolfram.com/mathematica/tutorial/…. – Ajasja Feb 28 '13 at 9:18