Re: Manipulate function
- To: mathgroup at smc.vnet.net
- Subject: [mg100935] Re: [mg100926] Manipulate function
- From: John Fultz <jfultz at wolfram.com>
- Date: Thu, 18 Jun 2009 06:18:50 -0400 (EDT)
- Reply-to: jfultz at wolfram.com
On Thu, 18 Jun 2009 04:54:34 -0400 (EDT), Przemyslaw Swatek wrote: > hi ! > > I have some problem (Mathematica 6) - when I type > Manipulate[Plot[If[x < 0, n Sin[x], Cos[x]], {x, -10, 10}], {n, 0, 1}] > all is ok, but when i try: > > M[x_]:=If[x < 0, n Sin[x], Cos[x]], {x, -10, 10}] and then > > Manipulate[Plot[M[x], {x, -10, 10}], {n, 0, 1}] it doesn`t work - I > cannot manipulate varible of n. Whay ? Manipulate (via DynamicModule) is using lexical scoping, which means that it only replaces values of 'n' which are literally in the body of the Manipulate expression. 'n' shows up nowhere in the expression 'M[x]'. If you tried to do a similar substitution for values of 'n' using Module or With, you'd run into a similar problem. Only Block[], with its use of dynamic scoping, is capable of doing this. Look for the following in the help viewer for more info: tutorial/BlocksComparedWithModules The correct way to deal with this is to add a second argument to M which represents the variable 'n'. I.e., M[x_, n_] := If[x < 0, n Sin[x], Cos[x]] Manipulate[Plot[M[x, n], {x, -10, 10}], {n, 0, 1}] Sincerely, John Fultz jfultz at wolfram.com User Interface Group Wolfram Research, Inc.