Re: novice needs help using Manipulate with Plot
- To: mathgroup at smc.vnet.net
- Subject: [mg78437] Re: novice needs help using Manipulate with Plot
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 2 Jul 2007 06:40:16 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f68458$eoh$1@smc.vnet.net>
PHILLMAN5 at gmail.com wrote:
> Manipulate seems at first to be very powerful, but I am having trouble
> using it with my own functions. To vastly simplify my problem say I
> want to plot y = m x, with x going from 0 to 10, with the slider in
> Manipulate controlling m. I have tried the following:
>
> test := m x
> Manipulate[Plot[test, {x, 0, 10}], {{m, 1}, 0, 2}]
>
> test3[x_] := m x;
> Manipulate[Plot[test3[x], {x, 0, 10}], {{m, 1}, 0, 2}]
>
> don't seem to work. If you define the function with m as a formal
> parameter, like the following it does.
>
> test2 = #1 #2 &;
> Manipulate[Plot[test2[m , x], {x, 0, 10}], {{m, 1}, 0, 2}]
>
> test4[m_, x_] := m x;
> Manipulate[Plot[test4[m, x], {x, 0, 10}], {{m, 1}, 0, 2}]
>
> Is there anyway to write functions to work with Manipulate without
> have to have all the slider(s) formally written as a parameter to the
> function?
The issue you face is that for Mathematica the parameters 'm' are not
the same in the function test (global scope) and in *Manipulate* (local
scope). Indeed the global 'm' is named 'm' whereas the local 'm' is name
'm$xxx' (xxx being some number produced by Mathematica that guarantees
the uniqueness of the local symbol 'm').
If you do not want to change the definition your function(s), you could
use a transformation rule instead, as in the following example:
f[x_] = m x;
Manipulate[Plot[f[x] /. m -> u, {x, 0, 10}], {{u, 1}, 0, 2}]
HTH,
Jean-Marc