1
$\begingroup$

In manipulate, I need to define two sliders : one basic slider, and a second one for which the end value depends on the current value selected on the first slider. How can I achieve this in Mathematica 7, without having a red glow when the second parameter is out of range ?

Here's a nice MWE code to work with :

UPDATE : I updated the MWE below, to show the annoying red glow on the second slider :

Manipulate[Plot[A Sin[2 Pi f t/12], {t, 0, 12},
PlotRange -> {{0, 12}, {-1, 1}},
AspectRatio -> 1,
Frame -> True,
Axes -> True,
ImageSize -> 600],
Row[{
    Control[{{f, 1, "frequency"}, -10, 10, 0.01, Appearance -> {"Labeled", "Closed"}}],
    Spacer[125],
    Control[{{A, 0.1, "Amplitude"}, 0, Dynamic[Which[f < 0, 0.5, f >= 0, 1]], 0.01, Appearance -> {"Labeled", "Closed"}}]
    }],
ControlPlacement -> Bottom
]

This working code shows a simple oscillation with 2 adjustable parameters : a frequency (from -10 to 10) and an amplitude (from 0 to 0.5 if f < 0, and from 0 to 1 if f > 0). The amplitude parameter is partly dependant on the frequency selected.

When the amplitude is set to its max value on its slider (or close to it), and then lower the frequency a bit, the amplitude slider may get a red glow as a warning that it is now out of range. I want to remove that annoying behavior. If the amplitude is already close to its max value, and you lower the frequency to a negative value, the amplitude value should adapt so it's never out of range. It should automatically be "dragged" to its maximum value instead of find itself out of range.

What would be the simplest way to achieve this in the manipulate code above ?

$\endgroup$
  • 1
    $\begingroup$ Does this question and answer help? $\endgroup$ – march Feb 3 '16 at 22:23
  • $\begingroup$ @march, sorry I overlooked your comment. I don't understand the answer to the question you're refering. I'm not sure it is related. Please, see my update above for the glitch in a partial solution. $\endgroup$ – Cham Feb 5 '16 at 14:27
  • 1
    $\begingroup$ Why not just add A = Min[A, Which[f < 0, 0.5, f >= 0, 1]]; or equivalent before the Plot? $\endgroup$ – Simon Woods Feb 5 '16 at 18:50
  • $\begingroup$ I didn't knew that trick. It appears to be working great. But just to be picky : using this trick makes the red glow to flash for a brief moment, just before the slider gets corrected. Can it be avoided at all ? Is there a command to tell Mathematica to not warn the user of any out of range value ? $\endgroup$ – Cham Feb 6 '16 at 0:27
  • $\begingroup$ Feature, or bug ? I just noticed that removing the 0.01 resolution in the control removes the glow effect, in my main code (but not in the simple example above) ! This is weird ! Now, I have many digits shown on the slider's label. Is there a command to cut or round the label value ? $\endgroup$ – Cham Feb 6 '16 at 1:29
1
$\begingroup$

The simplest way I thick is to use Dynamic["your function"] instated of 1 in your controller.

Control[{{A, 0.1, "Amplitude"}, 0, Dynamic["your function"], 0.01, Appearance -> {"Labeled", "Closed"}}]

I think this will give you want you want, (assuming the function of the end is f+1):

Manipulate[
 Plot[A Sin[2 Pi f t/12], {t, 0, 12}, PlotRange -> {{0, 12}, {-1, 1}},
   AspectRatio -> 1, Frame -> True, Axes -> True, ImageSize -> 300],

 Row[{
   Row[{"frequency   ", 
     Manipulator[
      Dynamic[f, (f = #; A = If[A > f + 1, f + 1, A]) &], {1, 10, 
       0.01}, Appearance -> {"Labeled", "Closed"}]}],
   Spacer[125],
   Control[{{A, 0.1, "Amplitude"}, 0, Dynamic[f + 1], 0.01, 
     Appearance -> {"Labeled", "Closed"}}
    ]
   }],
 ControlPlacement -> Bottom]
$\endgroup$
  • $\begingroup$ This appears to work. However, there's a glitch : when I move the first slider (frequency), the other slider (amplitude) moves as well. This isn't a problem. But if the second slider is already close to its end, and I move the first slider to the left (lowering the frequency), there's a red glow that appears on the second slider as a kind of warning. The amplitude's end value hasen't properly adapted to the new lower frequency. If I then move a bit the second slider, the red glow disappears. How to fix this glitch ? $\endgroup$ – Cham Feb 4 '16 at 1:14
  • $\begingroup$ The moving in the second slide is logic because you are changing the range of the second slider. 0.5 is at the middle of slider has range from 0 to 1 but 0.5 is at quarter for range from 0 to 2. For the second issue it is not glitch it is basically A is out of range. Because when you move f to the right and then A to the right let us say you give A value of 3 for range 0 to 5, then when you move f to the left you basically reduce the range of A let use say from 0 to 1 while value of A is still 3 which will give the red singe indicating that A is out of range $\endgroup$ – Algohi Feb 4 '16 at 5:02
  • $\begingroup$ Yes, I understand all this, but the red glow warning is irritating. Is there a way to make the A slider to move away, so it's never out of range ? $\endgroup$ – Cham Feb 4 '16 at 12:43
  • $\begingroup$ @Cham see that update. $\endgroup$ – Algohi Feb 4 '16 at 18:17
  • $\begingroup$ It's working, but I don't understand how and what it's actually doing yet. I'll have to study this. $\endgroup$ – Cham Feb 4 '16 at 22:17

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.