7

My question is probably best illustrated with this small sample code:

Manipulate[
 Plot[{eqn1, eqn2, eqn1-eqn2}, {x,-5,5}],
 {{eqn1,r-x,"y1="}},{{eqn2,E^-x,"y2="}},
 {r,-2,2}
]

I would like to be able to have the user input two equations which including a variable r, and then use the slider to manipulate this variable. The issue is that Manipulate changes r within the input field, and then doesn't update the graphs.

Is there a way of allowing the user to input an equation which contains one of Manipulate's variables without having it change that variable within the input field?

JP-Ellis
  • 1,037
  • 7
  • 15
  • 1
    I wonder, why did you gave the title "Prevent..." when you explicitly want to have interdependence? – István Zachar Sep 27 '12 at 11:59
  • @IstvánZachar Well, I guess it depends what you see as being interdependent. I want the variable r to be interdependent, but not the controls themselves, so that change one control won't affect the input field itself.

    So essentially, the variable r is dependent on all controls, but the controls shouldn't be interdependent of each other.

    – JP-Ellis Sep 27 '12 at 15:19
  • So you only want to retain eqn1 as r - x in the InputField instead of e.g. 1-x (if r is 1)? If I'm correct, than indeed my first example is the way to do it. – István Zachar Sep 27 '12 at 17:01
  • Yep, that's correct, and indeed your first answer achieved exactly what I was hoping. The rest of the answer is an interesting exploration of the way Manipulate behaves, but isn't strictly answering my question. – JP-Ellis Sep 27 '12 at 23:19
  • Then I will split my answer and move the irrelevant parts to a new thread. – István Zachar Sep 28 '12 at 09:05

1 Answers1

7

The symbol r that is controlled by the slider is automatically localized so manually entering an expression containing (global) r does not work.

How about a simple replacement of r -> rr where rr is the Manipulate-d variable?

The undocumented option Evaluated -> True is used to retain individual line styles.(1)(2)

Manipulate[
 Plot[{eqn1, eqn2, eqn1 - eqn2} /. r -> rr, {x, -5, 5}, Evaluated -> True],
 {{eqn1, r - x, "y1="}},
 {{eqn2, E^-x, "y2="}},
 {{rr, -2, "r"}, -2, 2}]

Edited

Moved off-topic discussion on variable decoupling to this new post.

István Zachar
  • 47,032
  • 20
  • 143
  • 291
  • István, I edited your answer. I included an explanation of the need for the replacement rule as well as an option to preserve the unique line styling. I also found that the second form you showed did not work for me so I removed it; please add it back if you see fit. I hope this edit is not stepping on your toes; instead my intent is to enhance this answer rather than posting a competing one. – Mr.Wizard Sep 27 '12 at 06:11
  • Thanks Mr.Wizard, no toestepping at all. I wanted to include some explanations but wasn't able to figure out, so I left it for a later rewriting. You were faster, as usually :) I'm still not sure I understand though: if r is local to the slider, how come it is updated in the InputField, and not in the body of the Manipulate? My second solution works consistently on my end (though I don't like it, would rather wrap the whole thing in DynamicModule)... are you using v6 or 7? What could cause this discrepancy of versions? – István Zachar Sep 27 '12 at 06:28
  • @Mr.Wizard, Nasser: Please check update and provide some insight if you could. – István Zachar Sep 27 '12 at 12:00
  • @Nasser: you are right in that this example is not useful for the OP, but the underlying problem is that the initial value definition in a Manipulate is not enough to trigger body updates. My example is a remedy for that. – István Zachar Sep 27 '12 at 12:28
  • It is also possible to force the re-evaluation of x in the minimal example by making it dynamic. So Manipulate[{Dynamic[x], r}, {{x, r}}, {r, 0, 1}] will see both values being changed. In the context of the initial question however, this is not that useful since the input field is also modified. – JP-Ellis Sep 27 '12 at 15:33
  • @Nasser I think you should post a separate question. The issue here is that symbols are localized to $CellContext and parameters additionally with $$. For example: Manipulate[Hold[r], {r, -2, 2}]. You probably should use a manually constructed object with Dynamic and Control rather than Manipulate if you don't want the automatic scoping. – Mr.Wizard Sep 27 '12 at 21:44