Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question

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.

    
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. –  Kuba Sep 13 '13 at 15:09
    
Try making a snapshot of your Manipulate and you will see what is going on. –  Yves Klett Sep 13 '13 at 15:53
add comment

1 Answer 1

up vote 1 down vote accepted

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)]
share|improve this answer
add comment

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