7
$\begingroup$

Consider this Manipulate

Manipulate[{x, y}, {{x, 0}, -2, 2}, {{y, 0}, -2, 2}] 

manipulate

Each of the sliders can move freely and independently. But, I want to impose a constrain: the difference between $x$ and $y$ can't exceed $1$, i.e. $|x-y|\le 1$. For example, when $x=0$ slider $y$ can move freely from $-1$ to $1$, but when $y$ reaches $1$ and we increase its value further, then this action will drag the slider $x$ away from $0$ to keep $|x-y|=1$. That is, when $x=0$ and $y=1$ and we change $y$ to $1.2$ then $x$ will jump to $0.2$ to satisfy $|x-y|=1$, but if we change $y$ back to $1$ then this won't affect $x$ since $|x-y|\le 1$ is not violated. This behavior must be symmetrical between $x$ and $y$. Is it possible to construct this conduct in Manipulate? How do we do this?

$\endgroup$

1 Answer 1

10
$\begingroup$

Try and if this works for you

enter image description here

enter image description here

enter image description here

enter image description here

code

Manipulate[ {x, y}, Grid[{ {"x", Manipulator[Dynamic[x, (x = #; If[Abs[x - y] > 1, If[y > x, y = x + 1, y = x - 1]]) &], {-2, 2, .01}]}, {"y", Manipulator[Dynamic[y, (y = #; If[Abs[x - y] > 1, If[x > y, x = y + 1, x = y - 1]]) &], {-2, 2, .01}]} }], {{x, 0}, None}, {{y, 0}, None}, TrackedSymbols :> {x, y} ] 
$\endgroup$
6
  • $\begingroup$ +1 I was wondering if you have any notes about the design of Manipulate on your personal page. I think I came across something related to that, but I’m not sure how much content you have on the topic. :) $\endgroup$ Commented Dec 15, 2024 at 7:20
  • $\begingroup$ There is unexpected behavior: set both $x$ and $y$ to $2$ an then gradually lower the value of $y$, when $y$ hits $0.9$ the value of $x$ jumps from $2$ to $-0.1$ (it should be $1.9$) $\endgroup$ Commented Dec 15, 2024 at 7:24
  • 1
    $\begingroup$ @E.Chan-López There are some specific notes on Dynamics in this section Dynamics, Manipulate, animations and related some collected from different source such as Wolfram and John Fultz and from email from WRI tech. Other than this, it is trial and error :) $\endgroup$ Commented Dec 15, 2024 at 7:24
  • $\begingroup$ @Naraghazi there could be a bug, Will look at it. I just did very basic testing, Might need an extra condition added. $\endgroup$ Commented Dec 15, 2024 at 7:25
  • $\begingroup$ @Nasser I appreciate your response and the link to the section on Dynamics. It’s great to know there’s a mix of sources, and I’ll definitely take a look at it. Trial and error is often the best teacher, too! Thanks again for sharing. :) $\endgroup$ Commented Dec 15, 2024 at 7:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.