I have two different Mathematica notebooks with similar, but different functions. Both work fine when they're the only notebook open. One of them consistently fails when the other notebook is open, despite my (liberal) use of Clear[] to clear out the relevant variables.
Call this one, say, GlobalManipulate
ClearAll["Global`*"]
Clear["Global`*"]
Definition[linear]
linear[x_] := a x;
quad[x_] := a x^2;
functionList := {linear, quad};
Manipulate[
Plot[function[dummy], {dummy, -10, 10}],
{function, functionList}, {a, -10, 10},
LocalizeVariables -> False, TrackedSymbols -> All
]
Call this one, say, LocalManipulate
Clear["Global`*"];
Manipulate[
{
linear := a x; quad := a x^2;
linear, quad, function,
Plot[ReleaseHold@function, {x, -10, 10}]
},
{function, {HoldForm@linear, HoldForm@quad}},
{a, -10, 10}, TrackedSymbols -> All
]
When run by itself, GlobalManipulate works as expected and I see the plot that updates as a
is changed. The definition of linear produces Null
.
When LocalManipulate is open, running, GlobalManipulate no longer works. even when it's rerun It's plot appears for a second and then vanishes.
I've reproduced this using my local copy of Mathematica 8 and a remote copy of Mathematica 7.
The problem must involve the functions linear[x_]
and quad[x_]
since
GlobalManipulatePrime:
ClearAll["Global`*"]
Clear["Global`*"]
Definition[linear]
linear1[x_] := a x;
quad1[x_] := a x^2;
functionList := {linear1, quad1};
Manipulate[
Plot[function[dummy], {dummy, -10, 10}],
{function, functionList}, {a, -10, 10},
LocalizeVariables -> False, TrackedSymbols -> All
]
works fine.
Edited to add bold text to stress that I'm rerunning Global, and that I'm trying to figure out why functions persist despite my ClearAll[].
Inside your LocalManipulate you have the definitions
linear := a x; quad := a x^2
.
Since these are not declared in the controller part of the Manipulate
command, they are not localized and thus overwrite the global variables and break your GlobalManipulate.
The simplest way to localize variables inside a Manipulate
is to use add them in with the controller type None
. Below I've modified your LocalManipulate code, so that the last line makes linear
and quad
local to that particular Manipulate
(DynamicModule
)
Manipulate[{linear := a x; quad := a x^2; linear, quad, function,
Plot[ReleaseHold@function, {x, -10, 10}]},
{function, {HoldForm@linear, HoldForm@quad}}, {a, -10, 10},
{linear, None}, {quad, None}, TrackedSymbols -> All]
Apart from wrapping the functions in Module
or DynamicModule
and localizing the variables you could also place the notebooks in individual contexts using the Evaluation ► Notebook's Default Context
menu.
You could also set the context using the function Begin
. This should all be done before your functions are defined.
Update
I didn't read your question too well I think. As to the cause of ClearAll not working:
linear:=FE`a$$51 x
linear
in FE`a$$51 x
the line functionList := {linear, quad};
in GlobalManipulate now actually means functionList := {FE`a$$51 x,FE`a$$53 x^2}
which, out of the other Manipulate
context, doesn't mean anything, so your plot disappearsClearAll
remove this definition? This has to do with the properties of the dynamic variables buried in the Manipulate box visible in the LocalManipulate notebook. They keep on getting redefined there. Remove the output of the Manipulate and re-execute GlobalManipulate and you'll see it now works. Something similar is discussed here.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With