0
$\begingroup$

This question already has an answer here:

I've a (perhaps too) basic questions that has been bugging me for a while. Manipulate seems to ignore global assignments. For instance the following does not result in the obvious output one would expect:

a = x*b;
Manipulate[Plot[a, {x, 0, 1}], {b, 1, 2}].

Instead, to had a 45 degree line, one had to redefine the a object within manipulation:

Manipulate[x*b;Plot[a, {x, 0, 1}], {b, 1, 2}].

Is there a way to have Manipulate to use the global assignment a=x*b? Thanks!!

$\endgroup$

marked as duplicate by Kuba, Artes, rm -rf Sep 13 '13 at 17:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ No always but often you can find interesting facts in Details & Options part for given function. For Manipulate below the table with options there is a bullet: Manipulate generates a DynamicModule object, with the variables u, v, etc. specified as local. $\endgroup$ – Kuba Sep 13 '13 at 15:09
  • $\begingroup$ Try making a snapshot of your Manipulate and you will see what is going on. $\endgroup$ – Yves Klett Sep 13 '13 at 15:53
1
$\begingroup$

When you introduced {b, 1, 2} as a control, you made b a local variable that shadows your global b. Two possible fixes are:

Manipulate[a := x b; Plot[a, {x, 0, 1}], {b, 1, 2}]

or

Manipulate[Plot[a[x, b], {x, 0, 1}],
  {b, 1, 2},
  Initialization :> (a[x_, b_] := x b)]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.