13
$\begingroup$

I would like to use dependent sliders in Manipulate. First slider K0 should be independent and varied from Kmin to Kmax. Two others sliders K1 and K2 should dependend on K0 so that K1*K2 == K0 should be always true. Is it possible?

$\endgroup$
1
  • $\begingroup$ Strangely, I've failed to find any related post about this fairly basic question (maybe this about interrelated sliders), and the Documentation Center only has a somewhat related example. $\endgroup$ Nov 15, 2012 at 19:08

2 Answers 2

11
$\begingroup$

As always, here is the more customizable DynamicModule version:

DynamicModule[{k0, k1, k2, kMin = -1, kMax = 1},
 Panel[Grid[{
   {"k0", Slider[Dynamic[k0, (k0 = #; k2 = k0/k1) &], {kMin, kMax}], Dynamic@k0},
   {"k1", Slider[Dynamic[k1, (k1 = #; k2 = k0/k1) &], {-1, 1}, Exclusions -> {0}], Dynamic@k1},
   {"k2", Slider[Dynamic[k2, (k2 = #; k1 = k0/k2) &], {-1, 1}, Exclusions -> {0}], Dynamic@k2},
   {Panel[Dynamic@Grid[{
         {"k0", k0}, {"k1", k1}, {"k2", k2}, {"k1*k2", k1*k2}}, 
        Alignment -> Left], ImageSize -> {250, 90}], SpanFromLeft}
   }, Alignment -> Left], ImageSize -> {300, 200}]
 ]

Mathematica graphics

Note that division by zero is handled by Exclusions -> {0}.

$\endgroup$
1
  • 5
    $\begingroup$ Use the Exclusions option for Slider to prevent landing on zero and division by zero. $\endgroup$ Nov 15, 2012 at 21:24
1
$\begingroup$

Here's one thing you might try

 k0Old = 1; k1Old = 1; k2Old = 1; kMin = 1; kMax = 2;
 Manipulate[
   If[k1 != k1Old || k0 != k0Old, k2 = k0/k1; k1Old = k1; k0 = k0Old];
   If[k2 != k2Old, k1 = k0/k2; k2Old = k2;];
      {k0, k1, k2, k1 k2}, {k0, kMin, kMax}, {k1, 0.1, 10}, {k2, 0.1, 10}]
$\endgroup$

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.