4
$\begingroup$

I want to display Plot[f,{t, t1, t2}] in a Manipulate with the domain {t1, t2} specified by slider controls with something like

Manipulate[
  Plot[t^2, {t, t1, t2}],
  {{t1, -1, "t1"}, -2, 2, 0.1},
  {{t2, 1, "t2"}, -2, 2, 0.1}]

In particular, I want to require t1 < t2 and to display a warning if t1 = t2 or t1 > t2. Although the above code produces an error when t1 = t2, Mathematica appears to switch the roles of t1 and t2 when the sliders are set to t1 > t2.

I have tried the control statements

{{t1, -1, "t1"}, -2, 2, 0.1},
{{t2, 1, "t2"}, t1, 2, 0.1}

but to no avail.

An alternative solution is just to eliminate the sliders and instead provide input fields for t1 and t2.

$\endgroup$
  • 2
    $\begingroup$ Have you considered If[t1 < t2, Plot[...], warning]? Put it inside a Pane to control ImageSize if desired. $\endgroup$ – Michael E2 Aug 15 '15 at 12:01
  • $\begingroup$ I replaced the Plot statement with Pane[If[ t1 < t2, Plot[t^2, {t, t1, t2}], Style["Require t2 > t1"]]. That works nicely. $\endgroup$ – Stephen Aug 15 '15 at 12:32
  • 2
    $\begingroup$ You could put your comment into an answer. :) Also, if you have V10, then there's [IntervalSlider](http://reference.wolfram.com/language/ref/IntervalSlider.html), which might help you avoid the issue entirely, if that would be preferable. $\endgroup$ – Michael E2 Aug 15 '15 at 12:40
  • $\begingroup$ @Michael E2: IntervalSlider - a great solution! $\endgroup$ – Stephen Aug 15 '15 at 13:00
3
$\begingroup$

There are couple of ways to handle your problem.

Simple -- give a warning of slider interference:

Manipulate[
  If[t1 < t2, Plot[t^2, {t, t1, t2}], "Warning: t1 > t2"],
  {{t1, -1}, -2, 2, 0.1, Appearance -> "Labeled"},
  {{t2, 1}, -2, 2, 0.1, Appearance -> "Labeled"}]

More elegant user interface, less elegant code -- prevent slider interference:

I have to give up Manipulate's simple control specification format for the sliders because I want to use the second argument of Dynamic to constrain the slider thumbs.

Manipulate[
  Plot[t^2, {t, t1, t2}],
  Row[{
    "t1 ", 
    Slider[Dynamic[t1, (t1 = If[# >= t2, t1, #]) &], {-2, 2, 0.1}, 
      Appearance -> "Labeled"]}],
  Row[{
    "t2 ", 
    Slider[Dynamic[t2, (t2 = If[# <= t1, t2, #]) &], {-2, 2, 0.1}, 
      Appearance -> "Labeled"]}],
  {{t1, -1}, None},
  {{t2, 1}, None}]

I prefer the second method because I believe it is better to prevent bad things from happening than to give warnings that they have happened.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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