I want to use a Manipulate that has two controllers that end up as a difference in the denominator of an expression. It is easy enough to set up the initial conditions such that the two controls are not equal in value, but upon user manipulation the two controls, if set the same give an error. The following code illustrates:

  table = Range[0, 6];
  divideByZeroProblem = 
    10/(time2 - time1), {{time1, 0}, Most[table], 
     ControlType -> PopupMenu}, {{time2, time1 + 1}, 
     Drop[table, Position[table, time1][[1, 1]]], 
     ControlType -> PopupMenu}]


But of course if I change time1 to be 1, then I get the following:

Bad Output

I naively thought that I might be able to utilize Initialization to get around the problem by basing the initial value of time2 on a function, but that doesn't update the initial value set for time2.

The best lead that I have found in my searching for the answer on Mathematica.SE is Control variable and body variable decoupling in Manipulate, but I didn't succeed in translating that answer into a solution for my problem. It did lead me to try some things though.

For example, my initial attempts at writing this question resulted in a simpler code that works as I would like it to.[Note added in later edit: I swear that it worked once this way, but I cannot get it to work again. It has the exact same kind of error. Must have been dreaming.]

    divideByZeroProblem2 = 
      10/(time2 - time1), {{time1, 0}, Range[0, 5], 
       ControlType -> PopupMenu}, {{time2, Dynamic[time1] + 1}, Range[1,6],ControlType->PopupMenu}]

I played around quite a bit with using Dynamic and Setting in opportune spots to see if I could get it to work, but failed to find an appropriate combination.

Any help that folks could provide would be much appreciated.


1 Answer 1


Maybe I've misunderstood the requirement, but why not simply apply a constraint to both controls?

Manipulate[10/(time2 - time1),
 {{time1, 0}, Select[Range[0, 6], # < time2 &], ControlType -> PopupMenu},
 {{time2, 1}, Select[Range[0, 6], # > time1 &], ControlType -> PopupMenu}]
  • $\begingroup$ That looks great, Simon. It is also necessary for time2 > time1. $\endgroup$ Mar 20, 2014 at 17:11
  • $\begingroup$ Ok, that is awesome! Thanks, I still am not used to the Mathematica vernacular so to speak. $\endgroup$ Mar 20, 2014 at 17:21
  • $\begingroup$ Thanks very much for the help. It was exactly the fix that I needed. It worked perfectly in the actual code that I needed. Should have seen the solution myself, but that is inexperience for you. $\endgroup$ Mar 20, 2014 at 18:55

Your Answer

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

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