12
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – jVincent
    Feb 21, 2013 at 9:31
  • $\begingroup$ Thanks for the catch. I revised the question. $\endgroup$
    – jmbejara
    Feb 21, 2013 at 9:46

6 Answers 6

14
$\begingroup$

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.

$\endgroup$
8
$\begingroup$

Just to be different:

func = Sin[a x + b];

func // Manipulate[Plot[#, {x, 0, 6}], {a, 1, 4}, {b, 0, 10}] &

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Very elegant. Thanks! $\endgroup$
    – jmbejara
    Feb 21, 2013 at 10:51
  • $\begingroup$ 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. $\endgroup$
    – Ajasja
    Feb 21, 2013 at 13:59
  • $\begingroup$ @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? $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 14:11
  • $\begingroup$ Hmm, to an empty plot? I think I get it. $\endgroup$
    – Ajasja
    Feb 21, 2013 at 14:24
7
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ 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}] $\endgroup$
    – jmbejara
    Feb 21, 2013 at 9:57
6
$\begingroup$

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}]
$\endgroup$
3
  • 1
    $\begingroup$ +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. $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 11:31
  • $\begingroup$ 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]? $\endgroup$
    – jmbejara
    Feb 21, 2013 at 11:51
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Feb 21, 2013 at 13:49
5
$\begingroup$

...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.

$\endgroup$
4
$\begingroup$

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

Mathematica graphics

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.
$\endgroup$
7
  • $\begingroup$ I'm not seeing how this is helpful in this application. Can you give an example? $\endgroup$
    – Mr.Wizard
    Feb 27, 2013 at 17:30
  • $\begingroup$ @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. $\endgroup$
    – Ajasja
    Feb 27, 2013 at 21:17
  • $\begingroup$ @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/…) $\endgroup$
    – Ajasja
    Feb 27, 2013 at 21:27
  • $\begingroup$ 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"? $\endgroup$
    – Mr.Wizard
    Feb 28, 2013 at 4:41
  • $\begingroup$ @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/…. $\endgroup$
    – Ajasja
    Feb 28, 2013 at 9:18

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.