7
$\begingroup$

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?

$\endgroup$
5
  • 1
    $\begingroup$ I wonder, why did you gave the title "Prevent..." when you explicitly want to have interdependence? $\endgroup$ Sep 27, 2012 at 11:59
  • $\begingroup$ @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. $\endgroup$
    – JP-Ellis
    Sep 27, 2012 at 15:19
  • $\begingroup$ 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. $\endgroup$ Sep 27, 2012 at 17:01
  • $\begingroup$ 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. $\endgroup$
    – JP-Ellis
    Sep 27, 2012 at 23:19
  • $\begingroup$ Then I will split my answer and move the irrelevant parts to a new thread. $\endgroup$ Sep 28, 2012 at 9:05

1 Answer 1

7
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Sep 27, 2012 at 6:11
  • $\begingroup$ 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? $\endgroup$ Sep 27, 2012 at 6:28
  • $\begingroup$ @Mr.Wizard, Nasser: Please check update and provide some insight if you could. $\endgroup$ Sep 27, 2012 at 12:00
  • $\begingroup$ @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. $\endgroup$ Sep 27, 2012 at 12:28
  • $\begingroup$ 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. $\endgroup$
    – JP-Ellis
    Sep 27, 2012 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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